|
10 | 10 | from fscacher import PersistentCache |
11 | 11 | import h5py |
12 | 12 | import hdmf |
| 13 | +from packaging.version import Version |
13 | 14 | import pynwb |
14 | 15 | from pynwb import NWBHDF5IO |
15 | 16 | import semantic_version |
@@ -335,23 +336,27 @@ def validate( |
335 | 336 | path = str(path) # Might come in as pathlib's PATH |
336 | 337 | errors: List[ValidationResult] = [] |
337 | 338 | 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.", |
354 | 358 | ) |
| 359 | + ) |
355 | 360 | except Exception as exc: |
356 | 361 | if devel_debug: |
357 | 362 | raise |
|
0 commit comments