Skip to content

Commit 068cf92

Browse files
committed
converting hash_value to a standalone function; edits in is_existing_file
1 parent 025eb74 commit 068cf92

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

pydra/engine/helpers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import subprocess as sp
1111

1212
from .specs import Runtime, File, attr_fields
13+
from .helpers_file import is_existing_file, hash_file
1314

1415

1516
def ensure_list(obj, tuple2list=False):
@@ -396,6 +397,27 @@ def hash_function(obj):
396397
return sha256(str(obj).encode()).hexdigest()
397398

398399

400+
def hash_value(value, tp=None, metadata=None):
401+
"""calculating hash or returning values recursively"""
402+
if isinstance(value, (tuple, list)):
403+
return [hash_value(el, tp, metadata) for el in value]
404+
elif isinstance(value, dict):
405+
dict_hash = {k: hash_value(v, tp, metadata) for (k, v) in value.items()}
406+
# returning a sorted object
407+
return sorted(dict_hash.items(), key=lambda x: x[0])
408+
else: # not a container
409+
if (
410+
(tp is File or "pydra.engine.specs.File" in str(tp))
411+
and is_existing_file(value)
412+
and "container_path" not in metadata
413+
):
414+
return hash_file(value)
415+
elif isinstance(value, tuple):
416+
return list(value)
417+
else:
418+
return value
419+
420+
399421
def output_names_from_inputfields(inputs):
400422
"""
401423
Collect outputs from input fields with output_file_template.

pydra/engine/helpers_file.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,9 @@ def is_local_file(f):
514514
return f.type is File and "container_path" not in f.metadata
515515

516516

517-
def is_existed_file(f):
518-
""" checking if object is an existed file: using multiple path classes"""
519-
from py import path
520-
521-
# for windows might need more path options
522-
if isinstance(f, (str, Path, path.local)) and Path(f).exists():
523-
return True
524-
else:
517+
def is_existing_file(f):
518+
""" checking if an object is an existing file"""
519+
try:
520+
return Path(f).exists()
521+
except TypeError:
525522
return False

pydra/engine/specs.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import attr
33
from pathlib import Path
44
import typing as ty
5-
from .helpers_file import hash_file, is_existed_file
65

76

87
def attr_fields(x):
@@ -40,15 +39,15 @@ def collect_additional_outputs(self, input_spec, inputs, output_dir):
4039
@property
4140
def hash(self):
4241
"""Compute a basic hash for any given set of fields."""
43-
from .helpers import hash_function
42+
from .helpers import hash_value, hash_function
4443

4544
inp_dict = {}
4645
for field in attr_fields(self):
4746
if field.name in ["_graph_checksums", "bindings"] or field.metadata.get(
4847
"output_file_template"
4948
):
5049
continue
51-
inp_dict[field.name] = self.hash_value(
50+
inp_dict[field.name] = hash_value(
5251
value=getattr(self, field.name), tp=field.type, metadata=field.metadata
5352
)
5453
inp_hash = hash_function(inp_dict)
@@ -57,28 +56,6 @@ def hash(self):
5756
else:
5857
return inp_hash
5958

60-
def hash_value(self, value, tp=None, metadata=None):
61-
"""calculating hash or returning values recursively"""
62-
if isinstance(value, (tuple, list)):
63-
return [self.hash_value(el, tp, metadata) for el in value]
64-
elif isinstance(value, dict):
65-
dict_hash = {
66-
k: self.hash_value(v, tp, metadata) for (k, v) in value.items()
67-
}
68-
# returning a sorted object
69-
return sorted(dict_hash.items(), key=lambda x: x[0])
70-
else: # not a container
71-
if (
72-
(tp is File or "pydra.engine.specs.File" in str(tp))
73-
and is_existed_file(value)
74-
and "container_path" not in metadata
75-
):
76-
return hash_file(value)
77-
elif isinstance(value, tuple):
78-
return list(value)
79-
else:
80-
return value
81-
8259
def retrieve_values(self, wf, state_index=None):
8360
"""Get values contained by this spec."""
8461
temp_values = {}

0 commit comments

Comments
 (0)