Add a registry argument to objreg.register.

This commit is contained in:
Florian Bruhin 2014-09-25 07:43:14 +02:00
parent e527db1560
commit 4067b584ec

View File

@ -102,7 +102,7 @@ def get(name, default=_UNSET, scope='global'):
raise raise
def register(name, obj, update=False, scope='global'): def register(name, obj, update=False, scope=None, registry=None):
"""Helper function to register an object. """Helper function to register an object.
Args: Args:
@ -110,6 +110,14 @@ def register(name, obj, update=False, scope='global'):
obj: The object to register. obj: The object to register.
update: If True, allows to update an already registered object. update: If True, allows to update an already registered object.
""" """
if scope is not None and registry is not None:
raise ValueError("scope ({}) and registry ({}) can't be given at the "
"same time!".format(scope, registry))
if registry is not None:
reg = registry
else:
if scope is None:
scope = 'global'
reg = _get_registry(scope) reg = _get_registry(scope)
if not update and name in reg: if not update and name in reg:
raise KeyError("Object '{}' is already registered ({})!".format( raise KeyError("Object '{}' is already registered ({})!".format(