-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.py
executable file
·73 lines (50 loc) · 1.73 KB
/
add.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
#!/usr/bin/python
import os
from argparse import ArgumentParser
from pathlib import Path
from sys import argv
from _common import DEFAULT_BASEDIR, PATH_GAMES
from convert import convert
# todo could simplify whole basedir thing
# by just getting it from the filename to add
# and doing all at once in the other scripts
# also getting suffix from metadata
# how has this simple download script become so complicated
# oh also remember to strip leading / from basedir in sh scripts
def get_parser() -> ArgumentParser:
parser = ArgumentParser()
parser.add_argument("-b", "--basedir", default=DEFAULT_BASEDIR, type=Path)
parser.add_argument("target", nargs="+", type=Path)
return parser
def load(games_path: Path) -> set[str]:
with open(games_path) as file:
games = set(file.readlines())
return games
def save(games_path: Path, to_add: set[str]):
with open(games_path, "a") as file:
file.writelines(to_add)
def add(games_path: Path, targets: list[str]) -> bool:
targets = set(t.stem + "\n" for t in targets)
games_path.touch()
games = load(games_path)
already_added = games.intersection(targets)
to_add = targets - games
if already_added:
print("".join(f"Already in there: {g}" for g in already_added), end="")
if to_add:
print("".join(f"Adding: {g}" for g in to_add), end="")
save(games_path, to_add)
return True
else:
return False
def main():
parser = get_parser()
args = parser.parse_args()
basedir = args.basedir
workdir = Path(__file__).parent
games_path = workdir / basedir / PATH_GAMES
targets = args.target
if add(games_path, targets):
convert(basedir)
if __name__ == "__main__":
main()