mirror of
https://github.com/django-components/django-components.git
synced 2025-09-26 23:49:07 +00:00
Move registry to separate file.
This commit is contained in:
parent
ebf4ac1d20
commit
df22a4049f
2 changed files with 22 additions and 21 deletions
|
@ -1,24 +1,4 @@
|
||||||
class AlreadyRegistered(Exception):
|
from .component_registry import ComponentRegistry, AlreadyRegistered, NotRegistered # NOQA
|
||||||
pass
|
|
||||||
|
|
||||||
class NotRegistered(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class ComponentRegistry(object):
|
|
||||||
def __init__(self):
|
|
||||||
self._registry = {} # component name -> component_class mapping
|
|
||||||
|
|
||||||
def register(self, name=None, component=None):
|
|
||||||
if name in self._registry:
|
|
||||||
raise AlreadyRegistered('The component "%s" is already registered' % name)
|
|
||||||
|
|
||||||
self._registry[name] = component
|
|
||||||
|
|
||||||
def unregister(self, name):
|
|
||||||
if name not in self._registry:
|
|
||||||
raise NotRegistered('The component "%s" is not registered' % name)
|
|
||||||
|
|
||||||
del self._registry[name]
|
|
||||||
|
|
||||||
# This variable represents the global component registry
|
# This variable represents the global component registry
|
||||||
registry = ComponentRegistry()
|
registry = ComponentRegistry()
|
||||||
|
|
21
django_components/component_registry.py
Normal file
21
django_components/component_registry.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
class AlreadyRegistered(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class NotRegistered(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ComponentRegistry(object):
|
||||||
|
def __init__(self):
|
||||||
|
self._registry = {} # component name -> component_class mapping
|
||||||
|
|
||||||
|
def register(self, name=None, component=None):
|
||||||
|
if name in self._registry:
|
||||||
|
raise AlreadyRegistered('The component "%s" is already registered' % name)
|
||||||
|
|
||||||
|
self._registry[name] = component
|
||||||
|
|
||||||
|
def unregister(self, name):
|
||||||
|
if name not in self._registry:
|
||||||
|
raise NotRegistered('The component "%s" is not registered' % name)
|
||||||
|
|
||||||
|
del self._registry[name]
|
Loading…
Add table
Add a link
Reference in a new issue