前回からの続き。
Trac HacksにあったTracのプラグインの作り方を見ながらHello Worldプラグインを作ってみた。
% mkdir -p ~/trac_make_plugin/helloworld-plugin/helloworld/
% mkdir -p ~/trac_make_plugin/helloworld-plugin/TracHelloworld.egg-info/
「~/trac_make_plugin/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 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', Markup('<a href="%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)
abuffer = 'Hello world!'
req.send_header('Content-Type', 'text/plain')
req.send_header('Content-length', str(len(abuffer)))
req.end_headers()
req.write(abuffer)
「~/trac_make_plugin/helloworld-plugin/helloworld/」に__init__.pyを作成。
# Helloworld module
from helloworld import *
「~/trac_make_plugin/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},
)
「~/trac_make_plugin/helloworld-plugin/TracHelloworld.egg-info/」にtrac_plugin.txtを作成。
% cd ~/trac_make_plugin/helloworld-plugin/
% python setup.py bdist_egg
これで「dist」ディレクトリが生成され、配下にeggファイルができる。
% ls ~/trac_make_plugin/helloworld-plugin/dist
TracHelloworld-0.1-py2.7.egg
これをtracのpluginsディレクトリにコピーして、tracを起動するとプラグインが読み込まれる。
% cp ~/trac_make_plugin/helloworld-plugin/dist/TracHelloworld-0.1-py2.7.egg ~/trac/testproject/plugins
% tracd -p 8080 --auth="*,/Users/trac/trac/users.htdigest,trac" -e ~/trac