1
# Copyright (C) 2006-2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Classes to provide name-to-object registry-like support."""
20
from bzrlib.pyutils import get_named_object
23
class _ObjectGetter(object):
24
"""Maintain a reference to an object, and return the object on request.
26
This is used by Registry to make plain objects function similarly
27
to lazily imported objects.
29
Objects can be any sort of python object (class, function, module,
35
def __init__(self, obj):
39
"""Get the module the object was loaded from."""
40
return self._obj.__module__
43
"""Get the object that was saved at creation time"""
47
class _LazyObjectGetter(_ObjectGetter):
48
"""Keep a record of a possible object.
50
When requested, load and return it.
53
__slots__ = ['_module_name', '_member_name', '_imported']
55
def __init__(self, module_name, member_name):
56
self._module_name = module_name
57
self._member_name = member_name
58
self._imported = False
59
super(_LazyObjectGetter, self).__init__(None)
62
"""Get the module the referenced object will be loaded from.
64
return self._module_name
67
"""Get the referenced object.
69
Upon first request, the object will be imported. Future requests will
70
return the imported object.
72
if not self._imported:
73
self._obj = get_named_object(self._module_name, self._member_name)
75
return super(_LazyObjectGetter, self).get_obj()
78
return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
79
self.__class__.__module__, self.__class__.__name__, id(self),
80
self._module_name, self._member_name, self._imported)
83
class Registry(object):
84
"""A class that registers objects to a name.
86
There are many places that want to collect related objects and access them
87
by a key. This class is designed to allow registering the mapping from key
88
to object. It goes one step further, and allows registering a name to a
89
hypothetical object which has not been imported yet. It also supports
90
adding additional information at registration time so that decisions can be
91
made without having to import the object (which may be expensive).
93
The functions 'get', 'get_info', and 'get_help' also support a
94
'default_key' (settable through my_registry.default_key = XXX, XXX must
95
already be registered.) Calling my_registry.get() or my_registry.get(None),
96
will return the entry for the default key.
100
"""Create a new Registry."""
101
self._default_key = None
102
# Map from key => (is_lazy, info)
107
def register(self, key, obj, help=None, info=None,
108
override_existing=False):
109
"""Register a new object to a name.
111
:param key: This is the key to use to request the object later.
112
:param obj: The object to register.
113
:param help: Help text for this entry. This may be a string or
114
a callable. If it is a callable, it should take two
115
parameters (registry, key): this registry and the key that
116
the help was registered under.
117
:param info: More information for this entry. Registry.get_info()
118
can be used to get this information. Registry treats this as an
119
opaque storage location (it is defined by the caller).
120
:param override_existing: Raise KeyErorr if False and something has
121
already been registered for that key. If True, ignore if there
122
is an existing key (always register the new value).
124
if not override_existing:
125
if key in self._dict:
126
raise KeyError('Key %r already registered' % key)
127
self._dict[key] = _ObjectGetter(obj)
128
self._add_help_and_info(key, help=help, info=info)
130
def register_lazy(self, key, module_name, member_name,
131
help=None, info=None,
132
override_existing=False):
133
"""Register a new object to be loaded on request.
135
:param key: This is the key to use to request the object later.
136
:param module_name: The python path to the module. Such as 'os.path'.
137
:param member_name: The member of the module to return. If empty or
138
None, get() will return the module itself.
139
:param help: Help text for this entry. This may be a string or
141
:param info: More information for this entry. Registry.get_info()
142
can be used to get this information. Registry treats this as an
143
opaque storage location (it is defined by the caller).
144
:param override_existing: If True, replace the existing object
145
with the new one. If False, if there is already something
146
registered with the same key, raise a KeyError
148
if not override_existing:
149
if key in self._dict:
150
raise KeyError('Key %r already registered' % key)
151
self._dict[key] = _LazyObjectGetter(module_name, member_name)
152
self._add_help_and_info(key, help=help, info=info)
154
def _add_help_and_info(self, key, help=None, info=None):
155
"""Add the help and information about this key"""
156
self._help_dict[key] = help
157
self._info_dict[key] = info
159
def get(self, key=None):
160
"""Return the object register()'ed to the given key.
162
May raise ImportError if the object was registered lazily and
163
there are any problems, or AttributeError if the module does not
164
have the supplied member.
166
:param key: The key to obtain the object for. If no object has been
167
registered to that key, the object registered for self.default_key
168
will be returned instead, if it exists. Otherwise KeyError will be
170
:return: The previously registered object.
171
:raises ImportError: If the object was registered lazily, and there are
172
problems during import.
173
:raises AttributeError: If registered lazily, and the module does not
174
contain the registered member.
176
return self._dict[self._get_key_or_default(key)].get_obj()
178
def _get_module(self, key):
179
"""Return the module the object will be or was loaded from.
181
:param key: The key to obtain the module for.
182
:return: The name of the module
184
return self._dict[key].get_module()
186
def get_prefix(self, fullname):
187
"""Return an object whose key is a prefix of the supplied value.
189
:fullname: The name to find a prefix for
190
:return: a tuple of (object, remainder), where the remainder is the
191
portion of the name that did not match the key.
193
for key in self.keys():
194
if fullname.startswith(key):
195
return self.get(key), fullname[len(key):]
197
def _get_key_or_default(self, key=None):
198
"""Return either 'key' or the default key if key is None"""
201
if self.default_key is None:
202
raise KeyError('Key is None, and no default key is set')
204
return self.default_key
206
def get_help(self, key=None):
207
"""Get the help text associated with the given key"""
208
the_help = self._help_dict[self._get_key_or_default(key)]
209
if callable(the_help):
210
return the_help(self, key)
213
def get_info(self, key=None):
214
"""Get the extra information associated with the given key"""
215
return self._info_dict[self._get_key_or_default(key)]
217
def remove(self, key):
218
"""Remove a registered entry.
220
This is mostly for the test suite, but it can be used by others
224
def __contains__(self, key):
225
return key in self._dict
228
"""Get a list of registered entries"""
229
return sorted(self._dict.keys())
232
for key, getter in self._dict.iteritems():
233
yield key, getter.get_obj()
236
# We should not use the iteritems() implementation below (see bug
238
return sorted([(key, getter.get_obj())
239
for key, getter in self._dict.items()])
241
def _set_default_key(self, key):
242
if not self._dict.has_key(key):
243
raise KeyError('No object registered under key %s.' % key)
245
self._default_key = key
247
def _get_default_key(self):
248
return self._default_key
250
default_key = property(_get_default_key, _set_default_key,
251
doc="Current value of the default key."
252
" Can be set to any existing key.")
255
class FormatRegistry(Registry):
256
"""Registry specialised for handling formats."""
258
def __init__(self, other_registry=None):
259
Registry.__init__(self)
260
self._other_registry = other_registry
262
def register(self, key, obj, help=None, info=None,
263
override_existing=False):
264
Registry.register(self, key, obj, help=help, info=info,
265
override_existing=override_existing)
266
if self._other_registry is not None:
267
self._other_registry.register(key, obj, help=help,
268
info=info, override_existing=override_existing)
270
def register_lazy(self, key, module_name, member_name,
271
help=None, info=None,
272
override_existing=False):
273
# Overridden to allow capturing registrations to two seperate
274
# registries in a single call.
275
Registry.register_lazy(self, key, module_name, member_name,
276
help=help, info=info, override_existing=override_existing)
277
if self._other_registry is not None:
278
self._other_registry.register_lazy(key, module_name, member_name,
279
help=help, info=info, override_existing=override_existing)
281
def remove(self, key):
282
Registry.remove(self, key)
283
if self._other_registry is not None:
284
self._other_registry.remove(key)
286
def get(self, format_string):
287
r = Registry.get(self, format_string)