~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/registry.py

  • Committer: wang
  • Date: 2006-10-29 13:41:32 UTC
  • mto: (2104.4.1 wang_65714)
  • mto: This revision was merged to the branch mainline in revision 2109.
  • Revision ID: wang@ubuntu-20061029134132-3d7f4216f20c4aef
Replace python's difflib by patiencediff because the worst case 
performance is cubic for difflib and people commiting large data 
files are often hurt by this. The worst case performance of patience is 
quadratic. Fix bug 65714.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Classes to provide name-to-object registry-like support."""
18
18
 
62
62
        return super(_LazyObjectGetter, self).get_obj()
63
63
 
64
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)
 
65
        obj = __import__(self._module_name, globals(), locals(),
 
66
                         [self._member_name])
 
67
        if self._member_name:
 
68
            obj = getattr(obj, self._member_name)
74
69
        self._obj = obj
75
70
        self._imported = True
76
71
 
77
 
    def __repr__(self):
78
 
        return "<%s.%s object at %x, module=%r attribute=%r>" % (
79
 
            self.__class__.__module__, self.__class__.__name__, id(self),
80
 
            self._module_name, self._member_name)
81
 
 
82
72
 
83
73
class Registry(object):
84
74
    """A class that registers objects to a name.
112
102
        :param obj: The object to register.
113
103
        :param help: Help text for this entry. This may be a string or
114
104
                a callable. If it is a callable, it should take two
115
 
                parameters (registry, key): this registry and the key that
 
105
                parameters (registry, key): this registry and the key that 
116
106
                the help was registered under.
117
107
        :param info: More information for this entry. Registry.get_info()
118
108
                can be used to get this information. Registry treats this as an
133
123
        """Register a new object to be loaded on request.
134
124
 
135
125
        :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
 
126
        :param member_name: The member of the module to return.  If empty or 
137
127
                None, get() will return the module itself.
138
128
        :param help: Help text for this entry. This may be a string or
139
129
                a callable.
140
 
        :param info: More information for this entry. Registry
 
130
        :param info: More information for this entry. Registry 
141
131
        :param override_existing: If True, replace the existing object
142
132
                with the new one. If False, if there is already something
143
133
                registered with the same key, raise a KeyError
157
147
        """Return the object register()'ed to the given key.
158
148
 
159
149
        May raise ImportError if the object was registered lazily and
160
 
        there are any problems, or AttributeError if the module does not
 
150
        there are any problems, or AttributeError if the module does not 
161
151
        have the supplied member.
162
152
 
163
153
        :param key: The key to obtain the object for. If no object has been
172
162
        """
173
163
        return self._dict[self._get_key_or_default(key)].get_obj()
174
164
 
175
 
    def get_prefix(self, fullname):
176
 
        """Return an object whose key is a prefix of the supplied value.
177
 
 
178
 
        :fullname: The name to find a prefix for
179
 
        :return: a tuple of (object, remainder), where the remainder is the
180
 
            portion of the name that did not match the key.
181
 
        """
182
 
        for key in self.keys():
183
 
            if fullname.startswith(key):
184
 
                return self.get(key), fullname[len(key):]
185
 
 
186
165
    def _get_key_or_default(self, key=None):
187
166
        """Return either 'key' or the default key if key is None"""
188
167
        if key is not None:
221
200
        for key, getter in self._dict.iteritems():
222
201
            yield key, getter.get_obj()
223
202
 
224
 
    def items(self):
225
 
        return sorted(self.iteritems())
226
 
 
227
203
    def _set_default_key(self, key):
228
204
        if not self._dict.has_key(key):
229
205
            raise KeyError('No object registered under key %s.' % key)
236
212
    default_key = property(_get_default_key, _set_default_key,
237
213
                            doc="Current value of the default key."
238
214
                                " Can be set to any existing key.")
239
 
 
240
 
 
241
 
class FormatRegistry(Registry):
242
 
    """Registry specialised for handling formats."""
243
 
 
244
 
    def __init__(self, other_registry=None):
245
 
        Registry.__init__(self)
246
 
        self._other_registry = other_registry
247
 
 
248
 
    def register_lazy(self, key, module_name, member_name,
249
 
                      help=None, info=None,
250
 
                      override_existing=False):
251
 
        # Overridden to allow capturing registrations to two seperate
252
 
        # registries in a single call.
253
 
        Registry.register_lazy(self, key, module_name, member_name,
254
 
                help=help, info=info, override_existing=override_existing)
255
 
        if self._other_registry is not None:
256
 
            self._other_registry.register_lazy(key, module_name, member_name,
257
 
                help=help, info=info, override_existing=override_existing)
258
 
 
259
 
    def get(self, format_string):
260
 
        r = Registry.get(self, format_string)
261
 
        if callable(r):
262
 
            r = r()
263
 
        return r
264
 
 
265