mirror of
https://github.com/django-components/django-components.git
synced 2025-08-05 14:57:59 +00:00
Merge pull request #406 from GabDug/fix/type-component-registry
fix: type component registry
This commit is contained in:
commit
a9c922c8e6
2 changed files with 20 additions and 11 deletions
|
@ -164,6 +164,7 @@ def _get_dir_path_from_component_module_path(component_module_path: str, candida
|
|||
class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
||||
# Either template_name or template must be set on subclass OR subclass must implement get_template() with
|
||||
# non-null return.
|
||||
class_hash: ClassVar[int]
|
||||
template_name: ClassVar[Optional[str]] = None
|
||||
template: Optional[str] = None
|
||||
js: Optional[str] = None
|
||||
|
|
|
@ -1,3 +1,11 @@
|
|||
from typing import TYPE_CHECKING, Callable, Dict, Type, TypeVar
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from django_components import component
|
||||
|
||||
_TC = TypeVar("_TC", bound=Type["component.Component"])
|
||||
|
||||
|
||||
class AlreadyRegistered(Exception):
|
||||
pass
|
||||
|
||||
|
@ -6,39 +14,39 @@ class NotRegistered(Exception):
|
|||
pass
|
||||
|
||||
|
||||
class ComponentRegistry(object):
|
||||
def __init__(self):
|
||||
self._registry = {} # component name -> component_class mapping
|
||||
class ComponentRegistry:
|
||||
def __init__(self) -> None:
|
||||
self._registry: Dict[str, Type["component.Component"]] = {} # component name -> component_class mapping
|
||||
|
||||
def register(self, name=None, component=None):
|
||||
def register(self, name: str, component: Type["component.Component"]) -> None:
|
||||
existing_component = self._registry.get(name)
|
||||
if existing_component and existing_component.class_hash != component.class_hash:
|
||||
raise AlreadyRegistered('The component "%s" has already been registered' % name)
|
||||
self._registry[name] = component
|
||||
|
||||
def unregister(self, name):
|
||||
def unregister(self, name: str) -> None:
|
||||
self.get(name)
|
||||
|
||||
del self._registry[name]
|
||||
|
||||
def get(self, name):
|
||||
def get(self, name: str) -> Type["component.Component"]:
|
||||
if name not in self._registry:
|
||||
raise NotRegistered('The component "%s" is not registered' % name)
|
||||
|
||||
return self._registry[name]
|
||||
|
||||
def all(self):
|
||||
def all(self) -> Dict[str, Type["component.Component"]]:
|
||||
return self._registry
|
||||
|
||||
def clear(self):
|
||||
def clear(self) -> None:
|
||||
self._registry = {}
|
||||
|
||||
|
||||
# This variable represents the global component registry
|
||||
registry = ComponentRegistry()
|
||||
registry: ComponentRegistry = ComponentRegistry()
|
||||
|
||||
|
||||
def register(name):
|
||||
def register(name: str) -> Callable[[_TC], _TC]:
|
||||
"""Class decorator to register a component.
|
||||
|
||||
Usage:
|
||||
|
@ -48,7 +56,7 @@ def register(name):
|
|||
...
|
||||
"""
|
||||
|
||||
def decorator(component):
|
||||
def decorator(component: _TC) -> _TC:
|
||||
registry.register(name=name, component=component)
|
||||
return component
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue