Skip to content

Commit

Permalink
Clean load_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
enekomartinmartinez committed Sep 27, 2021
1 parent 3d8e2d6 commit 9c36e2c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
5 changes: 2 additions & 3 deletions pysd/py_backend/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,12 @@ def _build_main_module(elements, subscript_dict, file_name):

text += textwrap.dedent("""
# load modules from modules_%(outfile)s directory
for module_name, module_content in _modules.items():
exec(load_modules(module_name, module_content, _root,
"%(outfile)s", submodules=[]))
exec(load_modules("modules_%(outfile)s", _modules, _root, []))
""" % {
"outfile": os.path.basename(file_name).split(".")[0],
})
print(text)

text += funcs
text = black.format_file_contents(text, fast=True, mode=black.FileMode())
Expand Down
11 changes: 4 additions & 7 deletions pysd/py_backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,21 +889,18 @@ def open_module(root_dir, model_name, module, submodule=None):
os.path.join(root_dir, "modules_" + model_name, rel_file_path)).read()


def load_modules(module_name, module_content, root_dir, model_name,
work_dir=None, submodules=[]):
def load_modules(module_name, module_content, work_dir, submodules):
# TODO: document
if not work_dir:
work_dir = os.path.join(root_dir, "modules_" + model_name)

if isinstance(module_content, list):
with open(os.path.join(work_dir, module_name + ".py"), "r") as file:
submodules.append(file.read())
else:
work_dir = os.path.join(work_dir, module_name)
for submod_name, submod_content in module_content.items():
load_modules(
submod_name, submod_content, root_dir, model_name,
work_dir=work_dir, submodules=submodules)
submod_name, submod_content,
os.path.join(work_dir, module_name),
submodules)

return "\n\n".join(submodules)

Expand Down

0 comments on commit 9c36e2c

Please sign in to comment.