Changes between Version 6 and Version 7 of TracDev/PluginDevelopment
- Timestamp:
- Aug 29, 2005, 2:06:28 AM (19 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/PluginDevelopment
v6 v7 5 5 == Extension points == 6 6 7 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'' (not available here yet).7 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. 8 8 9 9 Currently we have: 10 10 11 * {{{trac.core.IEnvironmentSetupParticipant}}} [[BR]] 12 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 13 * {{{trac.web.main.IRequestHandler}}} [[BR]] 14 Allows plugins to add handlers for HTTP requests. 15 * {{{trac.web.chrome.INavigationContributor}}} [[BR]] 16 Allows plugins to extend the navigation menus of the web interface. 17 * {{{trac.web.chrome.ITemplateProvider}}} [[BR]] 18 Extension point interface for components that provide their own ClearSilver templates and accompanying static resources. 19 * {{{trac.perm.IPermissionRequestor}}} [[BR]] 20 Plugins can use this extension point to define additional "actions" for the permission system. 21 * {{{trac.Timeline.ITimelineEventProvider}}} [[BR]] 22 Allows plugins to contribute events to the [wiki:TracTimeline timeline]. 23 * {{{trac.mimeview.api.IHTMLPreviewRenderer}}} [[BR]] 24 Allows plugins to provide support for rendering specific content of a specific type as HTML (used for TracSyntaxColoring and image preview) 25 * {{{trac.wiki.api.IWikiChangeListener}}} [[BR]] 26 Allows plugins to observe creation, modification and deletion of wiki pages. 27 * {{{trac.wiki.api.IWikiMacroProvider}}} [[BR]] 28 Allows plugins to contribute WikiMacros to Trac. 29 * {{{trac.wiki.api.IWikiSyntaxProvider}}} [[BR]] 30 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. 11 `trac.core.IEnvironmentSetupParticipant`:: 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 12 `trac.web.main.IRequestHandler`:: Allows plugins to add handlers for HTTP requests. 13 `trac.web.chrome.INavigationContributor`:: Allows plugins to extend the navigation menus of the web interface. 14 `trac.web.chrome.ITemplateProvider`:: Extension point interface for components that provide their own ClearSilver templates and accompanying static resources. 15 `trac.perm.IPermissionRequestor`:: Plugins can use this extension point to define additional "actions" for the permission system. 16 `trac.Timeline.ITimelineEventProvider`:: Allows plugins to contribute events to the [wiki:TracTimeline timeline]. 17 `trac.mimeview.api.IHTMLPreviewRenderer`:: Allows plugins to provide support for rendering specific content of a specific type as HTML (used for TracSyntaxColoring and image preview) 18 `trac.wiki.api.IWikiChangeListener`:: Allows plugins to observe creation, modification and deletion of wiki pages. 19 `trac.wiki.api.IWikiMacroProvider`:: Allows plugins to contribute WikiMacros to Trac. 20 `trac.wiki.api.IWikiSyntaxProvider`:: 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. 31 21 32 22 ''Note that plugins can themselves add new extension points, so the list above is incomplete by nature.'' 33 23 34 == Adding custom plugins==24 == Writing the plugin code == 35 25 36 To extend Trac with a custom plugin, you need to providea ''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:26 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: 37 27 38 28 {{{ 39 29 #!python 40 30 from trac.core import * 31 from trac.web import IRequestHandler 41 32 from trac.web.chrome import INavigationContributor 42 from trac.web.main import IRequestHandler43 33 44 34 class HelloWorldPlugin(Component): … … 72 62 * {{{log}}}: The configured logger, see the Python [http://docs.python.org/lib/module-logging.html logging API] for more information. 73 63 74 == Deploying a custom plugin == 64 These variables can also be accessed from the initializer (`__init__`) of a component. 75 65 76 To register a custom plugin with a Trac environment, you edit the [wiki:TracIni trac.ini] file to add a new configuration section for your plugin. Let's assume the above plugin is in a Python module with the name ''example.helloworld'' 66 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. 77 67 68 == Packaging and deploying plugins == 69 70 Plugins 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. 71 72 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. 73 74 ''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.'' 75 76 A plugin can either be deployed globally, or only for a specific environment. Global deployment is done by installing the plugin: 78 77 {{{ 79 [helloworld] 80 module = example.helloworld 78 $ cd /path/to/pluginsource 79 $ python setup.py install 81 80 }}} 82 81 83 Your plugin should now get loaded and registered when Trac is run (when running under [wiki:TracModPython mod_python] or [wiki:TracStandalone tracd], you'll need to restart the server). This assumes that your module is on the Python path. If it is not, you can simply tell Trac where to look for it by using the {{{path}}} option: 84 82 To deploy a plugin only to a specific Trac environment, copy the egg file into the `plugins` directory of that environment: 85 83 {{{ 86 [helloworld] 87 module = example.helloworld 88 path = /home/cmlenz/src/trac-plugins84 $ cd /path/to/pluginsource 85 $ python setup.py bdist_egg 86 $ cp dist/*.egg /path/to/projenv/plugins 89 87 }}} 90 88 91 The name of the configuration section is not significant: Trac simply goes through all the sections and looks for an option called {{{module}}}. But if your plugin also uses its own configuration options, you should of course document the name of the section where these options are expected to be found, and the admin will probably choose to put the {{{module}}} and {{{path}}} options in the same section: 92 89 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: 93 90 {{{ 94 [helloworld] 95 module = example.helloworld 96 path = /home/cmlenz/src/trac-plugins 97 message = Hello world! 91 $ cd /path/to/pluginsource 92 $ python setup.py develop --install-dir=/path/to/projenv/plugins 98 93 }}} 99 94 100 You can use this configuration option from the plugin as follows:95 You can omit the `--install-dir` argument to make the development version of your plugin available globally. 101 96 102 {{{ 103 #!python 104 ... 105 class HelloWorldPlugin(Component): 106 ... 107 def process_request(self, req): 108 req.send_response(200) 109 req.send_header('Content-Type', 'text/plain') 110 req.end_headers() 111 req.write(self.config.get('helloworld', 'message') 112 }}} 97 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. 113 98 114 99 == Disabling built-in components ==