def base_config(): Configuration.set('var_a', 'a') Configuration.set('var_b', 3) @Configuration.resolver('var_c') def _() -> str: return 'c'
def env_config(monkeypatch): monkeypatch.setenv('HAPS_var_a', 'a') monkeypatch.setenv('VAR_B', 'b') Configuration.env_resolver('var_a') Configuration.env_resolver('var_b', 'VAR_B') Configuration.env_resolver('var_c', default='c') Configuration.env_resolver('var_d', default=lambda: 'd')
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)
def test_dependencies_with_profiles(some_class, profiles, expected): class NewClass(some_class): pass class NewClass2(some_class): pass class NewClass3(some_class): pass Configuration().set('haps.profiles', profiles) haps.Container.configure([ haps.Egg(some_class, NewClass, None, NewClass), haps.Egg(some_class, NewClass2, None, NewClass2, 'test'), haps.Egg(some_class, NewClass3, None, NewClass3, 'prod') ]) class AnotherClass: some_instance: some_class = haps.Inject() some_instance = AnotherClass() assert type(some_instance.some_instance).__name__ == expected
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()
from haps import PROFILES, Container, Inject, inject from haps.config import Configuration from samples.autodiscover.services.bases import IHeater, IPump class CoffeeMaker: heater: IHeater = Inject() @inject def __init__(self, pump: IPump): self.pump = pump def make_coffee(self): return "heater: %r\npump: %r" % (self.heater, self.pump) if __name__ == '__main__': Configuration().set(PROFILES, ('test',)) Container.autodiscover(['samples.autodiscover.services']) print(CoffeeMaker().make_coffee())
from haps import inject from haps.application import Application, ApplicationRunner from haps.config import Config, Configuration from samples.autodiscover.services.bases import IHeater @Configuration.resolver('heat_count') def _(): return 5 Configuration.env_resolver('config_var') Configuration.set('another_var', 10) class MyApp(Application): config_var: str = Config() count: int = Config('heat_count') another_var: int = Config() @inject def __init__(self, heater: IHeater): self.heater = heater def run(self): for _ in range(self.count): self.heater.heat() print(self.config_var, self.another_var) if __name__ == '__main__':
def test_configuration_chaining(): Configuration().set("a", 1).set("b", 2).env_resolver("d").set("c", 3) assert Configuration().get_var('a') == 1 assert Configuration().get_var('b') == 2 assert Configuration().get_var('c') == 3 assert Configuration().get_var('d', None) is None
def test_env_config(env_config): assert Configuration().get_var('var_a') == 'a' assert Configuration().get_var('var_b') == 'b' assert Configuration().get_var('var_c') == 'c' assert Configuration().get_var('var_d') == 'd'
def test_base_config(base_config): assert Configuration().get_var('var_a') == 'a' assert Configuration().get_var('var_b') == 3 assert Configuration().get_var('var_c') == 'c'