Application Configuration

Configuration is done using a context class, which defines various methods which can be overridden. The recommended way of defining an application context is by inheriting from either ProductionJivagoContext or DebugJivagoContext, and overriding specific methods.

from typing import List

from jivago.config.production_jivago_context import ProductionJivagoContext
from jivago.config.router.router_builder import RouterBuilder
from jivago.event.event_bus import EventBus
from jivago.jivago_application import JivagoApplication
from jivago.lang.registry import Registry


class MyApplicationContext(ProductionJivagoContext):

    def __init__(self, root_package: "Module", registry: Registry, banner: bool = True):
        super().__init__(root_package, registry, banner)

    def configure_service_locator(self):
        super().configure_service_locator()

    def scopes(self) -> List[type]:
        return super().scopes()

    def get_views_folder_path(self) -> str:
        return super().get_views_folder_path()

    def get_config_file_locations(self) -> List[str]:
        return super().get_config_file_locations()

    def create_router_config(self) -> RouterBuilder:
        return super().create_router_config()

    def get_default_filters(self):
        return super().get_default_filters()

    def create_event_bus(self) -> EventBus:
        return super().create_event_bus()

    def get_banner(self) -> List[str]:
        return super().get_banner()


app = JivagoApplication(my_package, context=MyApplicationContext)

Configuration methods

configure_service_locator()

This method can be used to manually bind classes to the internal ServiceLocator. See Dependency Injection for more details.

scopes()

This method defines component scopes for the ServiceLocator which determine when to instantiate new components. By default, only the Singleton exists.

get_views_folder_path()

This method defines the folder in which template files are stored for RenderedView responses. Defaults to the views submodule of the root package.

get_config_file_locations()

Defines a list of files which should be tried when importing the application properties. The ApplicationProperties is creating using the first existent file in this rule. Defaults to ["application.yml", "application.json", "properties.yml", "properties.json"].

create_router_config()

This method is used to configure the Router object which is used to resolve requests. See Router Configuration for details.

get_default_filters()

Used only on subclasses of ProductionJivagoFilter. This method is called from create_router_config to define the default FilteringRule.

create_event_bus()

Used to register event handlers.

get_banner()

Defines the ASCII-art banner which is printed in the console at every startup.

ApplicationProperties and SystemEnvironmentProperties

Both the ApplicationProperties and SystemEnvironmentProperties dictionaries can be injected into a component class, thus providing access namely to the contents of the application config file, and to the environment variables. For instance, for an application.yml file placed in the working directory, an appropriate ApplicationProperties object is created.

application.yml

my_property: "foobar"

my_component.py

from jivago.config.properties.application_properties import ApplicationProperties
from jivago.inject.annotation import Component
from jivago.lang.annotations import Inject


@Component
class MyComponent(object):

    @Inject
    def __init__(self, application_properties: ApplicationProperties):
        self.application_properties = application_properties

    def do_something(self):
        print(self.application_properties["my_property"])