Skip to content

Commit 75da4bb

Browse files
authored
Merge pull request #1149 from catalystneuro/use_cached_namespace_validation
Use cached namespace validation
2 parents c66fd18 + d7ab706 commit 75da4bb

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

dandi/pynwb_utils.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from fscacher import PersistentCache
1111
import h5py
1212
import hdmf
13+
from packaging.version import Version
1314
import pynwb
1415
from pynwb import NWBHDF5IO
1516
import semantic_version
@@ -335,23 +336,27 @@ def validate(
335336
path = str(path) # Might come in as pathlib's PATH
336337
errors: List[ValidationResult] = []
337338
try:
338-
with pynwb.NWBHDF5IO(path, "r", load_namespaces=True) as reader:
339-
error_outputs = pynwb.validate(reader)
340-
# TODO: return ValidationResult structs
341-
for error_output in error_outputs:
342-
errors.append(
343-
ValidationResult(
344-
origin=ValidationOrigin(
345-
name="pynwb",
346-
version=pynwb.__version__,
347-
),
348-
severity=Severity.WARNING,
349-
id=f"pywnb.{error_output}",
350-
scope=Scope.FILE,
351-
path=Path(path),
352-
message="Failed to validate.",
353-
)
339+
if Version(pynwb.__version__) >= Version("2.2.0"): # Use cached namespace feature
340+
# argument get_cached_namespaces is True by default
341+
error_outputs, _ = pynwb.validate(paths=[path])
342+
else: # Fallback if an older version
343+
with pynwb.NWBHDF5IO(path=path, mode="r", load_namespaces=True) as reader:
344+
error_outputs = pynwb.validate(io=reader)
345+
# TODO: return ValidationResult structs
346+
for error_output in error_outputs:
347+
errors.append(
348+
ValidationResult(
349+
origin=ValidationOrigin(
350+
name="pynwb",
351+
version=pynwb.__version__,
352+
),
353+
severity=Severity.WARNING,
354+
id=f"pywnb.{error_output}",
355+
scope=Scope.FILE,
356+
path=Path(path),
357+
message="Failed to validate.",
354358
)
359+
)
355360
except Exception as exc:
356361
if devel_debug:
357362
raise

0 commit comments

Comments
 (0)