Components

Keg components follow the paradigm of flask extensions, and provide some defaults for the purpose of setting up model/view structure. Using components, a project may be broken down into logical blocks, each having their own entities, blueprints, templates, tests, etc.

  • Components need to be registered in config at KEG_REGISTERED_COMPONENTS
    • The path given here should be a full dotted path to the top level of the component
      • e.g. my_app.components.blog
    • At the top level of the component, __component__ must be defined as an instance of KegComponent
      • Depending on the needs of the component, model and view discovery may be driven by the subclasses of KegComponent that have path defaults
      • Examples:
        • __component__ = KegModelComponent(‘blog’)
        • __component__ = KegViewComponent(‘blog’)
        • __component__ = KegModelViewComponent(‘blog’)
  • Component discovery
    • A component will attempt to load model and blueprints on app init
    • The default paths relative to the component may be modified or extended on the component’s definition
    • Default model path in “model” components: .model.entities
      • Override via the component’s db_visit_modules list of relative import paths
    • Default blueprint path for “view” components: .views.component_bp
      • Use the create_named_blueprint or create_blueprint helpers on the component’s __component__ to create blueprints with configured template folders
      • Override via the component’s load_blueprints list
        • List elements are a tuple of the relative import path and the name of the blueprint attribute
      • Components have their own template stores, in a templates folder
        • Override the component’s template path via the template_folder attribute
    • Paths may also be supplied to the constructor
      • e.g. __component__ = KegComponent(‘blog’, db_visit_modules=(‘.somewhere.else’, ))

Documentation

class keg.component.KegComponent(name, app=None, db_visit_modules=None, load_blueprints=None, template_folder=None, parent_path=None)[source]

Keg components follow the paradigm of flask extensions, and provide some defaults for the purpose of setting up model/view structure. Using components, a project may be broken down into logical blocks, each having their own entities, blueprints, templates, tests, etc.

Setup involves: - KEG_REGISTERED_COMPONENTS config setting: assumed to be an iterable of importable dotted paths - __component__: at the top level of each dotted path, this attribute should point to an instance of KegComponent. E.g. __component__ = KegComponent(‘widgets’)

By default, components will load entities from db_visit_modules into metadata and register any blueprints specified by load_blueprints.

Blueprints can be created with the helper methods create_named_blueprint or create_blueprint in order to have a configured template folder relative to the blueprint path.

Use KegModelComponent, KegViewComponent, or KegModelViewComponent for some helpful defaults for model/blueprint discovery.

db_visit_modules: an iterable of dotted paths (e.g. .mycomponent.entities,
app.component.extraentities) where Keg can find the entities for this component to load them into the metadata.

Note

Normally this is not explicitly required but can be useful in cases where imports won’t reach that file.

Note

This can accept relative dotted paths (starts with .) and it will prepend the component python package determined by Keg when instantiating the component. You can also pass absolute dotted paths and no alterations will be performed.

load_blueprints: an iterable of tuples, each having a dotted path (e.g. .mycomponent.views,
app.component.extraviews) and the blueprint attribute name to load and register on the app. E.g. ((‘.views’, ‘component_bp’), )

Note

This can accept relative dotted paths (starts with .) and it will prepend the component python package determined by Keg when instantiating the component. You can also pass absolute dotted paths and no alterations will be performed.

template_folder: string to be passed for template config to blueprints created via the component

create_blueprint(*args, **kwargs)[source]

Make a flask blueprint having a template folder configured.

Generally, args and kwargs provided will be passed to the blueprint constructor, with the following exceptions:

  • template_folder kwarg defaults to the component’s template_folder if not provided
  • blueprint_cls kwarg may be used to specify an alternative to flask.Blueprint
create_named_blueprint(*args, **kwargs)[source]
db_visit_modules = ()
init_app(app, parent_path=None)[source]
init_blueprints(app, parent_path)[source]
init_config(app)[source]
init_db(parent_path)[source]
load_blueprints = ()
template_folder = 'templates'