Edgewall Software

Changes between Version 70 and Version 71 of TracDev/PluginDevelopment


Ignore:
Timestamp:
May 10, 2015, 6:40:11 AM (9 years ago)
Author:
Jun Omae
Comment:

Removed import * statements

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/PluginDevelopment

    v70 v71  
    99To 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:
    1010
    11 {{{
    12 #!python
    13 from trac.core import *
     11{{{#!python
     12from trac.core import Component, implements
    1413from trac.util.html import html
    1514from trac.web import IRequestHandler
     
    1716
    1817class HelloWorldPlugin(Component):
     18
    1919    implements(INavigationContributor, IRequestHandler)
    2020
    2121    # INavigationContributor methods
     22
    2223    def get_active_navigation_item(self, req):
    2324        return 'helloworld'
     25
    2426    def get_navigation_items(self, req):
    2527        yield ('mainnav', 'helloworld',
    26             html.A('Hello world', href= req.href.helloworld()))
     28               html.a('Hello world', href=req.href.helloworld()))
    2729
    2830    # IRequestHandler methods
    2931    def match_request(self, req):
    3032        return req.path_info == '/helloworld'
     33
    3134    def process_request(self, req):
    3235        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')
    3837}}}
    3938
     
    6463The 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:
    6564
    66 {{{
    67 #!python
     65{{{#!python
    6866from setuptools import find_packages, setup
    6967
     
    9088
    9189A 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
    9491cd /path/to/pluginsource
    9592python setup.py install
     
    9794
    9895To 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
    10197cd /path/to/pluginsource
    10298python setup.py bdist_egg
     
    105101
    106102During 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
    109104cd /path/to/pluginsource
    110105python setup.py develop --multi-version --exclude-scripts --install-dir /path/to/projenv/plugins
     
    112107
    113108or the short version:
    114 {{{
    115 #!sh
     109{{{#!sh
    116110python setup.py develop -mxd /path/to/projenv/plugins
    117111}}}
     
    130124
    131125For example, to disable the built-in Wiki macro `RecentChanges`, you'd include the following in [wiki:TracIni trac.ini]:
    132 {{{
    133 #!ini
     126{{{#!ini
    134127[components]
    135128trac.wiki.macros.RecentChangesMacro = disabled
     
    137130
    138131You 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
    141133[components]
    142134trac.wiki.* = disabled
     
    149141The logging API is a very good debugging tool. For example, use this code when you want to view the value of a variable:
    150142
    151 {{{
    152 #!python
     143{{{#!python
    153144env.log.debug("*** Hey, varname is %r ***", varname)
    154145}}}
     
    156147
    157148If you are inside the methods of a `Component` subclass, better use:
    158 {{{
    159 #!python
     149{{{#!python
    160150self.log.debug("Hey, varname is %r", varname)
    161151}}}