-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaffold.py
executable file
·76 lines (65 loc) · 2.14 KB
/
scaffold.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
#!/usr/bin/env python
import argparse
import os
import shutil
import sys
from pathlib import Path
from jinja2 import Environment, FileSystemLoader
CHALLENGE_URL = "https://adventofcode.com/{year}/day/{day}"
env = Environment(loader=FileSystemLoader("templates"), keep_trailing_newline=True)
header_template = env.get_template("header.j2")
prog_template = env.get_template("prog.py.j2")
test_template = env.get_template("tests.py.j2")
parser = argparse.ArgumentParser()
parser.add_argument("year", type=int)
parser.add_argument("day", type=int)
parser.add_argument("--force", "-f", action="store_true")
parser.add_argument("--header", "-H", action="store_true")
args = parser.parse_args()
if args.header:
for part in (1, 2):
context = {
"year": args.year,
"day": args.day,
"part": part,
"url": CHALLENGE_URL.format(year=args.year, day=args.day),
"fragment": "#part2" if part == 2 else "",
}
header = header_template.render(context)
print(header)
print()
sys.exit(0)
padded_day = f"{args.day:02d}"
target_dir: Path = Path(str(args.year)) / padded_day
# Create directory
if os.path.isdir(target_dir):
if args.force:
print("Removing existing directory.")
shutil.rmtree(target_dir)
else:
print("Directory already exists, aborting.")
sys.exit(1)
# Generate scaffold
os.makedirs(target_dir)
input_path = target_dir / "input"
input_path.touch()
for part in (1, 2):
demo_input = f"demo{part}"
(target_dir / demo_input).touch()
context = {
"year": args.year,
"day": args.day,
"part": part,
"demo_input": demo_input,
"url": CHALLENGE_URL.format(year=args.year, day=args.day),
"fragment": "#part2" if part == 2 else "",
}
prog_path = target_dir / f"part{part}.py"
with open(prog_path, "w") as fp:
fp.write(prog_template.render(context))
os.chmod(prog_path, 0o755)
test_path = target_dir / "tests.py"
with open(test_path, "w") as fp:
fp.write(test_template.render(context))
os.chmod(test_path, 0o755)
print("Scaffold successfully generated in:", target_dir)