Coverage for tw2.core.middleware : 95%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
''' ToscaWidgets Configuration Set
`translator` The translator function to use. (default: no-op)
`default_engine` The main template engine in use by the application. Widgets with no parent will display correctly inside this template engine. Other engines may require passing displays_on to :meth:`Widget.display`. (default:string)
`inject_resoures` Whether to inject resource links in output pages. (default: True)
`inject_resources_location` A location where the resources should be injected. (default: head)
`serve_resources` Whether to serve static resources. (default: True)
`res_prefix` The prefix under which static resources are served. This must start and end with a slash. (default: /resources/)
`res_max_age` The maximum time a cache can hold the resource. This is used to generate a Cache-control header. (default: 3600)
`serve_controllers` Whether to serve controller methods on widgets. (default: True)
`controller_prefix` The prefix under which controllers are served. This must start and end with a slash. (default: /controllers/)
`bufsize` Buffer size used by static resource server. (default: 4096)
`params_as_vars` Whether to present parameters as variables in widget templates. This is the behaviour from ToscaWidgets 0.9. (default: False)
`debug` Whether the app is running in development or production mode. (default: True)
`validator_msgs` A dictionary that maps validation message names to messages. This lets you override validation messages on a global basis. (default: {})
`encoding` The encoding to decode when performing validation (default: utf-8)
`auto_reload_templates` Whether to automatically reload changed templates. Set this to False in production for efficiency. If this is None, it takes the same value as debug. (default: None)
`preferred_rendering_engines` List of rendering engines in order of preference. (default: ['mako','genshi','jinja'])
`strict_engine_selection` If set to true, TW2 will only select rendering engines from within your preferred_rendering_engines, otherwise, it will try the default list if it does not find a template within your preferred list. (default: True)
`rendering_engine_lookup` A dictionary of file extensions you expect to use for each type of template engine. (default: { 'mako':['mak', 'mako'], 'genshi':['genshi', 'html'], 'jinja':['jinja', 'html'], })
`script_name` A name to prepend to the url for all resource links (different from res_prefix, as it may be shared across and entire wsgi app. (default: '') '''
'mako': ['mak', 'mako'], 'genshi': ['genshi', 'html'], 'genshi_abs': ['genshi', 'html'], # just for backwards compatibility with tw2 2.0.0 'jinja':['jinja', 'html'], 'chameleon': ['pt'] }
# Set boolean properties 'inject_resources', 'serve_resources', 'serve_controllers', 'params_as_vars', 'strict_engine_selection', 'debug', )
# Set integer properties
"""ToscaWidgets middleware
This performs three tasks: * Clear request-local storage before and after each request. At the start of a request, a reference to the middleware instance is stored in request-local storage. * Proxy resource requests to ResourcesApp * Inject resources """
# Here to avoid circular import
# Load up controllers that wanted to be registered before we were ready
# Load up resources that wanted to be registered before we were ready self.resources.register(modname, filename, whole_dir)
# Future resource registrations should know to just plug themselves into # me right away (instead of being queued).
path.startswith(self.config.res_prefix): else: path.startswith(self.config.controller_prefix): else: else:
self.config.inject_resources and 'html' in ct and not isinstance(resp.app_iter, types.GeneratorType) ) resp.body.decode(resp.charset), ).encode(resp.charset) else: body = self._resources_module.inject_resources( resp.body, )
resp.unicode_body = body else:
""" """
""" Return the path against which a given widget is mounted or None if it is not registered. """
return None
else:
""" API function for registering resources *for serving*.
This should not be confused with resource registration for *injection*. A resource must be registered for serving for it to be also registered for injection.
If the middleware is available, the resource is directly registered with the ResourcesApp.
If the middleware is not available, the resource is stored in the request_local dict. When the middleware is later initialized, those waiting registrations are processed. """
else: rl['queued_resources'] = rl.get('queued_resources', []) + [ (modname, filename, whole_dir) ] log.debug("No middleware in place. Queued %r->%r(%r) registration." % (modname, filename, whole_dir))
""" API function for registering widget controllers.
If the middleware is available, the widget is directly registered with the ControllersApp.
If the middleware is not available, the widget is stored in the request_local dict. When the middleware is later initialized, those waiting registrations are processed. """
else: rl.get('queued_controllers', []) + [(widget, path)] (path, widget))
return make_middleware(app=None, config=config, **kw) |