~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

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
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
 
19
23
 
20
24
class _ObjectGetter(object):
21
25
    """Maintain a reference to an object, and return the object on request.
32
36
    def __init__(self, obj):
33
37
        self._obj = obj
34
38
 
 
39
    def get_module(self):
 
40
        """Get the module the object was loaded from."""
 
41
        return self._obj.__module__
 
42
 
35
43
    def get_obj(self):
36
44
        """Get the object that was saved at creation time"""
37
45
        return self._obj
51
59
        self._imported = False
52
60
        super(_LazyObjectGetter, self).__init__(None)
53
61
 
 
62
    def get_module(self):
 
63
        """Get the module the referenced object will be loaded from.
 
64
        """
 
65
        return self._module_name
 
66
 
54
67
    def get_obj(self):
55
68
        """Get the referenced object.
56
69
 
58
71
        return the imported object.
59
72
        """
60
73
        if not self._imported:
61
 
            self._do_import()
 
74
            self._obj = get_named_object(self._module_name, self._member_name)
 
75
            self._imported = True
62
76
        return super(_LazyObjectGetter, self).get_obj()
63
77
 
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
 
 
77
78
    def __repr__(self):
78
 
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
 
79
        return "<%s.%s object at %x, module=%r attribute=%r imported=%r>" % (
79
80
            self.__class__.__module__, self.__class__.__name__, id(self),
80
 
            self._module_name, self._member_name)
 
81
            self._module_name, self._member_name, self._imported)
81
82
 
82
83
 
83
84
class Registry(object):
132
133
                      override_existing=False):
133
134
        """Register a new object to be loaded on request.
134
135
 
 
136
        :param key: This is the key to use to request the object later.
135
137
        :param module_name: The python path to the module. Such as 'os.path'.
136
138
        :param member_name: The member of the module to return.  If empty or
137
139
                None, get() will return the module itself.
138
140
        :param help: Help text for this entry. This may be a string or
139
141
                a callable.
140
 
        :param info: More information for this entry. Registry
 
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).
141
145
        :param override_existing: If True, replace the existing object
142
146
                with the new one. If False, if there is already something
143
147
                registered with the same key, raise a KeyError
172
176
        """
173
177
        return self._dict[self._get_key_or_default(key)].get_obj()
174
178
 
 
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
 
175
187
    def get_prefix(self, fullname):
176
188
        """Return an object whose key is a prefix of the supplied value.
177
189
 
248
260
        Registry.__init__(self)
249
261
        self._other_registry = other_registry
250
262
 
 
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
 
251
271
    def register_lazy(self, key, module_name, member_name,
252
272
                      help=None, info=None,
253
273
                      override_existing=False):
259
279
            self._other_registry.register_lazy(key, module_name, member_name,
260
280
                help=help, info=info, override_existing=override_existing)
261
281
 
 
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
 
262
287
    def get(self, format_string):
263
288
        r = Registry.get(self, format_string)
264
289
        if callable(r):
265
290
            r = r()
266
291
        return r
267
 
 
268