Changes between Version 70 and Version 71 of TracDev/PluginDevelopment
- Timestamp:
- May 10, 2015, 6:40:11 AM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/PluginDevelopment
v70 v71 9 9 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: 10 10 11 {{{ 12 #!python 13 from trac.core import * 11 {{{#!python 12 from trac.core import Component, implements 14 13 from trac.util.html import html 15 14 from trac.web import IRequestHandler … … 17 16 18 17 class HelloWorldPlugin(Component): 18 19 19 implements(INavigationContributor, IRequestHandler) 20 20 21 21 # INavigationContributor methods 22 22 23 def get_active_navigation_item(self, req): 23 24 return 'helloworld' 25 24 26 def get_navigation_items(self, req): 25 27 yield ('mainnav', 'helloworld', 26 html.A('Hello world', href=req.href.helloworld()))28 html.a('Hello world', href=req.href.helloworld())) 27 29 28 30 # IRequestHandler methods 29 31 def match_request(self, req): 30 32 return req.path_info == '/helloworld' 33 31 34 def process_request(self, req): 32 35 content = 'Hello World!' 33 req.send_response(200) 34 req.send_header('Content-Type', 'text/plain') 35 req.send_header('Content-Length', len(content)) 36 req.end_headers() 37 req.write(content) 36 req.send(content, 'text/plain') 38 37 }}} 39 38 … … 64 63 The egg needs to export an [https://pythonhosted.org/setuptools/setuptools.html#dynamic-discovery-of-services-and-plugins entry points] group named `trac.plugins`, listing the names of the modules that Trac should import for the plugin-provided components to get registered. For example: 65 64 66 {{{ 67 #!python 65 {{{#!python 68 66 from setuptools import find_packages, setup 69 67 … … 90 88 91 89 A plugin can either be deployed globally, or only for a specific environment. Global deployment is done by installing the plugin: 92 {{{ 93 #!sh 90 {{{#!sh 94 91 cd /path/to/pluginsource 95 92 python setup.py install … … 97 94 98 95 To deploy a plugin only to a specific Trac environment, copy the egg file into the `plugins` directory of that environment: 99 {{{ 100 #!sh 96 {{{#!sh 101 97 cd /path/to/pluginsource 102 98 python setup.py bdist_egg … … 105 101 106 102 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: 107 {{{ 108 #!sh 103 {{{#!sh 109 104 cd /path/to/pluginsource 110 105 python setup.py develop --multi-version --exclude-scripts --install-dir /path/to/projenv/plugins … … 112 107 113 108 or the short version: 114 {{{ 115 #!sh 109 {{{#!sh 116 110 python setup.py develop -mxd /path/to/projenv/plugins 117 111 }}} … … 130 124 131 125 For example, to disable the built-in Wiki macro `RecentChanges`, you'd include the following in [wiki:TracIni trac.ini]: 132 {{{ 133 #!ini 126 {{{#!ini 134 127 [components] 135 128 trac.wiki.macros.RecentChangesMacro = disabled … … 137 130 138 131 You can also use a wildcard at the end of a name, so you could even disable the complete Wiki module: 139 {{{ 140 #!ini 132 {{{#!ini 141 133 [components] 142 134 trac.wiki.* = disabled … … 149 141 The logging API is a very good debugging tool. For example, use this code when you want to view the value of a variable: 150 142 151 {{{ 152 #!python 143 {{{#!python 153 144 env.log.debug("*** Hey, varname is %r ***", varname) 154 145 }}} … … 156 147 157 148 If you are inside the methods of a `Component` subclass, better use: 158 {{{ 159 #!python 149 {{{#!python 160 150 self.log.debug("Hey, varname is %r", varname) 161 151 }}}