| 9 | |
| 10 | === Declaring Custom Extension Points === |
| 11 | |
| 12 | Plugins 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 |
| 15 | from trac.core import Interface |
| 16 | |
| 17 | class ICustomExtensionPoint(Interface): |
| 18 | """The extension point interface allows plugins to ... |
| 19 | """ |
| 20 | |
| 21 | def do_something(): |
| 22 | """Does something.""" |
| 23 | }}} |
| 24 | |
| 25 | Please 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. |
| 26 | This 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 | |
| 30 | In 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 |
| 33 | from trac.core import Component, implements |
| 34 | |
| 35 | class CustomComponent(Component): |
| 36 | |
| 37 | implements(ICustomExtensionPoint) |
| 38 | |
| 39 | # ICustomExtensionPoint methods |
| 40 | |
| 41 | def do_something(self): |
| 42 | self.log.debug("Hello World") |
| 43 | }}} |
| 44 | |
| 45 | When 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 | |
| 47 | More detailed information on this can be found below under [wiki:TracDev/PluginDevelopment#Writingtheplugincode]. |
| 48 | |
| 49 | == Using Extension Points == |
| 50 | |
| 51 | In 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 |
| 54 | from trac.core import Component, ExtensionPoint |
| 55 | |
| 56 | class SampleModule(Component): |
| 57 | |
| 58 | custom_extensions = ExtensionPoint(ICustomExtensionPoint) |
| 59 | |
| 60 | def example(): |
| 61 | for ext in self.custom_extensions: |
| 62 | ext.do_something() |
| 63 | }}} |