API¶
Container¶
Container is a heart of haps. For now, its implemented as a singleton that can only be used after one-time configuration.
from haps import Container
Container.autodiscover(['my.package']) # configuration, once in the app lifetime
Container().some_method() # Call method on the instance
That means, you can create instances of classes that use injections, only after haps is properly configured.
-
classmethod
Container.
autodiscover
(module_paths: List[str], subclass: Optional[haps.container.Container] = None) → None[source]¶ Load all modules automatically and find bases and eggs.
Parameters: - module_paths – List of paths that should be discovered
- subclass – Optional Container subclass that should be used
-
static
Container.
configure
(config: List[haps.container.Egg], subclass: Optional[haps.container.Container] = None) → None[source]¶ Configure haps manually, an alternative to
autodiscover()
Parameters: - config – List of configured Eggs
- subclass – Optional Container subclass that should be used
Egg¶
-
class
haps.
Egg
(base_: Optional[Type], type_: Type, qualifier: Optional[str], egg_: Callable, profile: str = None)[source]¶ Configuration primitive. Can be used to configure haps manually.
-
Egg.
__init__
(base_: Optional[Type], type_: Type, qualifier: Optional[str], egg_: Callable, profile: str = None) → None[source]¶ Parameters: - base – base of dependency, used to retrieve object
- type – type of dependency (for functions it’s a return type)
- qualifier – extra qualifier for dependency. Can be used to register more than one type for one base.
- egg – any callable that returns an instance of dependency, can be a class or a function
- profile – dependency profile name
Injection¶
-
class
haps.
Inject
(qualifier: str = None)[source]¶ A descriptor for injecting dependencies as properties
class SomeClass: my_dep: DepType = Inject()
Important
Dependency is injected (created/fetched) at the moment of accessing the attribute, not at the moment of instance creation. So, even if you create an instance of SomeClass, the instance of DepType may never be created.
-
haps.
inject
(fun: Callable) → Callable[source]¶ A decorator for injection dependencies into functions/methods, based on their type annotations.
class SomeClass: @inject def __init__(self, my_dep: DepType) -> None: self.my_dep = my_dep
Important
On the opposite to
Inject
, dependency is injected at the moment of method invocation. In case of decorating __init__, dependency is injected when SomeClass instance is created.Parameters: fun – callable with annotated parameters Returns: decorated callable
Dependencies¶
-
haps.
base
(cls: T) → T[source]¶ A class decorator that marks class as a base type.
Parameters: cls – Some base type Returns: Not modified cls
-
haps.
egg
(qualifier: Union[str, Type] = '', profile: str = None)[source]¶ A function that returns a decorator (or acts like a decorator) that marks class or function as a source of base.
If a class is decorated, it should inherit from base type.
If a function is decorated, it declared return type should inherit from some base type, or it should be the base type.
@egg class DepImpl(DepType): pass @egg(profile='test') class TestDepImpl(DepType): pass @egg(qualifier='special_dep') def dep_factory() -> DepType: return SomeDepImpl()
Parameters: - qualifier – extra qualifier for dependency. Can be used to register more than one type for one base. If non-string argument is passed, it’ll act like a decorator.
- profile – An optional profile within this dependency should be used
Returns: decorator
Configuration¶
-
class
haps.config.
Configuration
[source]¶ Configuration container, a simple object to manage application config variables. Variables can be set manually, from the environment, or resolved via custom function.
-
Configuration.
get_var
(var_name: str, default: Optional[Any] = <object object>) → Any[source]¶ Get a config variable. If a variable is not set, a resolver is not set, and no default is given
UnknownConfigVariable
is raised.Parameters: - var_name – Name of variable
- default – Default value
Returns: Value of config variable
-
classmethod
Configuration.
resolver
(var_name: str) → function[source]¶ Variable resolver decorator. Function or method decorated with it is used to resolve the config variable.
Note
Variable is resolved only once. Next gets are returned from the cache.
Parameters: var_name – Variable name Returns: Function decorator
-
classmethod
Configuration.
env_resolver
(var_name: str, env_name: str = None, default: Any = <object object>) → haps.config.Configuration[source]¶ Method for configuring environment resolver.
Parameters: - var_name – Variable name
- env_name – An optional environment variable name. If not set haps looks for HAPS_var_name
- default – Default value for variable. If it’s a callable, it is called before return. If not provided
UnknownConfigVariable
is raised
Returns: Configuration
instance for easy chaining
-
classmethod
Configuration.
set
(var_name: str, value: Any) → haps.config.Configuration[source]¶ Set the variable
Parameters: - var_name – Variable name
- value – Value of variable
Returns: Configuration
instance for easy chaining
-
class
haps.config.
Config
(var_name: str = None, default=<object object>)[source]¶ Descriptor providing config variables as a class properties.
class SomeClass: my_var: VarType = Config() custom_property_name: VarType = Config('var_name')
-
Config.
__init__
(var_name: str = None, default=<object object>) → None[source]¶ Parameters: - var_name – An optional variable name. If not set the property name is used.
- default – Default value for variable. If it’s a callable, it is called before return. If not provided
UnknownConfigVariable
is raised