Skip to content

Commit 2d109dc

Browse files
authored
Merge pull request #10 from markrofail/master
Support '{var}' in filenames
2 parents 45a0bb4 + 7cc9add commit 2d109dc

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

regen.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
# out.
55

66
import configparser
7-
import os
87
import re
98
from pathlib import Path
109
from typing import Match
1110

12-
THIS_DIR = Path(os.path.abspath(__file__)).parent
11+
THIS_DIR = Path(__file__).absolute().parent
1312
TEMPLATE_DIR = THIS_DIR / "templates"
1413

1514
# This is very simplistic...
@@ -41,33 +40,34 @@ def main() -> None:
4140
if "vars" not in parser:
4241
parser.add_section("vars")
4342

44-
for dirpath, dirnames, filenames in os.walk(TEMPLATE_DIR):
45-
for fn in filenames:
46-
if fn.endswith(".in"):
47-
template_path = Path(dirpath) / fn
48-
local_path = (Path(dirpath) / fn[:-3]).relative_to(TEMPLATE_DIR)
49-
with template_path.open("r") as f:
50-
data = f.read()
51-
variables = VARIABLE_RE.findall(data)
52-
for v in variables:
53-
if v not in parser["vars"]:
54-
parser["vars"][v] = input(f"Value for {v}? ").strip()
55-
with open(VARS_FILENAME, "w") as f:
56-
parser.write(f)
57-
58-
interpolated_data = variable_format(data, **parser["vars"])
59-
60-
if local_path.exists():
61-
with local_path.open("r") as f:
62-
existing_data = f.read()
63-
if existing_data == interpolated_data:
64-
print(f"Unchanged {local_path}")
65-
continue
66-
67-
print(f"Writing {local_path}")
68-
local_path.parent.mkdir(parents=True, exist_ok=True)
69-
with local_path.open("w") as f:
70-
f.write(interpolated_data)
43+
for template_path in TEMPLATE_DIR.glob('**/*'):
44+
if template_path.suffix == '.in':
45+
data = template_path.read_text()
46+
47+
variables = []
48+
variables.extend(VARIABLE_RE.findall(data))
49+
variables.extend(VARIABLE_RE.findall(str(template_path)))
50+
51+
for v in variables:
52+
if v not in parser["vars"]:
53+
parser["vars"][v] = input(f"Value for {v}? ").strip()
54+
with open(VARS_FILENAME, "w") as f:
55+
parser.write(f)
56+
57+
interpolated_data = variable_format(data, **parser["vars"])
58+
59+
local_path = template_path.with_suffix('').relative_to(TEMPLATE_DIR)
60+
local_path = Path(variable_format(str(local_path), **parser["vars"]))
61+
62+
if local_path.exists():
63+
existing_data = local_path.read_text()
64+
if existing_data == interpolated_data:
65+
print(f"Unchanged {local_path}")
66+
continue
67+
68+
print(f"Writing {local_path}")
69+
local_path.parent.mkdir(parents=True, exist_ok=True)
70+
local_path.write_text(interpolated_data)
7171

7272

7373
if __name__ == "__main__":

0 commit comments

Comments
 (0)