8.4 The Application Base Class

#
8.4 The `Application` Base Class

The `Application` class is the base class for all Albatross application objects.

The class inherits from the `ResourceMixin` class to allow all application resources to be loaded once and used for every browser request. The `AppContext` class directs all resource related execution context method here.

Figure: The `Application` class

The methods available in `Application` and the location of their definition are show below.

Method | Mixin
--- | ---
`base_url()` | `Application`
`discard_file_resources(filename)` | `ResourceMixin`
`format_exception()` | `Application`
`get_lookup(name)` | `ResourceMixin`
`get_macro(name)` | `ResourceMixin`
`get_tagclass(name)` | `ResourceMixin`
`handle_exception(ctx, req)` | `Application`
`load_session(ctx)` | `Application`
`merge_request(ctx)` | `Application`
`pickle_sign(text)` | `Application`
`pickle_unsign(text)` | `Application`
`register_lookup(name, lookup)` | `ResourceMixin`
`register_macro(name, macro)` | `ResourceMixin`
`register_tagclasses(*tags)` | `ResourceMixin`
`remove_session(ctx)` | `Application`
`run(req)` | `Application`
`save_session(ctx)` | `Application`
`template_traceback(tb)` | `Application`
`validate_request(ctx)` | `Application`

The `Application` class introduces a number of new methods.

`__init__`( | base_url)

  When you inherit from the `Application` class you must call this constructor.

  The base_url argument is used as the base for URLs produced by the `<al-a>` and `<al-form>` tags.

`base_url`( | )

  Returns the base_url argument which was passed to the constructor.

`run`( | req)

  Implements the standard application run sequence as described on page in section 4.1. The browser request passed as the req argument is attached to the execution context as soon as the context has been created.

  If an exception is caught then the `handle_exception()` method is called passing the req argument.

`format_exception`( | )

  Retrieves the current exception from `sys.exc_info()` then formats and returns the standard Python traceback and a template interpreter traceback.

`handle_exception`( | ctx, req)

  This implements the default exception handling for applications. The req argument is the browser request which was passed to the `run()` method.

  The method calls the `format_exception()` method to construct a standard Python traceback and a template traceback. A temporary execution context is then created, the Python traceback is saved in the `locals.python_exc` value, and the template traceback in the `locals.html_exc` value.

  The method then tries to load the `'traceback.html'` template file and execute it with the temporary execution context. This gives you the ability to control the presentation and reporting of exceptions.

  If any exceptions are raised during the execution of `'traceback.html'` the method writes both formatted exceptions as a `<pre>` formatted browser response.

`template_traceback`( | tb)

  Generates a template interpreter traceback from the Python stack trace in the tb argument.

`load_session`( | ctx)

  Calls the `load_session()` method of the execution context in the ctx argument.

`save_session`( | ctx)

  Calls the `save_session()` method of the execution context in the ctx argument.

`remove_session`( | ctx)

  Calls the `remove_session()` method of the execution context in the ctx argument.

`validate_request`( | ctx)

  Returns `TRUE`.

  You should override this method in your application object if you need to validate browser requests before processing them.

`pickle_sign`( | text)

  Returns an empty string to prevent insecure pickles being sent to the browser. This is overridden in the `PickleSignMixin` class.

`pickle_unsign`( | text)

  Returns an empty string to prevent insecure pickles being accepted from the browser. This is overridden in the `PickleSignMixin` class.

`merge_request`( | ctx)

  Calls the `merge_request()` method of the execution context.