| 1 | = Egg cooking = |
| 2 | |
| 3 | Since Trac 0.9 it has been possible to write plugins. Even better, you can deploy plugins as [http://peak.telecommunity.com/DevCenter/PythonEggs Python eggs]). |
| 4 | |
| 5 | This tutorial shows how to make an egg, successfully load an egg in Trac and in advanced topics how to serve templates and static content from an egg. |
| 6 | |
| 7 | You should be familiar with [http://projects.edgewall.com/trac/wiki/TracDev/ComponentArchitecture component architecture] and [http://projects.edgewall.com/trac/wiki/TracDev/PluginDevelopment plugin development]. This plugin is based on example in that plugin development article we just extend it a bit further. |
| 8 | |
| 9 | == Required items == |
| 10 | |
| 11 | First you need ''setuptools''. For instructions and files see [http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install EasyInstall] page. |
| 12 | |
| 13 | Then you need of course Trac 0.9. Currently, it means source checkout from Subversion repository. Instructions for getting it done are located at [http://projects.edgewall.com/trac/wiki/TracDownload#LatestDevelopmentSourceCode1 TracDownload] page. |
| 14 | |
| 15 | == Directories == |
| 16 | |
| 17 | To develop a plugin you need to create few directories to keep things together. |
| 18 | |
| 19 | So let's create following directories: |
| 20 | {{{ |
| 21 | ./helloworld-plugin/ |
| 22 | ./helloworld-plugin/helloworld/ |
| 23 | ./helloworld-plugin/TracHelloworld.egg-info/ |
| 24 | }}} |
| 25 | |
| 26 | == Main plugin == |
| 27 | |
| 28 | First step is to generate main module for this plugin. We will construct simple plugin that will display "Hello world!" on screen when accessed through /helloworld URL. Plugin also provides "Hello" button that is by default rendered on far right in main navigation bar. |
| 29 | |
| 30 | So create ''helloworld.py'' in ''./helloworld-plugin/helloworld/'': |
| 31 | {{{ |
| 32 | # Helloworld plugin |
| 33 | |
| 34 | from trac.core import * |
| 35 | from trac.web.chrome import INavigationContributor |
| 36 | from trac.web.main import IRequestHandler |
| 37 | from trac.util import escape |
| 38 | |
| 39 | class UserbaseModule(Component): |
| 40 | implements(INavigationContributor, IRequestHandler) |
| 41 | |
| 42 | # INavigationContributor methods |
| 43 | def get_active_navigation_item(self, req): |
| 44 | return 'helloworld' |
| 45 | |
| 46 | def get_navigation_items(self, req): |
| 47 | yield 'mainnav', 'helloworld', '<a href="%s">Hello</a>' \ |
| 48 | % escape(self.env.href.helloworld()) |
| 49 | |
| 50 | # IRequestHandler methods |
| 51 | def match_request(self, req): |
| 52 | return req.path_info == '/helloworld' |
| 53 | |
| 54 | def process_request(self, req): |
| 55 | req.send_response(200) |
| 56 | req.send_header('Content-Type', 'text/plain') |
| 57 | req.end_headers() |
| 58 | req.write('Hello world!') |
| 59 | |
| 60 | }}} |
| 61 | |
| 62 | == Make it as a module == |
| 63 | |
| 64 | Since this is not enough, we need to make our simple plugin as a module. To do so, you simply create that magic ''!__init!__.py'' into ''./helloworld-plugin/helloworld/'': |
| 65 | {{{ |
| 66 | # Helloworld module |
| 67 | from helloworld import * |
| 68 | }}} |
| 69 | |
| 70 | == Make it as an egg == |
| 71 | |
| 72 | Now it's time to make it as an egg. For that we need a chicken called ''setup.py'' that is created into ''./helloworld-plugin/'': |
| 73 | {{{ |
| 74 | from setuptools import setup |
| 75 | |
| 76 | PACKAGE = 'TracHelloworld' |
| 77 | VERSION = '0.1' |
| 78 | |
| 79 | setup(name=PACKAGE, version=VERSION, packages=['helloworld']) |
| 80 | }}} |
| 81 | |
| 82 | To make egg loadable in Trac we need to create one file more. in ''./helloworld-plugin/!TracHelloworld.egg-info/'' create file ''trac_plugin.txt'': |
| 83 | {{{ |
| 84 | helloworld |
| 85 | }}} |
| 86 | |
| 87 | == First deployment == |
| 88 | |
| 89 | Now you could try to build your first plugin. Run command {{{python setup.py bdist_egg}}} in directory where you created it. If everthing went OK you should have small .egg file in ''./dist'' directory. |
| 90 | |
| 91 | Copy this ''.egg'' file to ''/[your trac env]/plugins'' directory. If you're using mod_python you have to restart Apache. |
| 92 | |
| 93 | Now you should see ''Hello'' link at far right in main navigation bar when accessing your site. Click it. |
| 94 | |
| 95 | == Aftermath == |
| 96 | |
| 97 | Now you have successfully created your first egg. You can continue now reading [wiki:EggCookingTutorial/AdvancedEggCooking EggCookingTutorial/AdvancedEggCooking] to really integrate plugin into Trac layout. |
| 98 | |