Skip to content

Commit

Permalink
Allow dynamically registered units in AuxUnitRegister (#92)
Browse files Browse the repository at this point in the history
* fix: simplify AuxUnitRegister code paths

* Bump version to 0.10.5a0
  • Loading branch information
jvansanten authored Feb 5, 2025
1 parent c3cc921 commit 9a4e8c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions ampel/base/AuxUnitRegister.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ def new_unit(cls, model: UnitModel, *, sub_type: None = ..., **kwargs) -> AmpelU
def new_unit(cls, model: UnitModel, *, sub_type: None | type[T] = None, **kwargs) -> T | AmpelUnit:
""" :raises: ValueError is model.config is not of type None | dict """

if model.unit in cls._dyn:
Klass = cls._dyn[model.unit]
else:
Klass = cls.get_aux_class(klass=model.unit, sub_type=sub_type)
Klass = cls.get_aux_class(klass=model.unit, sub_type=sub_type)

if model.config:
if isinstance(model.config, dict):
return Klass(**(model.config | kwargs))
raise ValueError("Auxiliary units cannot use config aliases")
init_kwargs = model.config | kwargs
else:
raise ValueError("Auxiliary units cannot use config aliases")
else:
init_kwargs = kwargs

unit = Klass(**kwargs)
unit = Klass(**init_kwargs)
if hasattr(unit, "post_init"):
unit.post_init()

Expand All @@ -75,7 +75,12 @@ def get_aux_class(cls, klass: str, *, sub_type: None = ...) -> type[AmpelUnit]:
def get_aux_class(cls, klass: str, *, sub_type: None | type[T] = None) -> type[T | AmpelUnit]:
""" :raises: ValueError if unit is unknown """

if klass not in cls._defs:
if klass in cls._dyn:
ret = cls._dyn[klass]
elif klass in cls._defs:
fqn = cls._defs[klass]['fqn']
ret = getattr(import_module(fqn), klass)
else:
if not cls._defs:
raise ValueError(
f"Unknown auxiliary unit {klass}:"
Expand All @@ -86,9 +91,6 @@ def get_aux_class(cls, klass: str, *, sub_type: None | type[T] = None) -> type[T
f"- check your ampel conf to see if the unit is properly registered"
)

fqn = cls._defs[klass]['fqn']
ret = getattr(import_module(fqn), klass)

if sub_type:
check_class(ret, sub_type)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ampel-interface"
version = "0.10.4.post2"
version = "0.10.5a0"
description = "Base classes for the Ampel analysis platform"
authors = ["Valery Brinnel"]
maintainers = ["Jakob van Santen <[email protected]>"]
Expand Down

0 comments on commit 9a4e8c8

Please sign in to comment.