Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change: Create core registry infrastructure #105

Open
mathysgrapotte opened this issue Feb 19, 2025 · 1 comment
Open

change: Create core registry infrastructure #105

mathysgrapotte opened this issue Feb 19, 2025 · 1 comment

Comments

@mathysgrapotte
Copy link
Owner

Is your change request related to a problem? Please describe.

Need shared foundation for component registries to avoid code duplication.

Describe the solution you'd like

  1. Create src/stimulus/core/registry.py with:
    • BaseRegistry class handling entry points/decorators
    • Generic type enforcement for components
    • Plugin loading system
  2. Add basic tests for registration/instantiation
  3. Document base class usage for developers

Describe alternatives you've considered

Additional context

Required for all component registries. Example usage:

class ComponentRegistry(BaseRegistry[AbstractComponent]):
@mathysgrapotte
Copy link
Owner Author

could be something like this :

"""Central registry system for Stimulus components."""
from importlib.metadata import entry_points
from typing import Type, Dict, TypeVar, Generic

T = TypeVar('T')

class BaseRegistry(Generic[T]):
    """Base registry class for component registration."""
    
    def __init__(self, entry_point_group: str, base_class: Type[T]):
        self._components: Dict[str, Type[T]] = {}
        self.entry_point_group = entry_point_group
        self.base_class = base_class
        self.load_builtins()
        self.load_plugins()

    def register(self, name: str) -> callable:
        """Decorator factory for component registration."""
        def decorator(cls: Type[T]):
            if not issubclass(cls, self.base_class):
                raise TypeError(f"{cls.__name__} must subclass {self.base_class.__name__}")
            self._components[name] = cls
            return cls
        return decorator

    def load_builtins(self):
        """Override in child classes to register built-in components"""
        pass

    def load_plugins(self):
        """Load external components from entry points"""
        for ep in entry_points().select(group=self.entry_point_group):
            self._components[ep.name] = ep.load()

    def get(self, name: str, **params) -> T:
        """Instantiate a component with parameters"""
        cls = self._components.get(name)
        if not cls:
            available = ", ".join(self._components.keys())
            raise ValueError(f"Unknown {self.base_class.__name__} '{name}'. Available: {available}")
        return cls(**params)

    @property
    def component_names(self):
        return list(self._components.keys())

@mathysgrapotte mathysgrapotte moved this to Todo - mid issues in Stimulus v1.0 Feb 19, 2025
@mathysgrapotte mathysgrapotte moved this from Todo to Master issues in Stimulus v1.0 Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Master issues
Development

No branches or pull requests

1 participant