/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/lazy_import.py

  • Committer: Jelmer Vernooij
  • Date: 2018-02-18 21:42:57 UTC
  • mto: This revision was merged to the branch mainline in revision 6859.
  • Revision ID: jelmer@jelmer.uk-20180218214257-jpevutp1wa30tz3v
Update TODO to reference Breezy, not Bazaar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
to inherit from them).
42
42
"""
43
43
 
44
 
from .errors import BzrError, InternalBzrError
45
 
 
46
 
 
47
 
class ImportNameCollision(InternalBzrError):
48
 
 
49
 
    _fmt = ("Tried to import an object to the same name as"
50
 
            " an existing object. %(name)s")
51
 
 
52
 
    def __init__(self, name):
53
 
        BzrError.__init__(self)
54
 
        self.name = name
55
 
 
56
 
 
57
 
class IllegalUseOfScopeReplacer(InternalBzrError):
58
 
 
59
 
    _fmt = ("ScopeReplacer object %(name)r was used incorrectly:"
60
 
            " %(msg)s%(extra)s")
61
 
 
62
 
    def __init__(self, name, msg, extra=None):
63
 
        BzrError.__init__(self)
64
 
        self.name = name
65
 
        self.msg = msg
66
 
        if extra:
67
 
            self.extra = ': ' + str(extra)
68
 
        else:
69
 
            self.extra = ''
70
 
 
71
 
 
72
 
class InvalidImportLine(InternalBzrError):
73
 
 
74
 
    _fmt = "Not a valid import statement: %(msg)\n%(text)s"
75
 
 
76
 
    def __init__(self, text, msg):
77
 
        BzrError.__init__(self)
78
 
        self.text = text
79
 
        self.msg = msg
 
44
from __future__ import absolute_import
80
45
 
81
46
 
82
47
class ScopeReplacer(object):
119
84
            scope = object.__getattribute__(self, '_scope')
120
85
            obj = factory(self, scope, name)
121
86
            if obj is self:
122
 
                raise IllegalUseOfScopeReplacer(
123
 
                    name, msg="Object tried"
 
87
                raise errors.IllegalUseOfScopeReplacer(name, msg="Object tried"
124
88
                    " to replace itself, check it's not using its own scope.")
125
89
 
126
90
            # Check if another thread has jumped in while obj was generated.
135
99
 
136
100
        # Raise if proxying is disabled as obj has already been generated.
137
101
        if not ScopeReplacer._should_proxy:
138
 
            raise IllegalUseOfScopeReplacer(
 
102
            raise errors.IllegalUseOfScopeReplacer(
139
103
                name, msg="Object already replaced, did you assign it"
140
104
                          " to another variable?")
141
105
        return real_obj
165
129
    ScopeReplacer._should_proxy = False
166
130
 
167
131
 
168
 
_builtin_import = __import__
169
 
 
170
 
 
171
132
class ImportReplacer(ScopeReplacer):
172
133
    """This is designed to replace only a portion of an import list.
173
134
 
201
162
        :param children: Children entries to be imported later.
202
163
            This should be a map of children specifications.
203
164
            ::
204
 
 
 
165
            
205
166
                {'foo':(['breezy', 'foo'], None,
206
167
                    {'bar':(['breezy', 'foo', 'bar'], None {})})
207
168
                }
234
195
        children = object.__getattribute__(self, '_import_replacer_children')
235
196
        member = object.__getattribute__(self, '_member')
236
197
        module_path = object.__getattribute__(self, '_module_path')
237
 
        name = '.'.join(module_path)
 
198
        module_python_path = '.'.join(module_path)
238
199
        if member is not None:
239
 
            module = _builtin_import(name, scope, scope, [member], level=0)
 
200
            module = __import__(module_python_path, scope, scope, [member], level=0)
240
201
            return getattr(module, member)
241
202
        else:
242
 
            module = _builtin_import(name, scope, scope, [], level=0)
 
203
            module = __import__(module_python_path, scope, scope, [], level=0)
243
204
            for path in module_path[1:]:
244
205
                module = getattr(module, path)
245
206
 
296
257
            elif line.startswith('from '):
297
258
                self._convert_from_str(line)
298
259
            else:
299
 
                raise InvalidImportLine(
300
 
                    line, "doesn't start with 'import ' or 'from '")
 
260
                raise errors.InvalidImportLine(line,
 
261
                    "doesn't start with 'import ' or 'from '")
301
262
 
302
263
    def _convert_import_str(self, import_str):
303
264
        """This converts a import string into an import map.
322
283
                name = as_hunks[1].strip()
323
284
                module_path = as_hunks[0].strip().split('.')
324
285
                if name in self.imports:
325
 
                    raise ImportNameCollision(name)
 
286
                    raise errors.ImportNameCollision(name)
326
287
                if not module_path[0]:
327
288
                    raise ImportError(path)
328
289
                # No children available in 'import foo as bar'
381
342
            else:
382
343
                name = module = path
383
344
            if name in self.imports:
384
 
                raise ImportNameCollision(name)
 
345
                raise errors.ImportNameCollision(name)
385
346
            self.imports[name] = (from_module_path, module, {})
386
347
 
387
348
    def _canonicalize_import_text(self, text):
392
353
        """
393
354
        out = []
394
355
        cur = None
 
356
        continuing = False
395
357
 
396
358
        for line in text.split('\n'):
397
359
            line = line.strip()
413
375
                else:
414
376
                    out.append(line.replace('(', '').replace(')', ''))
415
377
        if cur is not None:
416
 
            raise InvalidImportLine(cur, 'Unmatched parenthesis')
 
378
            raise errors.InvalidImportLine(cur, 'Unmatched parenthesis')
417
379
        return out
418
380
 
419
381
 
444
406
    # This is just a helper around ImportProcessor.lazy_import
445
407
    proc = ImportProcessor(lazy_import_class=lazy_import_class)
446
408
    return proc.lazy_import(scope, text)
 
409
 
 
410
 
 
411
# The only module that this module depends on is 'breezy.errors'. But it
 
412
# can actually be imported lazily, since we only need it if there is a
 
413
# problem.
 
414
 
 
415
lazy_import(globals(), """
 
416
from breezy import errors
 
417
""")