[trac] plugin 'helloworld'
2010-03-24 18:13:52| 分类:
scm_trac
| 标签:
|举报
|字号大中小 订阅
我们来写一个超级简单的 plugin,挖哈哈。
--------------- 目录结构 ----------------
./setup.py
./myplugs/helloworld.py
./myplugs/__init__.py
./myplugs/templates/helloworld.html
--------------- setup.py -----------------
from setuptools import find_packages, setup
setup(
name='TracHelloWorld', version='1.0',
packages=find_packages(exclude=['*.tests*']),
entry_points = """
[trac.plugins]
helloworld = myplugs.helloworld
""",
)
--------- myplugs/helloworld.py --------
import pkg_resources
from trac.core import *
from trac.util.html import html
from trac.web import IRequestHandler
from trac.web.chrome import INavigationContributor, ITemplateProvider
class HelloWorldPlugin(Component):
implements(INavigationContributor, IRequestHandler, ITemplateProvider)
# INavigationContributor
def get_active_navigation_item(self, req):
return 'helloworld'
def get_navigation_items(self, req):
yield('mainnav', 'helloworld', html.A('Hello world', href=req.href.helloworld()))
# IRequestHandler
def match_request(self, req):
return req.path_info == '/helloworld'
def process_request(self, req):
return 'helloworld.html', {}, None
# ITemplateProvider methods
def get_htdocs_dirs(self):
return []
def get_templates_dirs(self):
return [pkg_resources.resource_filename('myplugs.helloworld', 'templates')]
--- myplugs/templates/helloworld.html ----
<html>
<body>
<h1>Hello World</h1>
</body>
</html>
--------------------------------------------
$ python setup.py develop -md /path/to/projenv/plugins
# 修改 conf/trac.ini
[components]
myplugs.helloworld.HelloWorldPlugin.* = enabled
--------------------------------------------
注意,插件在 trac.ini 中的名字,是 module name + '.' + class name
评论这张
转发至微博
转发至微博
评论