|
| 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:]) |
0 commit comments