-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.py
executable file
·72 lines (50 loc) · 1.68 KB
/
manager.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
#!/usr/bin/env python3
from typing import Final, Callable, Literal
from sys import argv, stderr
is_sorted_str: Final[
Callable[[list[str], Callable[[str, str], bool]], bool]
] = lambda l, cmp: all(cmp(l[i], l[i + 1]) for i in range(len(l) - 1))
def eprint(*values: object, **kwargs: object):
'''prints to `stderr`'''
return print(*values, file=stderr, **kwargs)
def print_help():
HELP_TXT: Final = ''
print(HELP_TXT)
return HELP_TXT
# stable ordering.
# this is intentionally case-sensitive, to break ties
cmp_caseless: Final[
Callable[[str, str], Literal[0, -1, 1]]
] = lambda a, b: 0 if a == b else (-1 if a.lower() < b.lower() else 1)
'''compare strings case-insensitive (except if equal)'''
def filter_main_files():
'''get an iterator of files whose stem is "main"'''
from os import listdir, path as os_path
from pathlib import Path
# order matters: 1st check stem (fast), then syscall (slow)
return filter(lambda s: Path(s).stem == 'main' and os_path.isfile(s), listdir())
def main(*args: str):
from sys import exit as sys_exit # avoid collision with global `exit`
if len(args) < 1:
eprint('no args')
return print_help()
match args[0]:
case 'help': return print_help()
case 'check':
for m in filter_main_files():
with open(m) as f:
if not is_sorted_str(f.read().splitlines(), lambda a, b: cmp_caseless(a, b) == 1):
eprint(f'{m} is not sorted')
case 'add':
pass
case 'sort':
for m in filter_main_files():
with open(m, 'r') as f:
lns = f.read().splitlines()
lns.sort(key=cmp_caseless)
# to-do
case subcmd:
eprint(f'unrecognized subcmd: "{subcmd}"\nuse "help"')
sys_exit(1)
if '__main__' == __name__:
main(*argv[1:])