| 1 | [[TOC]] |
| 2 | |
| 3 | = Egg cooking = |
| 4 | |
| 5 | Since Trac 0.11, Genshi is used as the new template engine. Some APIs have also been changed. |
| 6 | |
| 7 | This tutorial shows how to make an egg, and load an egg in Trac. In the advanced parts you'll learn how to serve templates and static content from an egg. |
| 8 | |
| 9 | You should be familiar with [trac:TracDev/ComponentArchitecture component architecture] and [trac:TracDev/PluginDevelopment plugin development]. This plugin is based on the example in the plugin development article. Here we extend it a bit further. |
| 10 | |
| 11 | == Required items == |
| 12 | |
| 13 | * ''setuptools>=0.6b1''. For instructions and files see [http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install EasyInstall] page. |
| 14 | * '''Trac 0.11'''. Download it from the [http://projects.edgewall.com/trac/wiki/TracDownload TracDownload] page. |
| 15 | * '''Genshi>-0.4'''. Download it from the [http://genshi.edgewall.org/wiki/Download GenshiDownload] page. |
| 16 | |
| 17 | == Directories == |
| 18 | |
| 19 | To develop a plugin you need to create a few directories to keep things together. |
| 20 | |
| 21 | So let's create following directories: |
| 22 | {{{ |
| 23 | ./helloworld-plugin/ |
| 24 | ./helloworld-plugin/helloworld/ |
| 25 | }}} |
| 26 | |
| 27 | == Main plugin == |
| 28 | |
| 29 | The first step is to generate the main module for this plugin. We will construct a simple plugin that will display "Hello world!" on the screen when accessed through the /helloworld URL. The plugin also provides a "Hello" button that is, by default, rendered on the far right in the main navigation bar. |
| 30 | |
| 31 | So create ''helloworld.py'' in ''./helloworld-plugin/helloworld/'': |
| 32 | {{{ |
| 33 | #!python |
| 34 | # Helloworld plugin |
| 35 | |
| 36 | import re |
| 37 | |
| 38 | from genshi.builder import tag |
| 39 | |
| 40 | from trac.core import * |
| 41 | from trac.web import IRequestHandler |
| 42 | from trac.web.chrome import INavigationContributor |
| 43 | |
| 44 | class HelloWorldPlugin(Component): |
| 45 | implements(INavigationContributor, IRequestHandler) |
| 46 | |
| 47 | # INavigationContributor methods |
| 48 | def get_active_navigation_item(self, req): |
| 49 | return 'helloworld' |
| 50 | |
| 51 | def get_navigation_items(self, req): |
| 52 | yield ('mainnav', 'helloworld', |
| 53 | tag.a('Hello World', href=req.href.helloworld())) |
| 54 | |
| 55 | # IRequestHandler methods |
| 56 | def match_request(self, req): |
| 57 | return re.match(r'/helloworld(?:_trac)?(?:/.*)?$', req.path_info) |
| 58 | |
| 59 | def process_request(self, req): |
| 60 | req.send_response(200) |
| 61 | req.send_header('Content-Type', 'text/plain') |
| 62 | req.end_headers() |
| 63 | req.write('Hello world!') |
| 64 | }}} |
| 65 | |
| 66 | To help understand how that works, read the [http://trac.edgewall.org/browser/trunk/trac/web/chrome.py#L108 INavigationContributor] and [http://trac.edgewall.org/browser/trunk/trac/web/api.py#L517 IRequestHandler] interface specifications. |
| 67 | |
| 68 | == Make it a module == |
| 69 | |
| 70 | To make the plugin a module, you simply create an ''_''____''_init_''____''_.py'' in ''./helloworld-plugin/helloworld/'': |
| 71 | {{{ |
| 72 | #!python |
| 73 | # Helloworld module |
| 74 | from helloworld import * |
| 75 | }}} |
| 76 | |
| 77 | == Make it an egg == |
| 78 | |
| 79 | Now it's time to make it an egg. For that we need a chicken called ''setup.py'' in ''./helloworld-plugin/'': |
| 80 | {{{ |
| 81 | #!python |
| 82 | from setuptools import find_packages, setup |
| 83 | |
| 84 | # name can be any name. This name will be used to create .egg file. |
| 85 | # name that is used in packages is the one that is used in the trac.ini file. |
| 86 | # use package name as entry_points |
| 87 | setup( |
| 88 | name='Trachelloworld', version='1.1', |
| 89 | packages=find_packages(exclude=['*.tests*']), |
| 90 | entry_points = """ |
| 91 | [trac.plugins] |
| 92 | helloworld = helloworld |
| 93 | """, |
| 94 | ) |
| 95 | }}} |
| 96 | |
| 97 | You done need to add special [http://peak.telecommunity.com/DevCenter/EggFormats#project-metadata egg metadata] in Trac 0.11. |
| 98 | |
| 99 | == First deployment == |
| 100 | |
| 101 | Now try to build the plugin. Run the command {{{python setup.py bdist_egg}}} in the directory where you created it. If everything went OK you should have a .egg file in ''./dist'' directory. |
| 102 | Copy this ''.egg'' file to ''/[your trac env]/plugins'' directory. |
| 103 | |
| 104 | During development you can run the command {{{python setup.py develop}}} in the directory where you created it. This way you don't need to do it every time you change the code. Check [trac:TracDev/PluginDevelopment] for more deployment options. |
| 105 | |
| 106 | Restart the trac server. If you're using mod_python you have to restart Apache. |
| 107 | |
| 108 | Now you should see ''Hello World'' link at far right in main navigation bar when accessing your site. Click it. |
| 109 | |
| 110 | == Aftermath == |
| 111 | |
| 112 | Now you have successfully created your first egg. You can continue further to learn how to use templates in your plugins, and make its output look like other Trac pages. |
| 113 | |
| 114 | |
| 115 | [[TagIt(khundeen)]] |