~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-10-05 21:15:13 UTC
  • mfrom: (5448.3.5 374700-Add-gnu-lsh-support)
  • Revision ID: pqm@pqm.ubuntu.com-20101005211513-whouyj5t7oo92gmq
(gz) Add support for GNU lsh as a secure shell client (Matthew Gordon)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Classes to provide name-to-object registry-like support."""
18
18
 
19
 
from __future__ import absolute_import
20
 
 
21
 
from bzrlib.pyutils import get_named_object
22
 
 
23
19
 
24
20
class _ObjectGetter(object):
25
21
    """Maintain a reference to an object, and return the object on request.
36
32
    def __init__(self, obj):
37
33
        self._obj = obj
38
34
 
39
 
    def get_module(self):
40
 
        """Get the module the object was loaded from."""
41
 
        return self._obj.__module__
42
 
 
43
35
    def get_obj(self):
44
36
        """Get the object that was saved at creation time"""
45
37
        return self._obj
59
51
        self._imported = False
60
52
        super(_LazyObjectGetter, self).__init__(None)
61
53
 
62
 
    def get_module(self):
63
 
        """Get the module the referenced object will be loaded from.
64
 
        """
65
 
        return self._module_name
66
 
 
67
54
    def get_obj(self):
68
55
        """Get the referenced object.
69
56
 
71
58
        return the imported object.
72
59
        """
73
60
        if not self._imported:
74
 
            self._obj = get_named_object(self._module_name, self._member_name)
75
 
            self._imported = True
 
61
            self._do_import()
76
62
        return super(_LazyObjectGetter, self).get_obj()
77
63
 
 
64
    def _do_import(self):
 
65
        if self._member_name:
 
66
            segments = self._member_name.split('.')
 
67
            names = segments[0:1]
 
68
        else:
 
69
            names = [self._member_name]
 
70
        obj = __import__(self._module_name, globals(), locals(), names)
 
71
        if self._member_name:
 
72
            for segment in segments:
 
73
                obj = getattr(obj, segment)
 
74
        self._obj = obj
 
75
        self._imported = True
 
76
 
78
77
    def __repr__(self):
79
 
        return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
 
78
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
80
79
            self.__class__.__module__, self.__class__.__name__, id(self),
81
 
            self._module_name, self._member_name, self._imported)
 
80
            self._module_name, self._member_name)
82
81
 
83
82
 
84
83
class Registry(object):
133
132
                      override_existing=False):
134
133
        """Register a new object to be loaded on request.
135
134
 
136
 
        :param key: This is the key to use to request the object later.
137
135
        :param module_name: The python path to the module. Such as 'os.path'.
138
136
        :param member_name: The member of the module to return.  If empty or
139
137
                None, get() will return the module itself.
140
138
        :param help: Help text for this entry. This may be a string or
141
139
                a callable.
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).
 
140
        :param info: More information for this entry. Registry
145
141
        :param override_existing: If True, replace the existing object
146
142
                with the new one. If False, if there is already something
147
143
                registered with the same key, raise a KeyError
176
172
        """
177
173
        return self._dict[self._get_key_or_default(key)].get_obj()
178
174
 
179
 
    def _get_module(self, key):
180
 
        """Return the module the object will be or was loaded from.
181
 
 
182
 
        :param key: The key to obtain the module for.
183
 
        :return: The name of the module
184
 
        """
185
 
        return self._dict[key].get_module()
186
 
 
187
175
    def get_prefix(self, fullname):
188
176
        """Return an object whose key is a prefix of the supplied value.
189
177
 
260
248
        Registry.__init__(self)
261
249
        self._other_registry = other_registry
262
250
 
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)
270
 
 
271
251
    def register_lazy(self, key, module_name, member_name,
272
252
                      help=None, info=None,
273
253
                      override_existing=False):
279
259
            self._other_registry.register_lazy(key, module_name, member_name,
280
260
                help=help, info=info, override_existing=override_existing)
281
261
 
282
 
    def remove(self, key):
283
 
        Registry.remove(self, key)
284
 
        if self._other_registry is not None:
285
 
            self._other_registry.remove(key)
286
 
 
287
262
    def get(self, format_string):
288
263
        r = Registry.get(self, format_string)
289
264
        if callable(r):
290
265
            r = r()
291
266
        return r
 
267
 
 
268