/
utils.py
344 lines (275 loc) · 9.27 KB
/
utils.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import contextlib
import difflib
import importlib
import importlib.util
import io
import itertools
import logging
import os
import pathlib
import re
import stat
import sys
import tokenize
import typing
from contextlib import suppress
import click
log = logging.getLogger(__name__)
PREFIX_RE = re.compile(r"^\s+")
def _get_module_path(module_name: str) -> str:
spec: typing.Optional[importlib.machinery.ModuleSpec] = None
with suppress(AttributeError, ModuleNotFoundError):
spec = importlib.util.find_spec(module_name)
if spec and spec.origin:
return os.path.normpath(spec.origin.lower())
else:
return ""
def _is_py_path(module_path: str) -> bool:
modules = [
i
for i in [
"typing", # usually links to system python
"re", # usually links to virtualenv
"math", # is dynamic lib so path is diff
"click", # sometimes site-packages is directly inside venv so get parent folder
]
if i not in sys.builtin_module_names
]
return any(
module_path.startswith(
str(pathlib.Path(importlib.import_module(i).__file__).parent)
.lower()
.split("site-packages")[0]
)
for i in modules
)
def _is_sitepackages_path(module_path: str) -> bool:
return "site-packages" in module_path.split(os.sep)
def is_std_lib(module_name: str) -> bool:
if not module_name:
return False
if module_name in sys.builtin_module_names:
return True
module_path = _get_module_path(module_name)
return not _is_sitepackages_path(module_path) and _is_py_path(module_path)
def is_site_package(module_name: str) -> bool:
if not module_name:
return False
module_path = _get_module_path(module_name)
return _is_sitepackages_path(module_path) and _is_py_path(module_path)
def force_text(data: typing.Union[bytes, str]) -> str:
if isinstance(data, bytes):
return data.decode("utf-8")
return data
def force_bytes(data: typing.Union[bytes, str]) -> bytes:
if isinstance(data, str):
return data.encode("utf-8")
return data
def largest_prefix(strings: typing.Iterable[str]) -> str:
return "".join(
c[0]
for c in itertools.takewhile(lambda chars: len(set(chars)) == 1, zip(*strings))
)
T = typing.TypeVar("T")
def takeafter(
predicate: typing.Callable[[T], typing.Any], iterable: typing.Iterable[T]
) -> typing.Iterator[T]:
found = False
for i in iterable:
if not found and predicate(i):
found = True
if found:
yield i
def list_set(iterable: typing.Iterable[T]) -> typing.List[T]:
items: typing.List[T] = []
for i in iterable:
if i not in items:
items.append(i)
return items
class TextPrefixSpex(typing.NamedTuple):
text: str
prefix: str
def get_number_clusters(numbers: typing.List[int]) -> typing.List[typing.List[int]]:
"""
Get the number clusters
::
>>> get_number_clusters([1, 2, 3, 5, 6, 8, 9])
[[1, 2, 3], [5, 6], [8, 9]]
"""
clusters = []
cluster = []
for i, number in enumerate(numbers):
previous_number = numbers[max(i - 1, 0)]
if previous_number <= number <= previous_number + 1:
cluster.append(number)
else:
clusters.append(cluster)
cluster = []
cluster.append(number)
if cluster:
clusters.append(cluster)
return clusters
def get_number_cluster_gaps(numbers: typing.List[int]) -> typing.List[typing.List[int]]:
"""
Get the gaps between number clusters
::
>>> get_number_cluster_gaps([1, 2, 3, 5, 6, 8, 9, 13, 14, 16])
[[4], [7], [10, 11, 12], [15]]
"""
clusters = get_number_clusters(numbers)
gaps = []
for i, cluster in enumerate(clusters[1:]):
previous_cluster = clusters[i]
gaps.append(list(range(previous_cluster[-1] + 1, cluster[0])))
return gaps
def remove_largest_whitespace_prefix(text: str) -> TextPrefixSpex:
lines = text.splitlines(keepends=True)
prefix = largest_prefix(
{next(iter(PREFIX_RE.findall(i.rstrip())), "") for i in lines if i.rstrip()}
)
if prefix:
l_prefix = len(prefix)
return TextPrefixSpex(
"".join(l[l_prefix:] if l.startswith(prefix) else l for l in lines), prefix
)
else:
return TextPrefixSpex(text, "")
def add_prefix_to_text(text: str, prefix: str) -> str:
if not prefix:
return text
return "".join(
prefix + l if l.rstrip() else l for l in text.splitlines(keepends=True)
)
def generate_diff(text1: str, text2: str, name: str, color: bool = True) -> str:
color_mapping: typing.Dict[str, str] = (
{
"@": "blue",
"-": "red",
"+": "green",
}
if color
else {}
)
return "\n".join(
click.style(i, fg=color_mapping[i[:1]]) if i[:1] in color_mapping else i
for i in difflib.unified_diff(
text1.splitlines(),
text2.splitlines(),
fromfile=f"original{os.sep}{name}",
tofile=f"importanized{os.sep}{name}",
lineterm="",
n=3,
)
)
def is_piped(
fd: typing.Union[typing.BinaryIO, typing.TextIO] = sys.stdin,
check_file_redirection: bool = True,
) -> bool:
with contextlib.suppress(Exception):
return (
# isatty catches cases when script is redirected from or to a file
# e.g. script < file
# e.g. script > file
(not fd.isatty() and check_file_redirection)
# S_ISFIFO specifically checks if script is being piped to or out of
# e.g. echo foo | script
# e.g. script | cat
or stat.S_ISFIFO(os.fstat(fd.fileno()).st_mode)
)
class OpenBytesIO(io.BytesIO):
def close(self) -> None:
"""
Dont close io buffer
"""
def read(self, size: int = -1) -> bytes:
self.seek(0)
return super().read(size)
class OpenStringIO(io.StringIO):
def close(self) -> None:
"""
Dont close io buffer
"""
def read(self, size: int = -1) -> str:
self.seek(0)
return super().read(size)
BasePath = pathlib.Path
BasePath = type(pathlib.Path()) # type: ignore
class StdPath(BasePath):
prefix: str = ""
encoding: str = "utf-8"
stdin: typing.BinaryIO = sys.stdin.buffer
stdout: typing.BinaryIO = sys.stdout.buffer
filein: typing.BinaryIO = None
fileout: typing.BinaryIO = None
def with_streams(
self,
stdin: typing.BinaryIO = None,
stdout: typing.BinaryIO = None,
filein: typing.BinaryIO = None,
fileout: typing.BinaryIO = None,
) -> "StdPath":
self.stdin = stdin or self.stdin
self.stdout = stdout or self.stdout
self.filein = filein or self.filein
self.fileout = fileout or self.fileout
return self
def open(
self,
mode: str = "r",
buffering: int = -1,
encoding: str = None,
errors: str = None,
newline: str = None,
) -> typing.IO[typing.Any]:
return (self.filein if "r" in mode else self.fileout) or super().open(
mode=mode,
buffering=buffering,
encoding=encoding,
errors=errors,
newline=newline,
)
def is_std_stream(self) -> bool:
return self.name == "-"
def is_file(self) -> bool:
return self.is_std_stream() or super().is_file()
def decode_pep263(self, data: bytes) -> typing.Tuple[str, str]:
encoding, lines = tokenize.detect_encoding(io.BytesIO(data).readline)
try:
return data.decode(encoding), encoding
except UnicodeDecodeError as e:
raise UnicodeDecodeError(
e.encoding,
e.object,
e.start,
e.end,
(
f"\n"
f"Cannot read Python file {self.name!r} "
f"with PEP263 encoding={self.encoding!r}. \n"
f"Make sure encoding comment is correct in the begining of the file. \n"
f"For example: \n"
f"# -*- coding: utf-8 -*-"
),
) from e
def read_text(self, encoding: str = None, errors: str = None) -> str:
if self.is_std_stream():
text, self.encoding = self.decode_pep263(self.stdin.read())
text_without_whitespace, self.prefix = remove_largest_whitespace_prefix(
text
)
if self.prefix:
log.debug(f"Stripping prefix {self.prefix!r} in {self}")
return text_without_whitespace
else:
text, self.encoding = self.decode_pep263(super().read_bytes())
return text
def write_text(self, data: str, encoding: str = None, errors: str = None) -> None:
if self.is_std_stream():
self.stdout.write(
add_prefix_to_text(data, self.prefix).encode(self.encoding)
)
self.stdout.flush()
else:
super().write_bytes(data.encode(encoding or self.encoding))