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, Type

from jivago.config.production_jivago_context import ProductionJivagoContext
from jivago.jivago_application import JivagoApplication
from jivago.wsgi.filter.filter import Filter
from jivago.wsgi.routing.router import Router


class MyApplicationContext(ProductionJivagoContext):

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

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

    def get_filters(self, path: str) -> List[Type[Filter]]:
        return super().get_filters(path)

    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(self) -> Router:
        return super().create_router()

    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_filters()
This method returns a list of Filters which should be applied to a specific request. It is called once for every request.
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()
This method is used to configure the Router object which is used to resolve requests.
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.lang.annotations import Inject
from jivago.lang.registry import Component


@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"])