ReMe/reme_cli/component/component_registry.py
jinli.yl 5fc57e7f4e feat(core): refactor application architecture with new component base classes
- Add BaseClient, BaseFileStore, BaseFileWatcher, BaseJob, BaseService, and BaseStep classes
- Move component initialization logic from ApplicationContext to Application class
- Add logo printing and logging initialization in Application startup
- Create client module with base client implementation
- Add file store base class with embedding resolution and validation
- Implement file watcher base class with watchfiles integration
- Add job base class for sequential step execution orchestration
- Create service base class for job exposure mechanisms
- Refactor BaseStep with LLM workflow execution capabilities
- Add case converter utility for naming convention transformations
- Update import structure and module organization
- Add proper type hints and docstrings across all components
- Implement component registry integration for dynamic loading
- Add error handling for missing backend configurations
2026-04-14 16:43:36 +08:00

78 lines
2.8 KiB
Python

"""
Component registry module.
Provides a global registry for managing component class registration and lookup.
Supports two registration methods:
1. Direct registration: R.register(MyClass, "name")
2. Decorator registration: @R.register("name")
"""
from typing import Callable, TypeVar, cast
from .base_component import BaseComponent
from ..enumeration import ComponentEnum
from ..utils import get_logger
T = TypeVar("T", bound=BaseComponent)
class ComponentRegistry:
"""Registry for managing component class registration and lookup."""
def __init__(self) -> None:
self._registry: dict[ComponentEnum, dict[str, type[BaseComponent]]] = {}
self.logger = get_logger()
def _do_register(self, cls: type[T], name: str) -> type[T]:
"""Register a component class with the given name."""
if not hasattr(cls, "component_type"):
raise TypeError(f"{cls.__name__} must have 'component_type' attribute")
if not name:
raise ValueError("Component name cannot be empty")
component_type = cls.component_type
if name in self._registry[component_type]:
self.logger.warning(f"Component '{name}' already registered for {component_type}, overwriting")
self._registry[component_type][name] = cls
return cls
def register(
self,
cls_or_name: type[T] | str,
name: str | None = None,
) -> Callable[[type[T]], type[T]] | type[T]:
"""Register a component class. Supports direct and decorator modes."""
# Direct registration: R.register(MyClass, "name")
if isinstance(cls_or_name, type):
return self._do_register(cast(type[T], cls_or_name), name or cls_or_name.__name__)
# Decorator mode: @R.register("name")
decorator_name = cls_or_name
def decorator(decorated_cls: type[T]) -> type[T]:
return self._do_register(decorated_cls, decorator_name or decorated_cls.__name__)
return decorator
def get(self, component_type: ComponentEnum, name: str) -> type[BaseComponent] | None:
"""Get a registered component class by type and name."""
return self._registry.get(component_type, {}).get(name)
def get_all(self, component_type: ComponentEnum) -> dict[str, type[BaseComponent]]:
"""Get all registered components of a given type."""
return dict(self._registry.get(component_type, {}))
def unregister(self, component_type: ComponentEnum, name: str) -> bool:
"""Remove a component from the registry. Returns True if found."""
if name in self._registry.get(component_type, {}):
del self._registry[component_type][name]
return True
return False
def clear(self) -> None:
"""Clear all registered components."""
self._registry.clear()
R = ComponentRegistry()