1
# -*- coding: utf8 - *-
2 2
"""Exceptions raised from the Cihai library."""
3

4 2
from __future__ import absolute_import, unicode_literals
5

6

7 2
class CihaiException(Exception):
8
    """Base Cihai Exception class."""
9

10

11 2
class ImportStringError(ImportError, CihaiException):
12
    """
13
    Provides information about a failed :func:`import_string` attempt.
14

15
    Notes
16
    -----
17
    This is from werkzeug.utils c769200 on May 23, LICENSE BSD.
18
    https://github.com/pallets/werkzeug
19

20
    Changes:
21
    - Deferred load import import_string from cihai.util
22
    - Format with black
23
    """
24

25
    #: String in dotted notation that failed to be imported.
26 2
    import_name = None
27
    #: Wrapped exception.
28 2
    exception = None
29

30 2
    def __init__(self, import_name, exception):
31 2
        from .utils import import_string
32

33 2
        self.import_name = import_name
34 2
        self.exception = exception
35

36 2
        msg = (
37
            'import_string() failed for %r. Possible reasons are:\n\n'
38
            '- missing __init__.py in a package;\n'
39
            '- package or module path not included in sys.path;\n'
40
            '- duplicated package or module name taking precedence in '
41
            'sys.path;\n'
42
            '- missing module, class, function or variable;\n\n'
43
            'Debugged import:\n\n%s\n\n'
44
            'Original exception:\n\n%s: %s'
45
        )
46

47 2
        name = ''
48 2
        tracked = []
49 2
        for part in import_name.replace(':', '.').split('.'):
50 2
            name += (name and '.') + part
51 2
            imported = import_string(name, silent=True)
52 2
            if imported:
53 2
                tracked.append((name, getattr(imported, '__file__', None)))
54
            else:
55 2
                track = ['- %r found in %r.' % (n, i) for n, i in tracked]
56 2
                track.append('- %r not found.' % name)
57 2
                msg = msg % (
58
                    import_name,
59
                    '\n'.join(track),
60
                    exception.__class__.__name__,
61
                    str(exception),
62
                )
63 2
                break
64

65 2
        ImportError.__init__(self, msg)
66

67
    def __repr__(self):
68
        return '<%s(%r, %r)>' % (
69
            self.__class__.__name__,
70
            self.import_name,
71
            self.exception,
72
        )

Read our documentation on viewing source code .

Loading