From c0186e7697a917a700252b41928ef217349a059c Mon Sep 17 00:00:00 2001 From: Florian Rupprecht Date: Thu, 23 May 2024 11:06:45 -0400 Subject: [PATCH] Refactor re-exports --- src/styx/compiler/compile/reexport_module.py | 10 ++++++++++ src/styx/main.py | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/styx/compiler/compile/reexport_module.py diff --git a/src/styx/compiler/compile/reexport_module.py b/src/styx/compiler/compile/reexport_module.py new file mode 100644 index 0000000..cc2c748 --- /dev/null +++ b/src/styx/compiler/compile/reexport_module.py @@ -0,0 +1,10 @@ +"""Generate __init__.py that re-exports __all__ from all submodules.""" + +from styx.pycodegen.core import PyModule + + +def generate_reexport_module(relative_imports: list[str]) -> str: + """Generate __init__.py that re-exports __all__ from all submodules.""" + module: PyModule = PyModule() + module.imports = list(map(lambda item: f"from .{item} import *", sorted(relative_imports))) + return module.text() diff --git a/src/styx/main.py b/src/styx/main.py index fb3b97f..1bc8473 100644 --- a/src/styx/main.py +++ b/src/styx/main.py @@ -4,6 +4,7 @@ import tomli as tomllib # Remove once we move to python 3.11 +from styx.compiler.compile.reexport_module import generate_reexport_module from styx.compiler.core import compile_boutiques_dict from styx.compiler.settings import CompilerSettings from styx.pycodegen.utils import python_snakify @@ -150,11 +151,10 @@ def main() -> None: def _walk_tree(tree: dict, path: str) -> None: for key, value in tree.items(): if key == "__items__": - buf = list(map(lambda item: f"from .{item} import *", sorted(value))) assert settings.output_path is not None out_path = settings.output_path / path / "__init__.py" with open(out_path, "w") as init_file: - init_file.write("\n".join(buf) + "\n") + init_file.write(generate_reexport_module(value)) print(f"Generated {out_path}") else: _walk_tree(value, f"{path}/{key}" if path else key)