Skip to content

Commit

Permalink
Refactor re-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed May 23, 2024
1 parent 44029e4 commit c0186e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/styx/compiler/compile/reexport_module.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions src/styx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c0186e7

Please sign in to comment.