Changes between Initial Version and Version 1 of EggCookingTutorial/BasicEggCooking


Ignore:
Timestamp:
Jul 28, 2005, 4:50:21 PM (19 years ago)
Author:
Jani Tiainen
Comment:

Initial article text

Legend:

Unmodified
Added
Removed
Modified
  • EggCookingTutorial/BasicEggCooking

    v1 v1  
     1= Egg cooking =
     2
     3Since 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
     5This 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
     7You 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
     11First you need ''setuptools''. For instructions and files see [http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install EasyInstall] page.
     12
     13Then 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
     17To develop a plugin you need to create few directories to keep things together.
     18
     19So 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
     28First 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
     30So create ''helloworld.py'' in ''./helloworld-plugin/helloworld/'':
     31{{{
     32# Helloworld plugin
     33
     34from trac.core import *
     35from trac.web.chrome import INavigationContributor
     36from trac.web.main import IRequestHandler
     37from trac.util import escape
     38
     39class 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
     64Since 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
     67from helloworld import *
     68}}}
     69
     70== Make it as an egg ==
     71
     72Now 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{{{
     74from setuptools import setup
     75
     76PACKAGE = 'TracHelloworld'
     77VERSION = '0.1'
     78
     79setup(name=PACKAGE, version=VERSION, packages=['helloworld'])
     80}}}
     81
     82To make egg loadable in Trac we need to create one file more. in ''./helloworld-plugin/!TracHelloworld.egg-info/'' create file ''trac_plugin.txt'':
     83{{{
     84helloworld
     85}}}
     86
     87== First deployment ==
     88
     89Now 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
     91Copy this ''.egg'' file to ''/[your trac env]/plugins'' directory. If you're using mod_python you have to restart Apache.
     92
     93Now you should see ''Hello'' link at far right in main navigation bar when accessing your site. Click it.
     94
     95== Aftermath ==
     96
     97Now 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