summaryrefslogtreecommitdiff
path: root/isort/identify.py
blob: 3e0779a0654e9665d34e89203c52d3ecaa05a087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
""""""
from pathlib import Path
from typing import Iterator, NamedTuple, Optional, TextIO

from isort.parse import _normalize_line, _strip_syntax, skip_line

from .comments import parse as parse_comments
from .settings import DEFAULT_CONFIG, Config


def import_type(line: str, config: Config = DEFAULT_CONFIG) -> Optional[str]:
    """If the current line is an import line it will return its type (from or straight)"""
    if line.lstrip().startswith(("import ", "cimport ")):
        return "straight"
    if line.lstrip().startswith("from "):
        return "from"
    return None


class IdentifiedImport(NamedTuple):
    line_number: int
    indented: bool
    module: str
    attribute: Optional[str] = None
    alias: Optional[str] = None
    cimport: bool = False
    file_path: Optional[Path] = None


def imports(
    input_stream: TextIO, config: Config = DEFAULT_CONFIG, file_path: Optional[Path] = None
) -> Iterator[IdentifiedImport]:
    """Parses a python file taking out and categorizing imports."""
    in_quote = ""

    indexed_input = enumerate(input_stream)
    for index, line in indexed_input:
        (skipping_line, in_quote) = skip_line(
            line, in_quote=in_quote, index=index, section_comments=config.section_comments
        )

        if skipping_line:
            continue

        line, *end_of_line_comment = line.split("#", 1)
        indented = line.startswith(" ") or line.startswith("\n")
        statements = [line.strip() for line in line.split(";")]
        if end_of_line_comment:
            statements[-1] = f"{statements[-1]}#{end_of_line_comment[0]}"

        for statement in statements:
            line, raw_line = _normalize_line(statement)
            type_of_import = import_type(line, config) or ""
            if not type_of_import:
                continue

            import_string, _ = parse_comments(line)
            normalized_import_string = (
                import_string.replace("import(", "import (").replace("\\", " ").replace("\n", " ")
            )
            cimports: bool = (
                " cimport " in normalized_import_string
                or normalized_import_string.startswith("cimport")
            )

            if "(" in line.split("#", 1)[0]:
                while not line.split("#")[0].strip().endswith(")"):
                    try:
                        index, next_line = next(indexed_input)
                    except StopIteration:
                        break

                    line, _ = parse_comments(next_line)
                    import_string += "\n" + line
            else:
                while line.strip().endswith("\\"):
                    index, next_line = next(indexed_input)
                    line, _ = parse_comments(next_line)

                    # Still need to check for parentheses after an escaped line
                    if "(" in line.split("#")[0] and ")" not in line.split("#")[0]:
                        import_string += "\n" + line

                        while not line.split("#")[0].strip().endswith(")"):
                            try:
                                index, next_line = next(indexed_input)
                            except StopIteration:
                                break
                            line, _ = parse_comments(next_line)
                            import_string += "\n" + line

                    if import_string.strip().endswith(
                        (" import", " cimport")
                    ) or line.strip().startswith(("import ", "cimport ")):
                        import_string += "\n" + line
                    else:
                        import_string = import_string.rstrip().rstrip("\\") + " " + line.lstrip()

            if type_of_import == "from":
                import_string = (
                    import_string.replace("import(", "import (")
                    .replace("\\", " ")
                    .replace("\n", " ")
                )
                parts = import_string.split(" cimport " if cimports else " import ")

                from_import = parts[0].split(" ")
                import_string = (" cimport " if cimports else " import ").join(
                    [from_import[0] + " " + "".join(from_import[1:])] + parts[1:]
                )

            just_imports = [
                item.replace("{|", "{ ").replace("|}", " }")
                for item in _strip_syntax(import_string).split()
            ]

            direct_imports = just_imports[1:]
            top_level_module = ""
            if "as" in just_imports and (just_imports.index("as") + 1) < len(just_imports):
                while "as" in just_imports:
                    attribute = None
                    as_index = just_imports.index("as")
                    if type_of_import == "from":
                        attribute = just_imports[as_index - 1]
                        top_level_module = just_imports[0]
                        module = top_level_module + "." + attribute
                        alias = just_imports[as_index + 1]
                        direct_imports.remove(attribute)
                        direct_imports.remove(alias)
                        direct_imports.remove("as")
                        just_imports[1:] = direct_imports
                        if attribute == alias and config.remove_redundant_aliases:
                            pass
                        else:
                            yield IdentifiedImport(
                                index,
                                indented,
                                top_level_module,
                                attribute,
                                alias=alias,
                                cimport=cimports,
                                file_path=file_path,
                            )

                    else:
                        module = just_imports[as_index - 1]
                        alias = just_imports[as_index + 1]
                        if not (module == alias and config.remove_redundant_aliases):
                            yield IdentifiedImport(
                                index,
                                indented,
                                module,
                                alias,
                                cimport=cimports,
                                file_path=file_path,
                            )

            else:
                if type_of_import == "from":
                    module = just_imports.pop(0)
                    for attribute in just_imports:
                        yield IdentifiedImport(
                            index,
                            indented,
                            module,
                            attribute,
                            file_path=file_path,
                        )
                else:
                    for module in just_imports:
                        yield IdentifiedImport(
                            index,
                            indented,
                            module,
                            cimport=cimports,
                            file_path=file_path,
                        )