-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclassify_imports.py
328 lines (263 loc) · 9.1 KB
/
classify_imports.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
from __future__ import annotations
import ast
import collections
import functools
import operator
import os.path
import stat
import sys
from collections.abc import Generator
from collections.abc import Iterable
from typing import Callable
from typing import NamedTuple
class Classified:
FUTURE = 'FUTURE'
BUILTIN = 'BUILTIN'
THIRD_PARTY = 'THIRD_PARTY'
APPLICATION = 'APPLICATION'
order = (FUTURE, BUILTIN, THIRD_PARTY, APPLICATION)
_STATIC_CLASSIFICATIONS = {
'__future__': Classified.FUTURE,
'__main__': Classified.APPLICATION,
# force distutils to be "third party" after being gobbled by setuptools
'distutils': Classified.THIRD_PARTY,
# relative imports: `from .foo import bar`
'': Classified.APPLICATION,
}
class Settings(NamedTuple):
application_directories: tuple[str, ...] = ('.',)
unclassifiable_application_modules: frozenset[str] = frozenset()
def _path_key(path: str) -> tuple[str, tuple[int, int]]:
path = path or '.' # '' in sys.path is the current directory
# os.path.samestat uses (st_ino, st_dev) to determine equality
st = os.stat(path)
return path, (st.st_ino, st.st_dev)
def _find_local(path: tuple[str, ...], base: str) -> bool:
for p in path:
p_dir = os.path.join(p, base)
try:
stat_dir = os.lstat(p_dir)
except OSError:
pass
else:
if stat.S_ISDIR(stat_dir.st_mode) and os.listdir(p_dir):
return True
try:
stat_file = os.lstat(os.path.join(p, f'{base}.py'))
except OSError:
pass
else:
return stat.S_ISREG(stat_file.st_mode)
else:
return False
if sys.version_info >= (3, 10): # pragma: >=3.10 cover
@functools.cache
def _get_app(app_dirs: tuple[str, ...]) -> tuple[str, ...]:
app_dirs_ret = []
filtered_stats = set()
for p in app_dirs:
try:
p, key = _path_key(p)
except OSError:
continue
else:
if key not in filtered_stats:
app_dirs_ret.append(p)
filtered_stats.add(key)
return tuple(app_dirs_ret)
@functools.cache
def classify_base(base: str, settings: Settings = Settings()) -> str:
try:
return _STATIC_CLASSIFICATIONS[base]
except KeyError:
pass
if base in sys.stdlib_module_names:
return Classified.BUILTIN
elif (
base in settings.unclassifiable_application_modules or
_find_local(_get_app(settings.application_directories), base)
):
return Classified.APPLICATION
else:
return Classified.THIRD_PARTY
else: # pragma: <3.10 cover
import importlib.machinery
_BUILTIN_MODS = frozenset(sys.builtin_module_names)
@functools.cache
def _get_path(
sys_path: tuple[str, ...],
app_dirs: tuple[str, ...],
pythonpath: str | None,
) -> tuple[Callable[[str], object | None], tuple[str, ...]]:
app_dirs_ret = []
filtered_stats = set()
for p in app_dirs:
try:
p, key = _path_key(p)
except OSError:
continue
else:
if key not in filtered_stats:
app_dirs_ret.append(p)
filtered_stats.add(key)
if pythonpath: # subtract out pythonpath from sys.path
for p in pythonpath.split(os.pathsep):
try:
filtered_stats.add(_path_key(p)[1])
except OSError:
pass
sys_path_ret = []
for p in sys_path:
# subtract out site-packages
if p.rstrip('/\\').endswith('-packages'):
continue
try:
p, key = _path_key(p)
except OSError:
continue
else:
if key not in filtered_stats:
sys_path_ret.append(p)
filtered_stats.add(key)
finder = functools.partial(
importlib.machinery.PathFinder.find_spec,
path=sys_path_ret,
)
return finder, tuple(app_dirs_ret)
@functools.cache
def classify_base(base: str, settings: Settings = Settings()) -> str:
try:
return _STATIC_CLASSIFICATIONS[base]
except KeyError:
pass
if base in settings.unclassifiable_application_modules:
return Classified.APPLICATION
elif base in _BUILTIN_MODS:
return Classified.BUILTIN
find_stdlib, app = _get_path(
tuple(sys.path),
settings.application_directories,
os.environ.get('PYTHONPATH'),
)
if _find_local(app, base):
return Classified.APPLICATION
elif find_stdlib(base) is not None:
return Classified.BUILTIN
else:
return Classified.THIRD_PARTY
def _ast_alias_to_s(node: ast.alias) -> str:
if node.asname:
return f'{node.name} as {node.asname}'
else:
return node.name
class ImportKey(NamedTuple):
module: str
asname: str
class Import:
def __init__(self, node: ast.Import) -> None:
self.node = node
self.is_multiple = len(node.names) > 1
@property
def module(self) -> str:
return self.node.names[0].name
@property
def module_base(self) -> str:
return self.module.partition('.')[0]
@functools.cached_property
def key(self) -> ImportKey:
alias = self.node.names[0]
return ImportKey(alias.name, alias.asname or '')
def __hash__(self) -> int:
return hash(self.key)
def __eq__(self, other: object) -> bool:
return isinstance(other, Import) and self.key == other.key
@property
def sort_key(self) -> tuple[str, str, str, str, str]:
name, asname = self.key
return ('0', name.lower(), asname.lower(), name, asname)
def split(self) -> Generator[Import]:
if not self.is_multiple:
yield self
else:
for name in self.node.names:
yield type(self)(ast.Import(names=[name]))
def __str__(self) -> str:
assert not self.is_multiple
return f'import {_ast_alias_to_s(self.node.names[0])}\n'
def __repr__(self) -> str:
return f'import_obj_from_str({str(self)!r})'
class ImportFromKey(NamedTuple):
module: str
symbol: str
asname: str
class ImportFrom:
def __init__(self, node: ast.ImportFrom) -> None:
self.node = node
self.is_multiple = len(node.names) > 1
@property
def module(self) -> str:
level = '.' * self.node.level # local imports
mod = self.node.module or '' # from . import bar makes module `None`
return f'{level}{mod}'
@property
def module_base(self) -> str:
return self.module.partition('.')[0]
@functools.cached_property
def key(self) -> ImportFromKey:
alias = self.node.names[0]
return ImportFromKey(self.module, alias.name, alias.asname or '')
def __hash__(self) -> int:
return hash(self.key)
def __eq__(self, other: object) -> bool:
return isinstance(other, ImportFrom) and self.key == other.key
@property
def sort_key(self) -> tuple[str, str, str, str, str, str, str]:
mod, name, asname = self.key
return (
'1',
mod.lower(), name.lower(), asname.lower(),
mod, name, asname,
)
def split(self) -> Generator[ImportFrom]:
if not self.is_multiple:
yield self
else:
for name in self.node.names:
node = ast.ImportFrom(
module=self.node.module,
names=[name],
level=self.node.level,
)
yield type(self)(node)
def __str__(self) -> str:
assert not self.is_multiple
return (
f'from {self.module} '
f'import {_ast_alias_to_s(self.node.names[0])}\n'
)
def __repr__(self) -> str:
return f'import_obj_from_str({str(self)!r})'
_import_type = {ast.Import: Import, ast.ImportFrom: ImportFrom}
@functools.cache
def import_obj_from_str(s: str) -> Import | ImportFrom:
node = ast.parse(s, mode='single').body[0]
return _import_type[type(node)](node)
def sort(
imports: Iterable[Import | ImportFrom],
settings: Settings = Settings(),
) -> tuple[tuple[Import | ImportFrom, ...], ...]:
# Partition the imports
imports_partitioned = collections.defaultdict(list)
for obj in imports:
tp = classify_base(obj.module_base, settings=settings)
if tp is Classified.FUTURE and isinstance(obj, Import):
tp = Classified.BUILTIN
imports_partitioned[tp].append(obj)
# sort each of the segments
sortkey = operator.attrgetter('sort_key')
for val in imports_partitioned.values():
val.sort(key=sortkey)
return tuple(
tuple(imports_partitioned[key])
for key in Classified.order if key in imports_partitioned
)