Skip to content

Commit b8e30a1

Browse files
committed
Added starter code generation from templates
1 parent a94fe4b commit b8e30a1

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
lines changed

gen_day.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2020 Andrew Krepps
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import os
25+
import sys
26+
27+
28+
def substitute_vars(line, var_map):
29+
result = line
30+
for var, value in var_map.items():
31+
result = result.replace(f"${{{var}}}", value)
32+
return result
33+
34+
35+
def gen_file_from_template(temp_path, out_path, var_map):
36+
with open(temp_path) as temp_f:
37+
with open(out_path, "w") as out_f:
38+
out_f.writelines((substitute_vars(line, var_map) for line in temp_f))
39+
40+
41+
def generate_code_for_day(day):
42+
day_file_name = f"day{day}.py"
43+
day_file_path = os.path.join("advent2020", day_file_name)
44+
test_file_name = f"test_{day_file_name}"
45+
test_file_path = os.path.join("test", test_file_name)
46+
if os.path.exists(day_file_path) or os.path.exists(test_file_path):
47+
raise RuntimeError(f"Files for day {day} already exist")
48+
49+
with open(os.path.join("templates", "license.template")) as license_f:
50+
var_map = {
51+
"license": license_f.read(),
52+
"day": str(day)
53+
}
54+
gen_file_from_template(os.path.join("templates", "day.template"), day_file_path, var_map)
55+
gen_file_from_template(os.path.join("templates", "test.template"), test_file_path, var_map)
56+
57+
58+
def gen_day_main(args):
59+
if len(args) == 0:
60+
raise RuntimeError("Must provide a day number")
61+
else:
62+
try:
63+
day = int(args[0])
64+
if day < 0:
65+
raise ValueError
66+
generate_code_for_day(day)
67+
except ValueError:
68+
raise RuntimeError("Day must be a valid positive number")
69+
70+
71+
if __name__ == "__main__":
72+
gen_day_main(sys.argv[1:])

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,5 @@ def advent2020_main(args):
7272
raise_day_input_error(day, max_day)
7373

7474

75-
if __name__ == '__main__':
75+
if __name__ == "__main__":
7676
advent2020_main(sys.argv[1:])

templates/day.template

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
${license}
2+
3+
4+
from . import util
5+
6+
7+
def get_part1_answer(lines):
8+
return None
9+
10+
11+
def get_part2_answer(lines):
12+
return None
13+
14+
15+
def run():
16+
lines = util.get_input_file_lines("day${day}.txt")
17+
print(f"The answer to part 1 is {get_part1_answer(lines)}")
18+
print(f"The answer to part 2 is {get_part2_answer(lines)}")

templates/license.template

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2020 Andrew Krepps
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.

templates/test.template

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
${license}
2+
3+
4+
import unittest
5+
6+
from advent2020.day${day} import get_part1_answer
7+
from advent2020.day${day} import get_part2_answer
8+
from advent2020.util import get_input_data_lines
9+
10+
11+
data = """
12+
13+
"""
14+
15+
16+
class Day${day}Test(unittest.TestCase):
17+
def test_day${day}(self):
18+
lines = get_input_data_lines(data)
19+
self.assertEqual(get_part1_answer(lines), None)
20+
self.assertEqual(get_part2_answer(lines), None)

0 commit comments

Comments
 (0)