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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Classes to provide name-to-object registry-like support."""
20
from bzrlib.pyutils import get_named_object
23
20
class _ObjectGetter(object):
24
21
"""Maintain a reference to an object, and return the object on request.
70
58
return the imported object.
72
60
if not self._imported:
73
self._obj = get_named_object(self._module_name, self._member_name)
75
62
return super(_LazyObjectGetter, self).get_obj()
65
obj = __import__(self._module_name, globals(), locals(),
68
obj = getattr(obj, self._member_name)
77
72
def __repr__(self):
78
return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
73
return "<%s.%s object at %x, module=%r attribute=%r>" % (
79
74
self.__class__.__module__, self.__class__.__name__, id(self),
80
self._module_name, self._member_name, self._imported)
75
self._module_name, self._member_name)
83
78
class Registry(object):
112
107
:param obj: The object to register.
113
108
:param help: Help text for this entry. This may be a string or
114
109
a callable. If it is a callable, it should take two
115
parameters (registry, key): this registry and the key that
110
parameters (registry, key): this registry and the key that
116
111
the help was registered under.
117
112
:param info: More information for this entry. Registry.get_info()
118
113
can be used to get this information. Registry treats this as an
133
128
"""Register a new object to be loaded on request.
135
130
:param module_name: The python path to the module. Such as 'os.path'.
136
:param member_name: The member of the module to return. If empty or
131
:param member_name: The member of the module to return. If empty or
137
132
None, get() will return the module itself.
138
133
:param help: Help text for this entry. This may be a string or
140
:param info: More information for this entry. Registry
135
:param info: More information for this entry. Registry
141
136
:param override_existing: If True, replace the existing object
142
137
with the new one. If False, if there is already something
143
138
registered with the same key, raise a KeyError
157
152
"""Return the object register()'ed to the given key.
159
154
May raise ImportError if the object was registered lazily and
160
there are any problems, or AttributeError if the module does not
155
there are any problems, or AttributeError if the module does not
161
156
have the supplied member.
163
158
:param key: The key to obtain the object for. If no object has been
173
168
return self._dict[self._get_key_or_default(key)].get_obj()
175
def _get_module(self, key):
176
"""Return the module the object will be or was loaded from.
178
:param key: The key to obtain the module for.
179
:return: The name of the module
181
return self._dict[key].get_module()
183
def get_prefix(self, fullname):
184
"""Return an object whose key is a prefix of the supplied value.
186
:fullname: The name to find a prefix for
187
:return: a tuple of (object, remainder), where the remainder is the
188
portion of the name that did not match the key.
190
for key in self.keys():
191
if fullname.startswith(key):
192
return self.get(key), fullname[len(key):]
194
170
def _get_key_or_default(self, key=None):
195
171
"""Return either 'key' or the default key if key is None"""
196
172
if key is not None:
229
205
for key, getter in self._dict.iteritems():
230
206
yield key, getter.get_obj()
233
# We should not use the iteritems() implementation below (see bug
235
return sorted([(key, getter.get_obj())
236
for key, getter in self._dict.items()])
238
208
def _set_default_key(self, key):
239
209
if not self._dict.has_key(key):
240
210
raise KeyError('No object registered under key %s.' % key)
247
217
default_key = property(_get_default_key, _set_default_key,
248
218
doc="Current value of the default key."
249
219
" Can be set to any existing key.")
252
class FormatRegistry(Registry):
253
"""Registry specialised for handling formats."""
255
def __init__(self, other_registry=None):
256
Registry.__init__(self)
257
self._other_registry = other_registry
259
def register(self, key, obj, help=None, info=None,
260
override_existing=False):
261
Registry.register(self, key, obj, help=help, info=info,
262
override_existing=override_existing)
263
if self._other_registry is not None:
264
self._other_registry.register(key, obj, help=help,
265
info=info, override_existing=override_existing)
267
def register_lazy(self, key, module_name, member_name,
268
help=None, info=None,
269
override_existing=False):
270
# Overridden to allow capturing registrations to two seperate
271
# registries in a single call.
272
Registry.register_lazy(self, key, module_name, member_name,
273
help=help, info=info, override_existing=override_existing)
274
if self._other_registry is not None:
275
self._other_registry.register_lazy(key, module_name, member_name,
276
help=help, info=info, override_existing=override_existing)
278
def remove(self, key):
279
Registry.remove(self, key)
280
if self._other_registry is not None:
281
self._other_registry.remove(key)
283
def get(self, format_string):
284
r = Registry.get(self, format_string)