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."""
20
from bzrlib.pyutils import get_named_object
20
23
class _ObjectGetter(object):
21
24
"""Maintain a reference to an object, and return the object on request.
58
70
return the imported object.
60
72
if not self._imported:
73
self._obj = get_named_object(self._module_name, self._member_name)
62
75
return super(_LazyObjectGetter, self).get_obj()
65
obj = __import__(self._module_name, globals(), locals(),
68
obj = getattr(obj, self._member_name)
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)
73
83
class Registry(object):
102
112
:param obj: The object to register.
103
113
:param help: Help text for this entry. This may be a string or
104
114
a callable. If it is a callable, it should take two
105
parameters (registry, key): this registry and the key that
115
parameters (registry, key): this registry and the key that
106
116
the help was registered under.
107
117
:param info: More information for this entry. Registry.get_info()
108
118
can be used to get this information. Registry treats this as an
122
132
override_existing=False):
123
133
"""Register a new object to be loaded on request.
135
:param key: This is the key to use to request the object later.
125
136
:param module_name: The python path to the module. Such as 'os.path'.
126
:param member_name: The member of the module to return. If empty or
137
:param member_name: The member of the module to return. If empty or
127
138
None, get() will return the module itself.
128
139
:param help: Help text for this entry. This may be a string or
130
:param info: More information for this entry. Registry
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).
131
144
:param override_existing: If True, replace the existing object
132
145
with the new one. If False, if there is already something
133
146
registered with the same key, raise a KeyError
147
160
"""Return the object register()'ed to the given key.
149
162
May raise ImportError if the object was registered lazily and
150
there are any problems, or AttributeError if the module does not
163
there are any problems, or AttributeError if the module does not
151
164
have the supplied member.
153
166
:param key: The key to obtain the object for. If no object has been
163
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):]
165
197
def _get_key_or_default(self, key=None):
166
198
"""Return either 'key' or the default key if key is None"""
167
199
if key is not None:
200
232
for key, getter in self._dict.iteritems():
201
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()])
203
241
def _set_default_key(self, key):
204
242
if not self._dict.has_key(key):
205
243
raise KeyError('No object registered under key %s.' % key)
212
250
default_key = property(_get_default_key, _set_default_key,
213
251
doc="Current value of the default key."
214
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)