-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_lines.py
58 lines (44 loc) · 1.55 KB
/
sort_lines.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
from __future__ import annotations
import argparse
from collections.abc import Iterator
from collections.abc import Sequence
def sort_lines(lines: Sequence[str]) -> Iterator[str]:
sorting = False
indentation: str | None = None
to_sort: list[str] = []
for line in lines:
if sorting:
if indentation is None: # first line
indentation = line.removesuffix(line.lstrip())
to_sort.append(line)
continue
elif line.strip() and line.startswith(indentation): # line to sort
to_sort.append(line)
continue
else: # sorting has ended
yield from sorted(to_sort)
sorting = False
if '# pragma: alphabetize' in line: # start sorting
sorting = True
to_sort = []
indentation = None # not known yet
yield line
else:
if sorting: # file ended during sorting
yield from sorted(to_sort)
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
args = parser.parse_args(argv)
ret = 0
for filename in args.filenames:
with open(filename) as f:
src_lines = f.readlines()
sorted_lines = list(sort_lines(src_lines))
if sorted_lines != src_lines:
with open(filename, 'w') as f:
f.writelines(sorted_lines)
ret |= 1
return ret
if __name__ == '__main__':
raise SystemExit(main())