App Handler

The app handler is a special “root” handler which corresponds to the app itself, whereas all other handlers correspond to some “aspect” or “portion” of the app.

It provides the primary interface for obtaining all other handlers, as well as various other utilities; for instance:

  • get general app/node info

  • create the “app” dir (on disk)

  • create database connections

  • get/set settings table records

  • make new UUID

  • convert UTC times to/from local time zone

  • render data of various types for display

The base class and default app handler is wuttjamaican.app.AppHandler.

Regardless which app handler class is in use, any app provider(s) installed will effectively “enhance” the app handler by providing additional methods. For more about that see App Providers.

The config object is responsible for creating the app handler via get_app():

from wuttjamaican.conf import make_config

config = make_config()
app = config.get_app()

Overriding the App Handler

It is expected that many apps will want a more customized app handler. To do this, first create your app handler class e.g. in poser/app.py:

import logging

from wuttjamaican.app import AppHandler

log = logging.getLogger(__name__)

class PoserAppHandler(AppHandler):
    """
    Custom app handler for the Poser system.
    """
    default_widget_handler_spec = "poser.widgets:WidgetHandler"

    def make_session(self, **kwargs):
        """
        Override a method to configure its behavior.
        """
        session = super().make_session(**kwargs)

        session.info["in_poser_land"] = True

        return session

    def hello(self):
        """
        Add a custom method to do something new.
        """
        # nb. appname will be "wutta" in most cases
        print("hello from", self.config.appname)

        with self.short_session() as session:
            in_poser_land = session.info.get("in_poser_land", False)

        print("in poser land?", in_poser_land)

    def get_widget_handler(self):
        """
        Get the configured widget handler.
        """
        if "widgets" not in self.handlers:
            spec = self.config.get(
                f"{self.config.appname}.widgets.handler",
                default=self.default_widget_handler_spec,
            )
            log.debug("widget handler spec is %s", spec)
            factory = self.load_object(spec)
            self.handlers["widgets"] = factory(self.config)
        return self.handlers["widgets"]

Then configure your app handler spec, e.g. in config file:

[wutta]
app.handler = poser.app:PoserAppHandler

See Handler Configuration for other options. In particular, using the config extension approach here usually makes the most sense.

Now you can use your app handler in calling code:

from wuttjamaican.conf import make_config

config = make_config()
app = config.get_app()

app.hello()

widgets = app.get_widget_handler()
widgets.do_something_about_widgets()