|
1 | | -import os |
2 | 1 | import shutil |
| 2 | +from pathlib import Path |
3 | 3 | from string import Template |
4 | 4 |
|
5 | 5 | if __name__ == "__main__": |
6 | | - from config import config # type:ignore |
| 6 | + from config import config # type:ignore[missing-imports] |
7 | 7 | else: |
8 | 8 | from .config import config |
9 | 9 |
|
10 | 10 |
|
11 | 11 | def main(args: list[str]) -> None: |
12 | 12 | if len(args) != 3: |
13 | | - raise ValueError(f"Usage: {__name__} <year> <day> <lang>") |
| 13 | + msg = f"Usage: {__name__} <year> <day> <lang>" |
| 14 | + raise ValueError(msg) |
14 | 15 |
|
15 | 16 | year = args[0] |
16 | 17 | day = args[1] |
17 | 18 | day2 = f"{args[1]:0>2}" |
18 | 19 | conf = config.get_languages()[args[2]] |
19 | 20 | for template in conf.templates: |
20 | | - destination = template.destination.format(year=year, day=day) |
21 | | - if os.path.exists(destination): |
| 21 | + destination = Path(template.destination.format(year=year, day=day)) |
| 22 | + if destination.exists(): |
22 | 23 | print(f"'{destination}' already exists") |
23 | 24 | continue |
24 | | - os.makedirs(os.path.dirname(destination), exist_ok=True) |
| 25 | + destination.parent.mkdir(parents=True, exist_ok=True) |
25 | 26 | mappings = {"year": year, "day": day, "day2": day2} |
26 | | - target = shutil.copyfile(template.source, destination) |
27 | | - with open(target, "r", encoding="utf-8") as f: |
| 27 | + target = Path(shutil.copyfile(template.source, destination)) |
| 28 | + with target.open("r", encoding="utf-8") as f: |
28 | 29 | t = Template(f.read()) |
29 | 30 | s = t.substitute(mappings) |
30 | | - with open(target, "w", encoding="utf-8") as f: |
| 31 | + with target.open("w", encoding="utf-8") as f: |
31 | 32 | f.write(s) |
32 | 33 | print(f"Generated '{target}'") |
33 | | - return |
34 | 34 |
|
35 | 35 |
|
36 | 36 | if __name__ == "__main__": |
37 | 37 | main(["2018", "1", "python"]) |
38 | 38 | main(["2018", "1", "java"]) |
39 | 39 | main(["2018", "1", "rust"]) |
| 40 | + main(["2025", "4", "bash"]) |
0 commit comments