{{{ #!trachtml

Egg cooking


[http://trac-hacks.org/wiki/EggCookingTutorial/BasicEggCooking 原文] [[VisitCounter(TracDoc/BasicEggCookingJa)]]

Since Trac 0.9 it has been possible to write plugins for Trac to extend Trac functionality. Even better, you can deploy plugins as [http://peak.telecommunity.com/DevCenter/PythonEggs Python eggs] that really makes plugin development fun and easy.

こ のチュートリアルはeggを作る方法とeggをTracにロードする方法を示します。なお、Advanced Partsで、テンプレートや静的コンテンツをeggから提 供する方法を学ぶ。 This tutorial shows how to make an egg, and load an egg in Trac. In the advanced parts you'll learn how to serve templates and static content from an egg.

You should be familiar with [trac:TracDev/ComponentArchitecture component architecture] and [trac:TracDev/PluginDevelopment plugin development] . This plugin is based on the example in the plugin development article. Here we extend it a bit further.

あなたは、[http://projects.edgewall.com/trac/wiki/TracDev/ComponentArchitecture component architecture] や [wiki:TracDoc/PluginDevelopmentJa plugin development] も見ておくべき。今回のプラグインはplugin developmentの記事の例に基づいている。ここでは、それにちょっと深入りするけど。

Required items (必要なもの)

First you need setuptools. For instructions and files see [http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install EasyInstall] page.

You also need Trac 0.9. Download it from the [trac:TracDownload#LatestDevelopmentRelease0.9beta2 TracDownload] page.

まず、setuptoolsが必要。[http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install EasyInstall] を参照。
Trac0.9も必要。

Directories

To develop a plugin you need to create a few directories to keep things together.
プラグインを開発するには、いくつかのディレクトリを定型で作る必要がある。

So let's create following directories:
以下のディレクトリをつくってみよう。

./helloworld-plugin/
./helloworld-plugin/helloworld/
./helloworld-plugin/TracHelloworld.egg-info/

Main plugin

The first step is to generate the main module for this plugin. We will construct a simple plugin that will display "Hello world!" on the screen when accessed through the /helloworld URL. The plugin also provides a "Hello" button that is, by default, rendered on the far right in the main navigation bar.
第一に行うのは、プラグインのメインモジュールの作成です。
これから、/helloworldというURLリクエストに対して"Hello world!"とだけ表示する、簡単なプラグインを作ります。
このプラグインはメインナビゲーションバーに"Hello"ボタンを表示します。

So create helloworld.py in ./helloworld-plugin/helloworld/:
./helloworld-plugin/helloworld/にhelloworld.pyを作ります。

# Helloworld plugin

from trac.core import *
from trac.web.chrome import INavigationContributor
from trac.web.main import IRequestHandler
from trac.util import escape, Markup

class UserbaseModule(Component):
implements(INavigationContributor, IRequestHandler)

# INavigationContributor methods
def get_active_navigation_item(self, req):
return 'helloworld'

def get_navigation_items(self, req):
yield 'mainnav', 'helloworld', Markup('<a xhref="%s">Hello</a>', self.env.href.helloworld())

# IRequestHandler methods
def match_request(self, req):
return req.path_info == '/helloworld'

def process_request(self, req):
req.send_response(200)
req.send_header('Content-Type', 'text/plain')
req.end_headers()
req.write('Hello world!')


Make it a module

To make the plugin a module, you simply create an __init__.py in ./helloworld-plugin/helloworld/:
プラグインモジュールを作るには、ただ単に、__init__.pyを./helloworld-plugin/helloworld/に置けばよい。

# Helloworld module
from helloworld import *

Make it an egg

Now it's time to make it an egg. For that we need a chicken called setup.py in ./helloworld-plugin/:
では、eggを作ろう。そのためには、./helloworld-plugin/にsetup.py(私たちはニワトリと呼ぶべき)が必要。

from setuptools import setup

PACKAGE = 'TracHelloworld'
VERSION = '0.1'

setup(name=PACKAGE,
version=VERSION,
packages=['helloworld'],
entry_points={'trac.plugins': '%s = helloworld' % PACKAGE},
)

First deployment

Now try to build the plugin. Run the command python setup.py bdist_egg in the directory where you created it. If everthing went OK you should have a .egg file in ./dist directory.
プラグインをビルドしてみる。以下のコマンドをあなたが作ったディレクトリで実行する。もし、すべてうまくいけば ./dist ディレクトリに .eggファイルができている。

python setup.py bdist_egg 

 

Copy this .egg file to /[your trac env]/plugins directory. If you're using mod_python you have to restart Apache.
.eggファイルを /[あなたの!TracEnv]/plugins ディレクトリにコピーしてください。もしmod_pythonを使っているなら、Apacheを再起動してください。

Now you should see Hello link at far right in main navigation bar when accessing your site. Click it.
メインナビゲーションバーの一番右にHelloリンクが見えているはずです。クリックしてみてください。

Aftermath

Now you have successfully created your first egg. You can continue by reading [wiki:EggCookingTutorial/AdvancedEggCooking EggCookingTutorial/AdvancedEggCooking] to learn how to use templates in your plugins, and make its output look like other Trac pages.
以上で、ファーストエッグの作成は終わりです。他のTracのページのようにプラグインでテンプレートを使いたければ、続けて、[wiki:TracDoc/AdvancedEggCookingja EggCookingTutorial/AdvancedEggCooking] を読んでください。

 

 


訳注:以下は、どこかのページに書いてあった、開発の再に便利な方法

To deploy a plugin only to a specific Trac environment, copy the egg file into the plugins directory of that environment:

$ cd /path/to/pluginsource
$ python setup.py bdist_egg
$ cp dist/*.egg /path/to/projenv/plugins

During development of a plugin, it is inconvenient to have to install it in either of the ways described above. Instead, you should use the setuptools develop command:

$ cd /path/to/pluginsource
$ python setup.py develop --install-dir=/path/to/projenv/plugins

You can omit the --install-dir argument to make the development version of your plugin available globally.

This will install an .egg-link file instead of the actual egg. That file is basically a link to the source directory of your plugin, so that Trac will always see the latest version of your code.
.egg-linkファイルが実際のeggファイルの代わりに出来る。このファイルは基本的にソースディレクトリへのリンクであるので、Tracは常に最新のソースを使うことになる。

(2006/04/12)

}}}