-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplateGenerator.py
52 lines (44 loc) · 1.69 KB
/
templateGenerator.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
"""
File: templateGenerator.py
Authors: Matthew Frances, Nicholas Conn
Date: 11-30-2021
Description: Create files for each puzzle according to a template
"""
import os
import sys
from distutils.dir_util import copy_tree
LANGUAGE = "python"
PATH = "./solutions/day"
KEY = "{day}"
if not sys.argv[1]:
print("try running with 'python3 ./templateGenerator.py <day>")
sys.exit()
else:
CURRENT_DAY = f"{int(sys.argv[1]):02d}"
def generate_directory():
#Check if valid templates exist
if(os.path.exists("./.templates")):
#copy template to solutions folder
if not os.path.exists(f"./solutions/day{CURRENT_DAY}"):
copy_tree("./.templates", "./solutions")
#rename templated day to current day
os.rename(PATH+"{day}", f"{PATH}{CURRENT_DAY}")
PY_PATH = PATH+f"{CURRENT_DAY}{LANGUAGE}\day"
os.rename(f"./solutions/day{CURRENT_DAY}/python/day{{day}}.py",f"./solutions/day{CURRENT_DAY}/python/day{CURRENT_DAY}.py")
#open file in directory, and replace '{day}' with current day
with open(f"./solutions/day{CURRENT_DAY}/python/day{CURRENT_DAY}.py") as r:
#read into string
template = r.read()
#replace with current day
template = template.replace(KEY, str(CURRENT_DAY))
#clear and rewrite with proper date
with open(f"./solutions/day{CURRENT_DAY}/python/day{CURRENT_DAY}.py", 'w') as f:
f.write(template)
else:
print("Cannot overwrite previous day")
return
else:
print("No template file found")
return
if __name__ == '__main__':
generate_directory()