20
20
class Registry(object):
21
"""A class that registers objects to a name."""
21
"""A class that registers objects to a name.
23
This is designed such that you can register objects in a lazy fashion,
24
so that they can be imported later. While still having the help text
23
28
def __init__(self):
24
29
"""Create a new Registry."""
25
30
self._default_key = None
26
31
# Map from key => (is_lazy, info)
29
def register(self, key, object):
36
def register(self, key, object, help=None, info=None,
37
override_existing=False):
30
38
"""Register a new object to a name.
32
40
:param key: This is the key to use to request the object later.
33
41
:param object: The object to register.
42
:param help: Help text for this entry. This may be a string or
43
a callable. If it is a callable, it should take a
44
single parameter, which is the key that the help was
46
:param info: More information for this entry. Registry.get_info()
47
can be used to get this information. It is meant as an
48
opaque storage location.
49
:param override_existing: If True, replace the existing object
50
with the new one. If False, if there is already something
51
registered with the same key, raise a KeyError
53
if not override_existing:
55
raise KeyError('Key %r already registered' % key)
35
56
self._dict[key] = (False, object)
37
__setitem__ = register
39
def register_lazy(self, key, module_name, member_name):
57
self._add_help_and_info(key, help=help, info=info)
59
def register_lazy(self, key, module_name, member_name,
61
override_existing=False):
40
62
"""Register a new object to be loaded on request.
42
64
:param module_name: The python path to the module. Such as 'os.path'.
43
65
:param member_name: The member of the module to return, if empty or
44
66
None get() will return the module itself.
67
:param help: Help text for this entry. This may be a string or
69
:param info: More information for this entry. Registry
70
:param override_existing: If True, replace the existing object
71
with the new one. If False, if there is already something
72
registered with the same key, raise a KeyError
74
if not override_existing:
76
raise KeyError('Key %r already registered' % key)
46
77
self._dict[key] = (True, (module_name, member_name))
78
self._add_help_and_info(key, help=help, info=info)
80
def _add_help_and_info(self, key, help=None, info=None):
81
"""Add the help and information about this key"""
82
self._help_dict[key] = help
83
self._info_dict[key] = info
48
85
def get(self, key=None):
49
86
"""Return the object register()'ed to the given key.
59
96
:return: The previously registered object.
62
if self.default_key is None:
63
raise KeyError('Key is None, and no default key is set')
65
key = self.default_key
66
return self._get_one(key)
98
return self._get_one(self._get_key_or_default(key))
100
def _get_key_or_default(self, key=None):
101
"""Return either 'key' or the default key if key is None"""
104
if self.default_key is None:
105
raise KeyError('Key is None, and no default key is set')
107
return self.default_key
88
129
self._dict[key] = (False, obj)
132
def get_help(self, key=None):
133
"""Get the help text associated with the given key"""
134
the_help = self._help_dict[self._get_key_or_default(key)]
135
if callable(the_help):
139
def get_info(self, key=None):
140
"""Get the extra information associated with the given key"""
141
return self._info_dict[self._get_key_or_default(key)]
91
143
def remove(self, key):
92
144
"""Remove a registered entry.