Exemplo n.º 1
0
 def find_base(bases: set, implementation: Type):
     found = {b for b in bases if issubclass(implementation, b)}
     if not found:
         raise ConfigurationError("No base defined for %r" %
                                  implementation)
     elif len(found) > 1:
         raise ConfigurationError("More than one base found for %r" %
                                  implementation)
     else:
         return found.pop()
Exemplo n.º 2
0
    def configure(config: List[Egg], subclass: 'Container' = None) -> None:
        """
        Configure haps manually, an alternative
        to :func:`~haps.Container.autodiscover`

        :param config: List of configured Eggs
        :param subclass: Optional Container subclass that should be used
        """

        profiles = Configuration().get_var(PROFILES, tuple)
        assert isinstance(profiles, (list, tuple))
        profiles = tuple(profiles) + (None, )

        seen = set()
        registered = set()

        filtered_config: List[Egg] = []

        for profile in profiles:
            for egg_ in (e for e in config if e.profile == profile):
                ident = (egg_.base_, egg_.qualifier, egg_.profile)
                if ident in seen:
                    raise ConfigurationError("Ambiguous implementation %s" %
                                             repr(egg_.base_))
                dep_ident = (egg_.base_, egg_.qualifier)
                if dep_ident in registered:
                    continue

                filtered_config.append(egg_)

                registered.add(dep_ident)
                seen.add(ident)
        config = filtered_config

        with Container._lock:
            if Container.__configured:
                raise AlreadyConfigured
            if subclass is None:
                subclass = Container

            Container.__subclass = subclass
            Container.__configured = True

            container = Container()
            if not all(isinstance(o, Egg) for o in config):
                raise ConfigurationError('All config items should be the eggs')
            container.config = config

            container.register_scope(INSTANCE_SCOPE, InstanceScope)
            container.register_scope(SINGLETON_SCOPE, SingletonScope)
Exemplo n.º 3
0
    def set(cls, var_name: str, value: Any) -> 'Configuration':
        """
        Set the variable

        :param var_name: Variable name
        :param value: Value of variable
        :return: :class:`~haps.config.Configuration` instance for easy\
                  chaining
        """
        with cls._lock:
            if var_name not in cls().cache:
                cls().cache[var_name] = value
            else:
                raise ConfigurationError(f'Value for {var_name} already set')
        return cls()
Exemplo n.º 4
0
 def egg_dec(obj: Union[FunctionType, type]) -> T:
     if isinstance(obj, FunctionType):
         spec = inspect.signature(obj)
         return_annotation = spec.return_annotation
         if return_annotation is Signature.empty:
             raise ConfigurationError('No return type annotation')
         egg.factories.append(
             Egg(type_=spec.return_annotation,
                 qualifier=qualifier,
                 egg_=obj,
                 base_=None,
                 profile=profile))
         return obj
     elif isinstance(obj, type):
         egg.factories.append(
             Egg(type_=obj,
                 qualifier=qualifier,
                 egg_=obj,
                 base_=None,
                 profile=profile))
         return obj
     else:
         raise AttributeError('Wrong egg obj type')
Exemplo n.º 5
0
    def run(app_class: Type[Application],
            extra_module_paths: List[str] = None, **kwargs: Any) -> None:
        """
        Runner for haps application.

        :param app_class: :class:`~haps.application.Application` type
        :param extra_module_paths: Extra modules list to autodiscover
        :param kwargs: Extra arguments are passed to\
                :func:`~haps.Container.autodiscover`
        """
        module = app_class.__module__
        if (module == '__main__' and
                extra_module_paths is None and
                'module_paths' not in kwargs):
            raise ConfigurationError(
                'You cannot run application from __main__ module without '
                'providing module_paths')

        if module != '__main__':
            module_paths = [app_class.__module__]
        else:
            module_paths = []

        if extra_module_paths is not None:
            module_paths.extend(extra_module_paths)
        autodiscover_kwargs = {
            'module_paths': module_paths,
        }
        autodiscover_kwargs.update(kwargs)

        app_class.configure(Configuration())

        Container.autodiscover(**autodiscover_kwargs)

        app = app_class()
        app.run()
Exemplo n.º 6
0
 def dec(f):
     if var_name in cls().resolvers:
         raise ConfigurationError(
             f'Resolver for {var_name} already registered')
     cls().resolvers[var_name] = f
     return f