~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
12
12
#
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
16
16
 
17
17
"""Classes to provide name-to-object registry-like support."""
18
18
 
19
19
 
 
20
from bzrlib.pyutils import get_named_object
 
21
 
 
22
 
20
23
class _ObjectGetter(object):
21
24
    """Maintain a reference to an object, and return the object on request.
22
25
 
32
35
    def __init__(self, obj):
33
36
        self._obj = obj
34
37
 
 
38
    def get_module(self):
 
39
        """Get the module the object was loaded from."""
 
40
        return self._obj.__module__
 
41
 
35
42
    def get_obj(self):
36
43
        """Get the object that was saved at creation time"""
37
44
        return self._obj
51
58
        self._imported = False
52
59
        super(_LazyObjectGetter, self).__init__(None)
53
60
 
 
61
    def get_module(self):
 
62
        """Get the module the referenced object will be loaded from.
 
63
        """
 
64
        return self._module_name
 
65
 
54
66
    def get_obj(self):
55
67
        """Get the referenced object.
56
68
 
58
70
        return the imported object.
59
71
        """
60
72
        if not self._imported:
61
 
            self._do_import()
 
73
            self._obj = get_named_object(self._module_name, self._member_name)
 
74
            self._imported = True
62
75
        return super(_LazyObjectGetter, self).get_obj()
63
76
 
64
 
    def _do_import(self):
65
 
        obj = __import__(self._module_name, globals(), locals(),
66
 
                         [self._member_name])
67
 
        if self._member_name:
68
 
            obj = getattr(obj, self._member_name)
69
 
        self._obj = obj
70
 
        self._imported = True
71
 
 
72
77
    def __repr__(self):
73
 
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
 
78
        return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
74
79
            self.__class__.__module__, self.__class__.__name__, id(self),
75
 
            self._module_name, self._member_name)
 
80
            self._module_name, self._member_name, self._imported)
76
81
 
77
82
 
78
83
class Registry(object):
107
112
        :param obj: The object to register.
108
113
        :param help: Help text for this entry. This may be a string or
109
114
                a callable. If it is a callable, it should take two
110
 
                parameters (registry, key): this registry and the key that 
 
115
                parameters (registry, key): this registry and the key that
111
116
                the help was registered under.
112
117
        :param info: More information for this entry. Registry.get_info()
113
118
                can be used to get this information. Registry treats this as an
127
132
                      override_existing=False):
128
133
        """Register a new object to be loaded on request.
129
134
 
 
135
        :param key: This is the key to use to request the object later.
130
136
        :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 
 
137
        :param member_name: The member of the module to return.  If empty or
132
138
                None, get() will return the module itself.
133
139
        :param help: Help text for this entry. This may be a string or
134
140
                a callable.
135
 
        :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).
136
144
        :param override_existing: If True, replace the existing object
137
145
                with the new one. If False, if there is already something
138
146
                registered with the same key, raise a KeyError
152
160
        """Return the object register()'ed to the given key.
153
161
 
154
162
        May raise ImportError if the object was registered lazily and
155
 
        there are any problems, or AttributeError if the module does not 
 
163
        there are any problems, or AttributeError if the module does not
156
164
        have the supplied member.
157
165
 
158
166
        :param key: The key to obtain the object for. If no object has been
167
175
        """
168
176
        return self._dict[self._get_key_or_default(key)].get_obj()
169
177
 
 
178
    def _get_module(self, key):
 
179
        """Return the module the object will be or was loaded from.
 
180
 
 
181
        :param key: The key to obtain the module for.
 
182
        :return: The name of the module
 
183
        """
 
184
        return self._dict[key].get_module()
 
185
 
170
186
    def get_prefix(self, fullname):
171
187
        """Return an object whose key is a prefix of the supplied value.
172
188
 
174
190
        :return: a tuple of (object, remainder), where the remainder is the
175
191
            portion of the name that did not match the key.
176
192
        """
177
 
        for key, value in self.iteritems():
 
193
        for key in self.keys():
178
194
            if fullname.startswith(key):
179
 
                return value, fullname[len(key):]
 
195
                return self.get(key), fullname[len(key):]
180
196
 
181
197
    def _get_key_or_default(self, key=None):
182
198
        """Return either 'key' or the default key if key is None"""
217
233
            yield key, getter.get_obj()
218
234
 
219
235
    def items(self):
220
 
        return sorted(self.iteritems())
 
236
        # We should not use the iteritems() implementation below (see bug
 
237
        # #430510)
 
238
        return sorted([(key, getter.get_obj())
 
239
                       for key, getter in self._dict.items()])
221
240
 
222
241
    def _set_default_key(self, key):
223
242
        if not self._dict.has_key(key):
231
250
    default_key = property(_get_default_key, _set_default_key,
232
251
                            doc="Current value of the default key."
233
252
                                " Can be set to any existing key.")
 
253
 
 
254
 
 
255
class FormatRegistry(Registry):
 
256
    """Registry specialised for handling formats."""
 
257
 
 
258
    def __init__(self, other_registry=None):
 
259
        Registry.__init__(self)
 
260
        self._other_registry = other_registry
 
261
 
 
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)
 
269
 
 
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)
 
280
 
 
281
    def remove(self, key):
 
282
        Registry.remove(self, key)
 
283
        if self._other_registry is not None:
 
284
            self._other_registry.remove(key)
 
285
 
 
286
    def get(self, format_string):
 
287
        r = Registry.get(self, format_string)
 
288
        if callable(r):
 
289
            r = r()
 
290
        return r