39
40
if self._first_is_default and not self._dict:
40
41
self._default_key = key
41
self._dict[key] = object
42
self._dict[key] = (False, object)
44
__setitem__ = register
46
def register_lazy(self, key, module_name, member_name):
47
"""Register a new object to be loaded on request.
49
:param module_name: The python path to the module. Such as 'os.path'.
50
:param member_name: The member of the module to return, if empty or
51
None get() will return the module itself.
53
if self._first_is_default and not self._dict:
54
self._default_key = key
55
self._dict[key] = (True, (module_name, member_name))
43
57
def get(self, key=None):
44
58
"""Return the object register()'ed to the given key.
60
May raise ImportError if the object was registered lazily and
61
there are any problems, or AttributeError if the module does not
62
have the supplied member.
46
64
:param key: The key to obtain the object for. If no object has been
47
65
registered to that key, the object registered for self.default_key
48
66
will be returned instead, if it exists. Otherwise KeyError will be
50
68
:return: The previously registered object.
53
return self._dict[key]
55
if self.default_key is not None:
56
return self._dict[self.default_key]
71
if self.default_key is None:
72
raise KeyError('Key is None, and no default key is set')
74
key = self.default_key
75
return self._get_one(key)
79
def _get_one(self, key):
80
"""Attempt to return a single entry.
82
This will import the entry if it is lazy, and replace the registry
83
with the imported object.
85
This may raise KeyError if the given key doesn't exist, or ImportError
88
is_lazy, info_or_object = self._dict[key]
90
# We have a real object to return
93
module_name, member_name = info_or_object
94
obj = __import__(module_name, globals(), locals(), [member_name])
96
obj = getattr(obj, member_name)
97
self._dict[key] = (False, obj)
100
def remove(self, key):
101
"""Remove a registered entry.
103
This is mostly for the test suite, but it can be used by others
109
def __contains__(self, key):
110
return key in self._dict
113
return len(self._dict)
61
116
"""Get a list of registered entries"""
62
117
return sorted(self._dict.keys())
120
return self._dict.iterkeys()
123
for key in self._dict.iterkeys():
124
yield key, self._get_one(key)
127
return list(self.iteritems())
129
def itervalues(self):
130
for key in self._dict.iterkeys():
131
yield self._get_one(key)
134
return list(self.itervalues())
64
136
def _set_default_key(self, key):
65
137
if not self._dict.has_key(key):
66
138
raise KeyError('No object registered under key %s.' % key)
70
142
def _get_default_key(self):
71
143
return self._default_key
73
default_key = property(_get_default_key, _set_default_key)
74
"""Current value of the default key. Can be set to any existing key."""
77
class LazyImportRegistry(Registry):
78
"""A class to register modules/members to be loaded on request."""
80
def register(self, key, module_name, member_name):
81
"""Register a new object to be loaded on request.
83
:param module_name: The python path to the module. Such as 'os.path'.
84
:param member_name: The member of the module to return, if empty or None
85
get() will return the module itself.
87
Registry.register(self, key, (module_name, member_name))
89
def get(self, key=None):
90
"""Load the module and return the object specified by the given key.
92
May raise ImportError if there are any problems, or AttributeError if
93
the module does not have the supplied member.
95
module_name, member_name = Registry.get(self, key)
96
module = __import__(module_name, globals(), locals(), [member_name])
98
return getattr(module, member_name)
145
default_key = property(_get_default_key, _set_default_key,
146
doc="Current value of the default key."
147
"Can be set to any existing key.")