/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: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
from __future__ import absolute_import
45
45
 
46
 
from .errors import BzrError, InternalBzrError
47
 
 
48
 
 
49
 
class ImportNameCollision(InternalBzrError):
50
 
 
51
 
    _fmt = ("Tried to import an object to the same name as"
52
 
            " an existing object. %(name)s")
53
 
 
54
 
    def __init__(self, name):
55
 
        BzrError.__init__(self)
56
 
        self.name = name
57
 
 
58
 
 
59
 
class IllegalUseOfScopeReplacer(InternalBzrError):
60
 
 
61
 
    _fmt = ("ScopeReplacer object %(name)r was used incorrectly:"
62
 
            " %(msg)s%(extra)s")
63
 
 
64
 
    def __init__(self, name, msg, extra=None):
65
 
        BzrError.__init__(self)
66
 
        self.name = name
67
 
        self.msg = msg
68
 
        if extra:
69
 
            self.extra = ': ' + str(extra)
70
 
        else:
71
 
            self.extra = ''
72
 
 
73
 
 
74
 
class InvalidImportLine(InternalBzrError):
75
 
 
76
 
    _fmt = "Not a valid import statement: %(msg)\n%(text)s"
77
 
 
78
 
    def __init__(self, text, msg):
79
 
        BzrError.__init__(self)
80
 
        self.text = text
81
 
        self.msg = msg
82
 
 
83
46
 
84
47
class ScopeReplacer(object):
85
48
    """A lazy object that will replace itself in the appropriate scope.
121
84
            scope = object.__getattribute__(self, '_scope')
122
85
            obj = factory(self, scope, name)
123
86
            if obj is self:
124
 
                raise IllegalUseOfScopeReplacer(
125
 
                    name, msg="Object tried"
126
 
                    " to replace itself, check it's not using its own scope.")
 
87
                raise errors.IllegalUseOfScopeReplacer(name, msg="Object tried"
 
88
                                                       " to replace itself, check it's not using its own scope.")
127
89
 
128
90
            # Check if another thread has jumped in while obj was generated.
129
91
            real_obj = object.__getattribute__(self, '_real_obj')
137
99
 
138
100
        # Raise if proxying is disabled as obj has already been generated.
139
101
        if not ScopeReplacer._should_proxy:
140
 
            raise IllegalUseOfScopeReplacer(
 
102
            raise errors.IllegalUseOfScopeReplacer(
141
103
                name, msg="Object already replaced, did you assign it"
142
104
                          " to another variable?")
143
105
        return real_obj
298
260
            elif line.startswith('from '):
299
261
                self._convert_from_str(line)
300
262
            else:
301
 
                raise InvalidImportLine(
302
 
                    line, "doesn't start with 'import ' or 'from '")
 
263
                raise errors.InvalidImportLine(line,
 
264
                                               "doesn't start with 'import ' or 'from '")
303
265
 
304
266
    def _convert_import_str(self, import_str):
305
267
        """This converts a import string into an import map.
324
286
                name = as_hunks[1].strip()
325
287
                module_path = as_hunks[0].strip().split('.')
326
288
                if name in self.imports:
327
 
                    raise ImportNameCollision(name)
 
289
                    raise errors.ImportNameCollision(name)
328
290
                if not module_path[0]:
329
291
                    raise ImportError(path)
330
292
                # No children available in 'import foo as bar'
383
345
            else:
384
346
                name = module = path
385
347
            if name in self.imports:
386
 
                raise ImportNameCollision(name)
 
348
                raise errors.ImportNameCollision(name)
387
349
            self.imports[name] = (from_module_path, module, {})
388
350
 
389
351
    def _canonicalize_import_text(self, text):
415
377
                else:
416
378
                    out.append(line.replace('(', '').replace(')', ''))
417
379
        if cur is not None:
418
 
            raise InvalidImportLine(cur, 'Unmatched parenthesis')
 
380
            raise errors.InvalidImportLine(cur, 'Unmatched parenthesis')
419
381
        return out
420
382
 
421
383
 
446
408
    # This is just a helper around ImportProcessor.lazy_import
447
409
    proc = ImportProcessor(lazy_import_class=lazy_import_class)
448
410
    return proc.lazy_import(scope, text)
 
411
 
 
412
 
 
413
# The only module that this module depends on is 'breezy.errors'. But it
 
414
# can actually be imported lazily, since we only need it if there is a
 
415
# problem.
 
416
 
 
417
lazy_import(globals(), """
 
418
from breezy import errors
 
419
""")