Files
brewpi/components/factory.py
T
jensandClaude Sonnet 4.6 f7355270c8 Extract shared factory dispatch into ComponentFactory base class
All four name-dispatch factories repeated the same if/elif lazy-import
pattern. Replace with a ComponentFactory base class holding a callable
registry and a _lazy() helper for the common import-and-construct case.
PlantFactory is an orchestrator, not a dispatcher, and is left as-is.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tqxrk8uj4M3w3d3eXm3xK8
2026-06-25 20:44:43 +02:00

20 lines
562 B
Python

import importlib
def _lazy(module, classname):
"""Return a builder that lazily imports module.classname on first call."""
def build(*args, **kwargs):
return getattr(importlib.import_module(module), classname)(*args, **kwargs)
return build
class ComponentFactory:
_registry = {}
@classmethod
def create(cls, name, *args, **kwargs):
for key, builder in cls._registry.items():
if key in name:
return builder(*args, **kwargs)
raise ValueError(f"{cls.__name__}: unknown name {name!r}")