/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

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