Application API

glashammer.application

copyright:2008 Glashammer Developers
license:MIT

The Glashammer Application.

class glashammer.application.GlashammerApplication(setup_func, instance_dir=None, config_name='config.ini', config_factory=<class 'glashammer.utils.config.Configuration'>, url_map=None, view_map=None, template_searchpaths=None, template_filters=None, template_globals=None, template_loaders=None, template_tests=None, template_env_kw=None, request_cls=None, view_finder=None)

WSGI Application

This should usually be made using the make_app() function, which additionally wraps the application in threadlocal session cleaning code.

setup_func
The callable to be called to initialize the application
instance_dir
The directory to use for instance-specific information
config_name
The name of the configuration file in the instance directory
config_factory
The dict-like class that will be used for configuration. This is provided in situations where a file-based configuration file is not suitable, such as AppEngine.
url_map
A werkzeug.routing.Map instance. Additional application rules will be added to this. If this is not provided a new Map will be created by default.
view_map
A map of endpoints to view callables. Views added during setup will be added to this map. If it is not provided, a new dict is created.
template_searchpaths
A list of paths to search for templates. Additional paths added during application setup will be added to this list. If none is provided a new list will be created (default).
template_filters
A mapping of name to callable which are template filters. Additional filters will be added to this map before the template environment is created. If none is provided a new dict is created (default).
template_globals
A mapping of name to variable which will be available in the template’s global namespace. Additional variables added during application setup will be added to this map. If this is not provided a new dict will be created.
request_cls
The base class that is used for requests. Inherit from glashammer.utils.Request to make your own. For more info on request and response wrapper objects, see here.
view_finder
An instance of ViewFinder. Defaults to a new instance.
add_bundle(bundle, *args, **kw)
Add a bundle (a module or other thing) with a setup_app callable.
add_config_var(key, type, default)
Add a configuration variable to the application.
add_data_func(data_func)
Add a data callable to be called after the setup callables.
add_middleware(middleware_factory, *args, **kwargs)
Add a middleware to the application.
add_setup(setup_func, *args, **kw)

Add a setup callable to be called on startup by the application.

setup_func
The callable to be called during application setup.
*args
Arguments that will be passed to the setup function
**kw
Keyword arguments that will be passed to the setup function

This method is used to add pluggable capability to the application. For example, plugin A can load plugin B by importing and adding its setup func like so:

>>> from a import setup_a
>>> app.add_setup(setup_a)

Or in a real example:

>>> from glashammer.bundles.auth import setup_auth
>>> app.add_setup(setup_auth)

Will initialize the auth bundle for the application.

add_shared(name, path)
Add a shared export for name that points to a given path and creates an url rule for shared/<name> that takes a filename parameter.
add_template_filter(name, f)
Add a template filter.
add_template_global(key, value)

Add a template global variable.

key
The variable name.
value
The variable value. Note that you can use a proxy to a local variable by using glashammer.utils.local(‘<variable name>’).
add_template_loader(loader)
Add a Jinja2 Loader to the list of loaders.
add_template_searchpath(path)
Add a directory to the template search path.
add_template_test(name, f)
Add a template filter.
add_url(url, endpoint, view=None, **kw)
Register a url for an endpoint, optionally register a view with it.
add_url_rule(rule, view=None)
Add a url rule with optional view.
add_url_rules(rules)
Add a list of rules to the map.
add_view(endpoint, view)
Register a view for an endpoint.
add_views_controller(endpoint_base, controller)

Add an instance or module which contains functions for a number of views.

‘endpoint_base` The base end point. ‘controller` A module or instance to search for views in.

This method allows you to add multiple views from a source. It is useful in situations where it is desirable to add many views at once for a certain endpoint’s base.

For example:

app.add_url('/foo', 'foo/index')
app.add_url('/foo/add', 'foo/add')

class Controller(object):

    def index(self):
        ...

    def add(self):
        ...

app.add_views_controller('foo', Controller())

Here, the endpoint’s base is ‘foo’. And the actual endpoints will be the attribute names concatenated with the endpoint_base.

connect_event(event, callback, position='after')
Connect an event to the current application.
glashammer.application.declare_app(config_file, setup_func=None, instance_dir=None, **kw)
Create an application instance from a config_file.
glashammer.application.make_app(setup_func, instance_dir=None, **kw)

Create an application instance.

setup_func
The callable used by the application to set itself up. This is the main entry point into the application.
instance_dir
The directory to use for instance-specific information.
kw
See GlashammerApplication