Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

660 use groups to create subdirectories in nwd #661

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tests/integration/test_project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pathlib

import pytest
Expand Down Expand Up @@ -276,3 +277,45 @@ def test_build_groups(tmp_path_2):

with pytest.raises(ValueError):
project.run(nodes=[42])


def test_groups_nwd(tmp_path_2):
with zntrack.Project(automatic_node_names=True) as project:
node_1 = WriteIO(inputs="Lorem Ipsum")
with project.group() as group_1:
node_2 = WriteIO(inputs="Dolor Sit")
with project.group(name="CustomGroup") as group_2:
node_3 = WriteIO(inputs="Adipiscing Elit")

project.build()

assert node_1.nwd == pathlib.Path("nodes", node_1.name)
assert node_2.nwd == pathlib.Path(
"nodes", group_1.name, node_2.name.replace(f"{group_1.name}_", "")
)
assert node_3.nwd == pathlib.Path(
"nodes", group_2.name, node_3.name.replace(f"{group_2.name}_", "")
)
# now load the Nodes and assert as well

assert zntrack.from_rev(node_1).nwd == pathlib.Path("nodes", node_1.name)
assert zntrack.from_rev(node_2).nwd == pathlib.Path(
"nodes", group_1.name, node_2.name.replace(f"{group_1.name}_", "")
)
assert zntrack.from_rev(node_3).nwd == pathlib.Path(
"nodes", group_2.name, node_3.name.replace(f"{group_2.name}_", "")
)

with open("zntrack.json") as f:
data = json.load(f)
data[node_1.name]["nwd"]["value"] = "test"
data[node_2.name].pop("nwd")

with open("zntrack.json", "w") as f:
json.dump(data, f)

assert zntrack.from_rev(node_1).nwd == pathlib.Path("test")
assert zntrack.from_rev(node_2).nwd == pathlib.Path("nodes", node_2.name)
assert zntrack.from_rev(node_3).nwd == pathlib.Path(
"nodes", group_2.name, node_3.name.replace(f"{group_2.name}_", "")
)
17 changes: 16 additions & 1 deletion zntrack/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,15 @@ def state(self) -> NodeStatus:
@property
def nwd(self) -> pathlib.Path:
"""Get the node working directory."""
nwd = pathlib.Path("nodes", znflow.get_attribute(self, "name"))
try:
nwd = self.__dict__["nwd"]
except KeyError:
try:
zntrack_config = json.loads(pathlib.Path("zntrack.json").read_text())
nwd = zntrack_config[znflow.get_attribute(self, "name")]["nwd"]
nwd = json.loads(json.dumps(nwd), cls=znjson.ZnDecoder)
except (FileNotFoundError, KeyError):
nwd = pathlib.Path("nodes", znflow.get_attribute(self, "name"))
if not nwd.exists():
nwd.mkdir(parents=True)
return nwd
Expand Down Expand Up @@ -202,6 +210,13 @@ def save(
f"Field {attr} has no group. Please assign a group from"
f" '{FieldGroup.__module__}.{FieldGroup.__name__}'."
)
# save the nwd to zntrack.json
file_io.update_config_file(
file=pathlib.Path("zntrack.json"),
node_name=self.name,
value_name="nwd",
value=self.nwd,
)

def run(self) -> None:
"""Run the node's code."""
Expand Down
1 change: 1 addition & 0 deletions zntrack/project/zntrack_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def group(self, name: str = None):
for node_uuid in self.graph.get_sorted_nodes():
node: Node = self.graph.nodes[node_uuid]["value"]
if node_uuid not in existing_nodes:
node.__dict__["nwd"] = pathlib.Path("nodes", group.name, node.name)
node.name = f"{name}_{node.name}"
group.nodes.append(node)

Expand Down