-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.py
70 lines (51 loc) · 1.94 KB
/
new.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
import os
import re
from pathlib import Path
from shutil import copyfile
BOILERPLATE_DIRECTORY = Path("./bp/")
DAY_PATTERN = re.compile(r"(\d{4}).(\d{2}).?(\d)?")
class BoilerplateException(Exception):
pass
def create_day_directory(year, day):
directory = Path() / year / day
if not directory.exists():
os.makedirs(directory)
return directory
def create_source_file(folder, lang, part="1", force=False):
if part != "1":
boilerplate = folder / f"p{int(part) - 1}.{lang}"
else:
boilerplate = BOILERPLATE_DIRECTORY / f"bp.{lang}"
destination = folder / f"p{part}.{lang}"
if destination.exists() and not force:
raise BoilerplateException(
"Can not copy to a file that already exists: {}".format(destination)
)
elif not boilerplate.exists():
raise BoilerplateException("No .{} boilerplate file exists".format(lang))
copyfile(boilerplate, destination)
def create_empty_files(folder, input=False):
if input:
(folder / "input.txt").touch()
(folder / "challenge.txt").touch()
def main(args):
year, day, part = DAY_PATTERN.match(args.day).groups()
dest_folder = create_day_directory(year, day)
create_source_file(dest_folder, args.lang, part or "1", args.force)
create_empty_files(dest_folder, args.input)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Create a new empty day.")
parser.add_argument("day", help='Day to make a new directory for; e.g. "2020.06"')
parser.add_argument("lang", help="Corresponds to a file extension in ./bp/")
parser.add_argument(
"-i", "--input", action="store_true", help="Add empty input file too"
)
parser.add_argument(
"-f", "--force", action="store_true", help="Force copying of boilerplate file"
)
args = parser.parse_args()
try:
main(args)
except BoilerplateException as e:
print("Error:", e)