13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Classes to provide name-to-object registry-like support."""
19
from __future__ import absolute_import
21
from bzrlib.pyutils import get_named_object
20
24
class _ObjectGetter(object):
21
25
"""Maintain a reference to an object, and return the object on request.
58
71
return the imported object.
60
73
if not self._imported:
74
self._obj = get_named_object(self._module_name, self._member_name)
62
76
return super(_LazyObjectGetter, self).get_obj()
65
obj = __import__(self._module_name, globals(), locals(),
68
obj = getattr(obj, self._member_name)
72
78
def __repr__(self):
73
return "<%s.%s object at %x, module=%r attribute=%r>" % (
79
return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
74
80
self.__class__.__module__, self.__class__.__name__, id(self),
75
self._module_name, self._member_name)
81
self._module_name, self._member_name, self._imported)
78
84
class Registry(object):
107
113
:param obj: The object to register.
108
114
:param help: Help text for this entry. This may be a string or
109
115
a callable. If it is a callable, it should take two
110
parameters (registry, key): this registry and the key that
116
parameters (registry, key): this registry and the key that
111
117
the help was registered under.
112
118
:param info: More information for this entry. Registry.get_info()
113
119
can be used to get this information. Registry treats this as an
127
133
override_existing=False):
128
134
"""Register a new object to be loaded on request.
136
:param key: This is the key to use to request the object later.
130
137
:param module_name: The python path to the module. Such as 'os.path'.
131
:param member_name: The member of the module to return. If empty or
138
:param member_name: The member of the module to return. If empty or
132
139
None, get() will return the module itself.
133
140
:param help: Help text for this entry. This may be a string or
135
:param info: More information for this entry. Registry
142
:param info: More information for this entry. Registry.get_info()
143
can be used to get this information. Registry treats this as an
144
opaque storage location (it is defined by the caller).
136
145
:param override_existing: If True, replace the existing object
137
146
with the new one. If False, if there is already something
138
147
registered with the same key, raise a KeyError
152
161
"""Return the object register()'ed to the given key.
154
163
May raise ImportError if the object was registered lazily and
155
there are any problems, or AttributeError if the module does not
164
there are any problems, or AttributeError if the module does not
156
165
have the supplied member.
158
167
:param key: The key to obtain the object for. If no object has been
168
177
return self._dict[self._get_key_or_default(key)].get_obj()
179
def _get_module(self, key):
180
"""Return the module the object will be or was loaded from.
182
:param key: The key to obtain the module for.
183
:return: The name of the module
185
return self._dict[key].get_module()
170
187
def get_prefix(self, fullname):
171
188
"""Return an object whose key is a prefix of the supplied value.
174
191
:return: a tuple of (object, remainder), where the remainder is the
175
192
portion of the name that did not match the key.
177
for key, value in self.iteritems():
194
for key in self.keys():
178
195
if fullname.startswith(key):
179
return value, fullname[len(key):]
196
return self.get(key), fullname[len(key):]
181
198
def _get_key_or_default(self, key=None):
182
199
"""Return either 'key' or the default key if key is None"""
216
233
for key, getter in self._dict.iteritems():
217
234
yield key, getter.get_obj()
237
# We should not use the iteritems() implementation below (see bug
239
return sorted([(key, getter.get_obj())
240
for key, getter in self._dict.items()])
219
242
def _set_default_key(self, key):
220
243
if not self._dict.has_key(key):
221
244
raise KeyError('No object registered under key %s.' % key)
228
251
default_key = property(_get_default_key, _set_default_key,
229
252
doc="Current value of the default key."
230
253
" Can be set to any existing key.")
256
class FormatRegistry(Registry):
257
"""Registry specialised for handling formats."""
259
def __init__(self, other_registry=None):
260
Registry.__init__(self)
261
self._other_registry = other_registry
263
def register(self, key, obj, help=None, info=None,
264
override_existing=False):
265
Registry.register(self, key, obj, help=help, info=info,
266
override_existing=override_existing)
267
if self._other_registry is not None:
268
self._other_registry.register(key, obj, help=help,
269
info=info, override_existing=override_existing)
271
def register_lazy(self, key, module_name, member_name,
272
help=None, info=None,
273
override_existing=False):
274
# Overridden to allow capturing registrations to two seperate
275
# registries in a single call.
276
Registry.register_lazy(self, key, module_name, member_name,
277
help=help, info=info, override_existing=override_existing)
278
if self._other_registry is not None:
279
self._other_registry.register_lazy(key, module_name, member_name,
280
help=help, info=info, override_existing=override_existing)
282
def remove(self, key):
283
Registry.remove(self, key)
284
if self._other_registry is not None:
285
self._other_registry.remove(key)
287
def get(self, format_string):
288
r = Registry.get(self, format_string)