- TurboGears Tutorial
- TurboGears - Home
- TurboGears - Overview
- TurboGears - Environment
- TurboGears - First Program
- TurboGears - Dependencies
- TurboGears - Serving Templates
- TurboGears - HTTP Methods
- Genshi Template Language
- TurboGears - Includes
- TurboGears - JSON Rendering
- TurboGears - URL Hierarchy
- TurboGears - Toscawidgets Forms
- TurboGears - Validation
- TurboGears - Flash Messages
- TurboGears - Cookies and Sessions
- TurboGears - Caching
- TurboGears - Sqlalchemy
- TurboGears - Creating Models
- TurboGears - Crud Operations
- TurboGears - DataGrid
- TurboGears - Pagination
- TurboGears - Admin Access
- Authorization & Authentication
- TurboGears - Using MongoDB
- TurboGears - Scaffolding
- TurboGears - Hooks
- TurboGears - Writing Extensions
- TurboGears - Pluggable Applications
- TurboGears - Restful Applications
- TurboGears - Deployment
- TurboGears Useful Resources
- TurboGears - Quick Guide
- TurboGears - Useful Resources
- TurboGears - Discussion
TurboGears - Writing Extensions
TurboGears extensions are identified by tgext.* package. A Gearbox toolkit provides tgext command to create a sample extension. For example −
gearbox tgext -n myextension
Other optional parameters for this command are −
--author − name of package author.
--email − email of package author.
--licence − licence used for package. Default is MIT.
--description − Description of package.
--keywords − Package keywords (default: turbogears2.extension).
This will create a tgext.myextension directory, which has a simple sample extension inside.
Run the setup.py inside the directory −
Python setup.py install
The _init_.py file inside tgext/myextension folder contains −
Plugme function − This is the entry point of extension.
SetupExtension class − extension initialization takes place here.
On_startup function − inside the class is a hook registered on __call__ function inside class.
Brief version of the tgext\myextension\__init__.py.
from tg import config from tg import hooks from tg.configuration import milestones import logging log = logging.getLogger('tgext.myextension') def plugme(configurator, options = None): if options is None: options = {} log.info('Setting up tgext.myextension extension...') milestones.config_ready.register(SetupExtension(configurator)) return dict(appid='tgext.myextension') class SetupExtension(object): def __init__(self, configurator): self.configurator = configurator def __call__(self): log.info('>>> Public files path is %s' % config['paths']['static_files']) hooks.register('startup', self.on_startup) def echo_wrapper_factory(handler, config): def echo_wrapper(controller, environ, context): log.info('Serving: %s' % context.request.path) return handler(controller, environ, context) return echo_wrapper self.configurator.register_wrapper(echo_wrapper_factory) def on_startup(self): log.info('+ Application Running!')
Once the extension is installed, turn it on by making the following additions in the application's app_cfg.py configuration file.
from tgext.myextension import plugme plugme(base_config)
If we launch the server using a gearbox server command, the notification of a newly registered extension can be viewed on the console by the following −
14:29:13,250 INFO [tgext.myextension] Setting up tgext.myextension extension... 14:29:13,453 INFO [tgext.myextension] >>> Public files path is c:\tghello\hello\hello\public 14:29:13,453 INFO [tgext.myextension] + Application Running! Starting Standard HTTP server on http://127.0.0.1:8080