Beispiel #1
0
def gather_constructors(service_methods, construct):
    """Loads the construct methods that are defined."""
    try:
        _methods = service_methods
    except KeyError:
        pass
    else:
        for meth in _methods:
            try:
                func = meth['function']
            except KeyError:
                pass
            else:
                construct.append(util.importer(func))
def init_services(service_definitions,
                  service_context,
                  state_db,
                  client_authn_factory=None):
    """
    Initiates a set of services

    :param service_definitions: A dictionary containing service definitions
    :param service_context: A reference to the service context, this is the same
        for all service instances.
    :param state_db: A reference to the state database. Shared by all the
        services.
    :param client_authn_factory: A list of methods the services can use to
        authenticate the client to a service.
    :return: A dictionary, with service name as key and the service instance as
        value.
    """
    service = {}
    for service_name, service_configuration in service_definitions.items():
        try:
            kwargs = service_configuration['kwargs']
        except KeyError:
            kwargs = {}

        kwargs.update({
            'service_context': service_context,
            'state_db': state_db,
            'client_authn_factory': client_authn_factory
        })

        if isinstance(service_configuration['class'], str):
            _srv = util.importer(service_configuration['class'])(**kwargs)
        else:
            _srv = service_configuration['class'](**kwargs)

        if 'post_functions' in service_configuration:
            gather_constructors(service_configuration['post_functions'],
                                _srv.post_construct)
        if 'pre_functions' in service_configuration:
            gather_constructors(service_configuration['pre_functions'],
                                _srv.pre_construct)

        try:
            service[_srv.service_name] = _srv
        except AttributeError:
            raise ValueError("Could not load '{}'".format(service_name))

    return service
Beispiel #3
0
def do_add_ons(add_ons, services):
    for key, spec in add_ons.items():
        _func = importer(spec['function'])
        _func(services, **spec['kwargs'])