From a8ec290d35b71340971353bf3caa02dbc69dbabd Mon Sep 17 00:00:00 2001 From: Mark Rofail Date: Sun, 18 Oct 2020 08:47:38 +0200 Subject: [PATCH 1/2] [feat] add support to {var} in filepaths --- regen.py | 70 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/regen.py b/regen.py index 9b5331e..94d7169 100755 --- a/regen.py +++ b/regen.py @@ -4,12 +4,11 @@ # out. import configparser -import os import re from pathlib import Path from typing import Match -THIS_DIR = Path(os.path.abspath(__file__)).parent +THIS_DIR = Path(__file__).absolute().parent TEMPLATE_DIR = THIS_DIR / "templates" # This is very simplistic... @@ -34,6 +33,13 @@ def replace(match: Match[str]) -> str: return VARIABLE_RE.sub(replace, tmpl) +def is_template_file(filepath: Path) -> bool: + return ( + filepath.suffix == 'in' + or filepath.name == '__init__.py' + ) + + def main() -> None: # In case we've answered anything before, attempt load. parser = configparser.RawConfigParser() @@ -41,33 +47,39 @@ def main() -> None: if "vars" not in parser: parser.add_section("vars") - for dirpath, dirnames, filenames in os.walk(TEMPLATE_DIR): - for fn in filenames: - if fn.endswith(".in"): - template_path = Path(dirpath) / fn - local_path = (Path(dirpath) / fn[:-3]).relative_to(TEMPLATE_DIR) - with template_path.open("r") as f: - data = f.read() - variables = VARIABLE_RE.findall(data) - for v in variables: - if v not in parser["vars"]: - parser["vars"][v] = input(f"Value for {v}? ").strip() - with open(VARS_FILENAME, "w") as f: - parser.write(f) - - interpolated_data = variable_format(data, **parser["vars"]) - - if local_path.exists(): - with local_path.open("r") as f: - existing_data = f.read() - if existing_data == interpolated_data: - print(f"Unchanged {local_path}") - continue - - print(f"Writing {local_path}") - local_path.parent.mkdir(parents=True, exist_ok=True) - with local_path.open("w") as f: - f.write(interpolated_data) + for template_path in TEMPLATE_DIR.glob('**/*'): + dirpath = template_path.parent + + if is_template_file(template_path): + with template_path.open("r") as f: + data = f.read() + + variables = list() + variables.extend(VARIABLE_RE.findall(data)) + variables.extend(VARIABLE_RE.findall(str(template_path))) + + for v in variables: + if v not in parser["vars"]: + parser["vars"][v] = input(f"Value for {v}? ").strip() + with open(VARS_FILENAME, "w") as f: + parser.write(f) + + interpolated_data = variable_format(data, **parser["vars"]) + + local_path = (dirpath / template_path.with_suffix('')).relative_to(TEMPLATE_DIR) + local_path = Path(variable_format(str(local_path), **parser["vars"])) + + if local_path.exists(): + with local_path.open("r") as f: + existing_data = f.read() + if existing_data == interpolated_data: + print(f"Unchanged {local_path}") + continue + + print(f"Writing {local_path}") + local_path.parent.mkdir(parents=True, exist_ok=True) + with local_path.open("w") as f: + f.write(interpolated_data) if __name__ == "__main__": From 7cc9add2c214f226fc7eb1499e66895d84793b0d Mon Sep 17 00:00:00 2001 From: Mark Rofail Date: Sun, 18 Oct 2020 18:36:59 +0200 Subject: [PATCH 2/2] [fix] apply more pathlib modernization [fix] remove redundant code --- regen.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/regen.py b/regen.py index 94d7169..c64cdfd 100755 --- a/regen.py +++ b/regen.py @@ -33,13 +33,6 @@ def replace(match: Match[str]) -> str: return VARIABLE_RE.sub(replace, tmpl) -def is_template_file(filepath: Path) -> bool: - return ( - filepath.suffix == 'in' - or filepath.name == '__init__.py' - ) - - def main() -> None: # In case we've answered anything before, attempt load. parser = configparser.RawConfigParser() @@ -48,13 +41,10 @@ def main() -> None: parser.add_section("vars") for template_path in TEMPLATE_DIR.glob('**/*'): - dirpath = template_path.parent - - if is_template_file(template_path): - with template_path.open("r") as f: - data = f.read() + if template_path.suffix == '.in': + data = template_path.read_text() - variables = list() + variables = [] variables.extend(VARIABLE_RE.findall(data)) variables.extend(VARIABLE_RE.findall(str(template_path))) @@ -66,20 +56,18 @@ def main() -> None: interpolated_data = variable_format(data, **parser["vars"]) - local_path = (dirpath / template_path.with_suffix('')).relative_to(TEMPLATE_DIR) + local_path = template_path.with_suffix('').relative_to(TEMPLATE_DIR) local_path = Path(variable_format(str(local_path), **parser["vars"])) if local_path.exists(): - with local_path.open("r") as f: - existing_data = f.read() + existing_data = local_path.read_text() if existing_data == interpolated_data: print(f"Unchanged {local_path}") continue print(f"Writing {local_path}") local_path.parent.mkdir(parents=True, exist_ok=True) - with local_path.open("w") as f: - f.write(interpolated_data) + local_path.write_text(interpolated_data) if __name__ == "__main__":