Repository navigation
Expand file tree
/
Copy pathcreate_python_day.py
More file actions
executable file
·133 lines (106 loc) · 4.14 KB
/
Copy pathcreate_python_day.py
File metadata and controls
executable file
·133 lines (106 loc) · 4.14 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python
import argparse
from datetime import datetime
import os
from jinja2 import Environment, FileSystemLoader, Template
def argparse_setup():
parser = argparse.ArgumentParser(
description="Create a python day code file out of a Jinja template"
)
parser.add_argument(
'-y', '--year',
default=datetime.now().year,
type=int,
help='The year of the AoC python file to create'
)
parser.add_argument(
'-d', '--day',
default=datetime.now().day,
type=int,
help='The day of the AoC python file to create'
)
parser.add_argument(
'-t', '--test-file',
action='store_true',
help='Create empty test files (does nothing if they already exist)'
)
parser.add_argument(
'-n', '--no-action',
action='store_true',
help='Take no action (do a dry-run) (NOT IMPLEMENTED YET)'
)
return parser
def pad_day(day: int) -> str:
return str(day).rjust(2, '0')
def generate_test_input_files(year: int, day: int) -> None:
"""
Create empty test files that match the given year and date
:param year: The year of the file to create
:type year: int
:param day: The day of the file to create
:type day: int
"""
test_dir: str = f'src/test/_{year}'
print(f'Creating directory {test_dir}')
os.makedirs(test_dir, exist_ok=True)
# Create test files if they don't exist
# The actual input (e.g. day01_00.in)
test_file: str = f'{test_dir}/day{pad_day(day)}_{pad_day(0)}.in'
if os.path.isfile(test_file):
print(f'The test file {test_file} already exists')
else:
print(f'Creating test file {test_file}')
open(test_file, 'w', encoding='utf-8').close()
# The first sample input (e.g. day01_01.in)
test_file: str = f'{test_dir}/day{pad_day(day)}_{pad_day(1)}.in'
if os.path.isfile(test_file):
print(f'The test file {test_file} already exists')
else:
print(f'Creating test file {test_file}')
open(test_file, 'w', encoding='utf-8').close()
def generate_source_file(template, dir_: str, filename: str, year: int, day: int) -> None:
"""
Create the specified python source file out of a Jinja2 template
Create leading directories if they don't exist
This can be used for both the python source file and the corresponding unit
test (when called separately with different args)
:param template: A Jinja2 template object
:type template: Template
:param dir_: Where the file should be created, excluding the year
:type dir_: str
:param filename: The target file "base" name, excluding any year/day values
:type filename: str
:param year: The year of the file to create
:type year: int
:param day: The day of the file to create
:type day: int
"""
# The purpose of this is to be able to re-use the same method for functional
# code and the unit test file
src_dir: str = f'{dir_}/_{year}'
print(f'Creating directory {src_dir}')
os.makedirs(src_dir, exist_ok=True)
src_file: str = f'{src_dir}/{filename}{pad_day(day)}.py'
# Do nothing if the file exists
if os.path.isfile(src_file):
print(f'The file {src_file} already exists')
else:
# Render the template into the file (e.g. day01.py)
print(f'Creating source file {src_file}')
with open(src_file, 'w', encoding='utf-8') as f:
print(template.render(year=year, day=day, day_padded=pad_day(day)), file=f)
def main():
parser = argparse_setup()
args = parser.parse_args()
env = Environment(loader=FileSystemLoader('src/main/template'))
template: Template = env.get_template('day.py.j2')
generate_source_file(template, 'src/main', 'day', args.year, args.day)
test_template = env.get_template('test_day.py.j2')
generate_source_file(test_template, 'src/test', 'test_day', args.year, args.day)
# Create __init__ files
open(f"src/main/_{args.year}/__init__.py", 'w').close()
open(f"src/test/_{args.year}/__init__.py", 'w').close()
if args.test_file:
generate_test_input_files(args.year, args.day)
if __name__ == '__main__':
main()