Don't allow registering the same component twice.

This commit is contained in:
Emil Stenström 2015-06-11 20:47:35 +02:00
parent 313852ce21
commit 00bd40aaaf
2 changed files with 11 additions and 0 deletions

View file

@ -1,8 +1,14 @@
class AlreadyRegistered(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
# This variable represents the global component registry

View file

@ -22,3 +22,8 @@ class ComponentRegistryTest(unittest.TestCase):
self.registry._registry.items(),
[("testcomponent", MockComponent), ("testcomponent2", MockComponent)]
)
def test_prevent_registering_twice(self):
self.registry.register(name="testcomponent", component=MockComponent)
with self.assertRaises(component.AlreadyRegistered):
self.registry.register(name="testcomponent", component=MockComponent)