{{{ #!trachtml

Writing Plugins for Trac


[http://projects.edgewall.com/trac/wiki/TracDev/PluginDevelopment#Writingtheplugincode 原文] [[VisitCounter(TracDoc/PluginDevelopmentJa)]]

[milestone:0.9 0.9] から、Trac の機能を拡張するためのplugins を開発できる。プラグインの機能は [http://projects.edgewall.com/trac/wiki/TracDev/ComponentArchitecture component architecture] に基づく。 このドキュメントに事前に目を通すこと。Starting with version [milestone:0.9 0.9] , you can develop plugins for Trac that extend the builtin functionality. The plugin functionality is based on the [wiki:TracDev/ComponentArchitecture component architecture] , so please read that document before continuing here.

Extension points

Trac は様々な機能のためにカスタマイズされた拡張を追加可能にする「エクステンションポイント」を提供する。 あなたのインストールしたTracのAbout Trac/Pluginsページで、提供された「エクステンションポイント」のリストを見ることができる。 Trac offers an increasing number of extension points that allow you to plugin custom extensions for various functions. You can view a list of provided extension points on the page About Trac/Plugins of your Trac installation.

Currently we have:

[http://projects.edgewall.com/trac/browser/trunk/trac/env.py trac.env.IEnvironmentSetupParticipant]

plugins が環境の作成や改善に加わるようにする。プラグインのために必要とされるデータベースのテーブルかディレクトリの追加をセットアップするのに使用できる。 Allows plugins to participate in the creation and upgrade of the environment. Can be used to setup additional database tables or directories needed for the plugin to operate
(訳注:Trac自身ではあまり使ってない。)

[http://projects.edgewall.com/trac/browser/trunk/trac/web/api.py trac.web.api.IRequestHandler]

pluginsがHTTPリクエストを扱えるようにする。Allows plugins to add handlers for HTTP requests.
(訳注:リクエストの処理を書くときに、このインタフェースを継承して作ります。Tracのほとんどのモジュールが継承してます。)

[http://projects.edgewall.com/trac/browser/trunk/trac/web/chrome.py trac.web.chrome.INavigationContributor]

pluginsがナビゲーションバーのWebインタフェースを拡張できるようにする。Allows plugins to extend the navigation menus of the web interface.
(訳注:ナビゲーションバーにリンクを追加するときに継承する。Tracのほとんどのモジュールが継承してます。)

[http://projects.edgewall.com/trac/browser/trunk/trac/web/chrome.py trac.web.chrome.ITemplateProvider]

自身のClearSilverテンプレートおよび静的な資源に伴うことを提供する部品のための拡張ポイントインターフェイス。Extension point interface for components that provide their own ClearSilver templates and accompanying static resources.

[http://projects.edgewall.com/trac/browser/trunk/trac/perm.py trac.perm.IPermissionRequestor]

Plugins はこの拡張ポイントをパーミッションシステムのための追加"アクション" を定義するのに使用できる。Plugins can use this extension point to define additional "actions" for the permission system.

[http://projects.edgewall.com/trac/browser/trunk/trac/Timeline.py trac.Timeline.ITimelineEventProvider]

plugins がタイムラインにイベントを表示できるようにする。Allows plugins to contribute events to the [wiki:TracTimeline timeline] .

[http://projects.edgewall.com/trac/browser/trunk/trac/mimeview/api.py trac.mimeview.api.IHTMLPreviewRenderer]

plugins が(TracSyntaxColoring 及びイメージのプレビューに使用する) HTML として特定のタイプの特定の内容をレンダリングするためのサポートをする提供できるようにする。

Allows plugins to provide support for rendering specific content of a specific type as HTML (used for TracSyntaxColoring and image preview)

[http://projects.edgewall.com/trac/browser/trunk/trac/wiki/api.py trac.wiki.api.IWikiChangeListener]

plugins がwiki のページの作成、修正および削除を監視するようにする。Allows plugins to observe creation, modification and deletion of wiki pages.

[http://projects.edgewall.com/trac/browser/trunk/trac/wiki/api.py trac.wiki.api.IWikiMacroProvider]

plugins がTrac にWikiMacros を提供するようにする。Allows plugins to contribute WikiMacros to Trac.

[http://projects.edgewall.com/trac/browser/trunk/trac/wiki/api.py trac.wiki.api.IWikiSyntaxProvider]

wiki の書式作成システムに注文の構文法の規則を加えるためにPlugins はこの拡張ポイントを拡張できる。特に、これはTracLinks の付加的なタイプの登録を可能にする。Plugins can extend this extension point to add custom syntax rules to the wiki formatting system. In particular, this allows registration of additional TracLinks types.

注:plugins が彼ら自身を加える新しい延長ポイントを缶詰にする、従って上記のリストは性質によって不完全である。 Note that plugins can themselves add new extension points, so the list above is incomplete by nature.

Writing the plugin code

カスタムプラグインでTrac を拡張ためには、「コンポーネント」を実装する必要がある。例えば、Tracに新しいWebモジュール (例えば:HTTP の要求を扱い、ナビゲーションバーを伸ばすコンポーネント) を加えるためには、次のようなコードを書く: To extend Trac with a custom plugin, you need to implement a component. For example, to add a new web module to Trac (i.e. a component that handles HTTP requests and extends the navigation bar), you'd start with something like the following code:

from trac.core import *
from trac.web import IRequestHandler
from trac.web.chrome import INavigationContributor

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

# INavigationContributor methods
def get_active_navigation_item(self, req):
return 'helloworld'
def get_navigation_items(self, req):
yield 'mainnav', 'helloworld', '<a xhref="%s">Hello World</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!')
 

期待する結果を得るためには、拡張ポイントインターフェイスのAPI ドキュメンテーションを見なさい。 Look at the API documentation for the extension point interfaces to see what you're expected to return.

Component member variables

Trac の環境にインストールされるあらゆるコンポーネントは、3つの外部メンバー変数を使用可能である。: Every component that gets instantiated through the Trac environment gets three extra member variables for convenience:

これらの変数はコンポーネントの初期設定('init') でもアクセスすることができる。These variables can also be accessed from the initializer (__init__) of a component.

あ なたのコンポーネントのインスタンス変数として他のオブジェクトを蓄えることは、おそらくバッドアイディアである。:コンポーネントは与 えられた環境で一度しかインスタンス化されないことを覚えなさい; あなたのプラグインがTrac のCGI の配置で使用されなければ、たくさんのHTTPリクエストごと同じコンポーネントインスタンスが呼び出される。もしサーバがマルチスレッドなら、これは同 時に起こる。 Storing any other objects as instance variables of your component is probably a bad idea: remember that a component is only instantiated once for a given environment; unless your plugin is used in a CGI deployment of Trac, that means that the same component instance will get invoked for multiple HTTP requests; if the server is multi-threaded, this will even happen concurrently.
(訳注:この行は意味がよくわからない。重要そうなんだけど。。。)


以下は、情報が古い。 TracDoc/BasicEggCookingJa を参照してください。

 

Packaging and deploying plugins

TracPluginsは[http://peak.telecommunity.com/DevCenter/PythonEggs Python Eggs] としてパッケージ化される。 あなたのプラグインのsetup.pyを作るために、[http://peak.telecommunity.com/DevCenter/setuptools setuptools] を使うことが可能である。 TracPlugins are packaged as [http://peak.telecommunity.com/DevCenter/PythonEggs Python Eggs] . You can use [http://peak.telecommunity.com/DevCenter/setuptools setuptools] to make a setup.py script that will produce a Python Egg for your plugin.

egg ファイルは、そのEGG-INFOディレクトリ内にtrac_plugin.txtという名前のファイルが必要である。 このファイルは、あなたのモジュールを登録するためにTracがインポートする必要があるすべてのモジュールの名前を含むべきである。 The egg file needs to have a file named trac_plugin.txt in its EGG-INFO directory. This file should contain the names of all modules that need to be imported by Trac to register your components.

注:これが近い将来以内に大きくに変わる: setuptools 0.6 はtrac_plugin.txt のディスクリプターの代りに使用される"エントリーポイント" の概念をもたらす。詳細についてはTracPlugins を見なさい。 Note that this will change in the very near future: setuptools 0.6 will introduce the concept of “entry points”, which will be used instead of the trac_plugin.txt descriptor.. See TracPlugins for details.

A plugin can either be deployed globally, or only for a specific environment. Global deployment is done by installing the plugin:

$ cd /path/to/pluginsource
$ python setup.py install

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

Pluginの開発の間に、上記述されている方法のどちらかにそれをインストールが必要なことは不便である。その代りに、setuptools の'develop' コマンドを使用するべきである: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

globallyに使えるプラグインのバージョンを作る際には、--install-dirは省略できる。 You can omit the --install-dir argument to make the development version of your plugin available globally. これは実際のeggの代りに.egg-linkファイルをインストールする。そのファイルはあなたのPluginのソースディレクトリへTrac が常にあなたのコードの最も最近の版を見るような、基本的なリンクである。この場合TracPlugins で説明されるようにtrac コンフィギュレーションであなたのpluginを明確にenableにしなければならない。 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. In this case you will have to explicitly enable your plugin in the trac configuration as explained on TracPlugins.

A tutorial to build your own plugins is available [http://trac-hacks.swapoff.org/wiki/EggCookingTutorial here] .

Disabling built-in components

Sometimes you might want to write a plugin that completely replaces a built-in component, for example to develop an advanced variant of an existing module. Trac uses a list of default component to load, as specified in the default_components list in [source:/trunk/trac/db_default.py#latest trac.db_default] . These built-in components are always loaded, and might therefore conflict with your replacement plugin.

You can however disable built-in components using a special [wiki:TracIni trac.ini] section called [components]. This section contains the qualified name of the components to disable, along with disabled or off as the value.

For example, to disable the built-in Wiki macro RecentChanges, you'd include the following in [wiki:TracIni trac.ini] :

[components]
trac.wiki.macros.RecentChanges = disabled

You can also use a wildcard at the end of a name, so you could even disable the complete Wiki module (although wiki formatting will still work in the remaining modules, of course):

[components]
trac.wiki.* = disabled

See also: TracDev, TracDev/ComponentArchitecture, TracPlugins BasicEggCooking AdvancedCookingOne AdvancedCookingTwo

}}}