Edgewall Software

Changes between Version 35 and Version 36 of TracDev/PluginDevelopment


Ignore:
Timestamp:
May 6, 2010, 9:29:59 PM (14 years ago)
Author:
Carsten Klein <carsten.klein@…>
Comment:

Added information on how to author custom extension point interfaces and make use of these

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/PluginDevelopment

    v35 v36  
    77
    88Trac 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.
     9
     10=== Declaring Custom Extension Points ===
     11
     12Plugins can declare additional extension points. In order to do so, one must inherit from [source:trunk/trac/core.py trac.core.Interface].
     13
     14{{{#!python
     15from trac.core import Interface
     16
     17class ICustomExtensionPoint(Interface):
     18    """The extension point interface allows plugins to ...
     19    """
     20
     21    def do_something():
     22        """Does something."""
     23}}}
     24
     25Please note that all extension point interfaces need to be authored so that their declared methods are unbound, that is, you simply omit the 'self' as first parameter to the method.
     26This is so, that future extensions or changes to the API can be introduced, by for example adding new or removing existing parameters without causing breakage of existing components implementing the interface.
     27
     28== Implementing Extension Points ==
     29
     30In order to implement an extension point, your class must be derived from [source:trunk/trac/core.py trac.core.Component] and it must implement the extension point interface.
     31
     32{{{#!python
     33from trac.core import Component, implements
     34
     35class CustomComponent(Component):
     36
     37    implements(ICustomExtensionPoint)
     38
     39    # ICustomExtensionPoint methods
     40
     41    def do_something(self):
     42        self.log.debug("Hello World")
     43}}}
     44
     45When implementing the methods of the interface, the methods have to be bound to the instance, so you will have to specify 'self' as the first parameter.
     46
     47More detailed information on this can be found below under [wiki:TracDev/PluginDevelopment#Writingtheplugincode].
     48
     49== Using Extension Points ==
     50
     51In order for your custom component making use of plugins that implement all or one custom extension point interface provided by you, you will have to again derive your component from trac.core.Component, and then use [source:trunk/trac/core.py trac.core.ExtensionPoint] to get the instances that implement your custom extension point interface.
     52
     53{{{#!python
     54from trac.core import Component, ExtensionPoint
     55
     56class SampleModule(Component):
     57
     58    custom_extensions = ExtensionPoint(ICustomExtensionPoint)
     59
     60    def example():
     61        for ext in self.custom_extensions:
     62            ext.do_something()
     63}}}
    964
    1065=== Available Extension Points ===