def register(self, hook_name, func, controller=None): """Registers a TurboGears hook. Given an hook name and a function it registers the provided function for that role. For a complete list of hooks provided by default have a look at :ref:`hooks_and_events`. It permits to register hooks both application wide or for specific controllers:: tg.hooks.register('before_render', hook_func, controller=RootController.index) tg.hooks.register('startup', startup_function) """ if hook_name in ('startup', 'shutdown') and controller is not None: raise TGConfigError('Startup and Shutdown hooks cannot be registered on controllers') if hook_name == 'controller_wrapper': raise TGConfigError('tg.hooks.wrap_controller must be used to register wrappers') if controller is None: config_ready.register(_ApplicationHookRegistration(hook_name, func)) else: controller = default_im_func(controller) renderers_ready.register(_ControllerHookRegistration(controller, hook_name, func))
def register(self, hook_name, func, controller=None): """Registers a TurboGears hook. Given an hook name and a function it registers the provided function for that role. For a complete list of hooks provided by default have a look at :ref:`hooks_and_events`. It permits to register hooks both application wide or for specific controllers:: tg.hooks.register('before_render', hook_func, controller=RootController.index) tg.hooks.register('startup', startup_function) """ if hook_name in ('startup', 'shutdown') and controller is not None: raise TGConfigError( 'Startup and Shutdown hooks cannot be registered on controllers' ) if hook_name == 'controller_wrapper': raise TGConfigError( 'tg.hooks.wrap_controller must be used to register wrappers') if controller is None: config_ready.register(_ApplicationHookRegistration( hook_name, func)) else: controller = default_im_func(controller) renderers_ready.register( _ControllerHookRegistration(controller, hook_name, func))
def wrap_controller(self, func, controller=None): """Registers a TurboGears controller wrapper. Controller Wrappers are much like a **decorator** applied to every controller. They receive :class:`tg.configuration.AppConfig` instance as an argument and the next handler in chain and are expected to return a new handler that performs whatever it requires and then calls the next handler. A simple example for a controller wrapper is a simple logging wrapper:: def controller_wrapper(app_config, caller): def call(*args, **kw): try: print 'Before handler!' return caller(*args, **kw) finally: print 'After Handler!' return call tg.hooks.wrap_controller(controller_wrapper) It is also possible to register wrappers for a specific controller:: tg.hooks.wrap_controller(controller_wrapper, controller=RootController.index) """ if config_ready.reached: raise TGConfigError('Controller wrappers can be registered only at ' 'configuration time.') if controller is None: config_ready.register(_ApplicationHookRegistration('controller_wrapper', func)) else: controller = default_im_func(controller) registration = _ControllerHookRegistration(controller, 'controller_wrapper', func) config_ready.register(registration)