|
| 1 | +import os |
| 2 | +import argparse |
| 3 | + |
| 4 | + |
| 5 | +TEMPLATES_PATH = os.path.join(".", "src", "advent_of_code", "scripts", "templates") |
| 6 | + |
| 7 | + |
| 8 | +def parse_input_args(): |
| 9 | + parser = argparse.ArgumentParser() |
| 10 | + parser.add_argument("--day", type=str, required=True) |
| 11 | + parser.add_argument("--year", type=str, required=True) |
| 12 | + args = parser.parse_args() |
| 13 | + return args |
| 14 | + |
| 15 | + |
| 16 | +def read_template(template_file_path) -> None: |
| 17 | + with open(template_file_path, "r") as template_file: |
| 18 | + return template_file.read() |
| 19 | + |
| 20 | + |
| 21 | +def write_template(template: str, write_path: str) -> None: |
| 22 | + # if os.path.exists(write_path): |
| 23 | + # print(f"File {write_path} already exists! Not writing.") |
| 24 | + # else: |
| 25 | + with open(write_path, "w") as f: |
| 26 | + f.write(template) |
| 27 | + |
| 28 | + |
| 29 | +def generate_file(template_file_path, input_day, input_year, output_file_path): |
| 30 | + try: |
| 31 | + # Read the template content from the file |
| 32 | + template_content = read_template(template_file_path) |
| 33 | + except FileNotFoundError: |
| 34 | + print(f"Template file '{template_file_path}' not found.") |
| 35 | + exit(1) |
| 36 | + |
| 37 | + # Replace placeholder with the provided integer input |
| 38 | + modified_content = template_content.replace("{day}", str(input_day)).replace( |
| 39 | + "{year}", str(input_year) |
| 40 | + ) |
| 41 | + # Generate the Python file from the template |
| 42 | + write_template(modified_content, output_file_path) |
| 43 | + print(f"Python file '{output_file_path}' generated successfully.") |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + args = parse_input_args() |
| 48 | + input_day = args.day |
| 49 | + input_year = args.year |
| 50 | + |
| 51 | + day_file_dest = os.path.join( |
| 52 | + "src", "advent_of_code", f"year_{input_year}", f"days/{input_day}.py" |
| 53 | + ) |
| 54 | + template_path = os.path.join(TEMPLATES_PATH, "days_template.txt") |
| 55 | + generate_file(template_path, input_day, input_year, day_file_dest) |
| 56 | + |
| 57 | + solvers_file_dest = os.path.join( |
| 58 | + "src", |
| 59 | + "advent_of_code", |
| 60 | + f"year_{input_year}", |
| 61 | + f"solvers/day_{input_day}_solvers.py", |
| 62 | + ) |
| 63 | + template_path = os.path.join(TEMPLATES_PATH, "solvers_template.txt") |
| 64 | + generate_file(template_path, input_day, input_year, solvers_file_dest) |
| 65 | + |
| 66 | + tests_file_dest = os.path.join( |
| 67 | + "tests", f"year_{input_year}", f"test_day_{input_day}_solvers.py" |
| 68 | + ) |
| 69 | + template_path = os.path.join(TEMPLATES_PATH, "tests_template.txt") |
| 70 | + generate_file(template_path, input_day, input_year, tests_file_dest) |
0 commit comments