|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from collections.abc import Iterable |
4 | 3 | import logging |
5 | 4 | import os |
6 | 5 | import re |
|
15 | 14 | from ..validate.types import Severity, ValidationResult |
16 | 15 |
|
17 | 16 |
|
| 17 | +def _collect_results( |
| 18 | + paths: tuple[str, ...], |
| 19 | + schema: str | None, |
| 20 | + devel_debug: bool, |
| 21 | + allow_any_path: bool, |
| 22 | +) -> list[ValidationResult]: |
| 23 | + """Run validation and collect all results into a list.""" |
| 24 | + # Avoid heavy import by importing within function: |
| 25 | + from ..pynwb_utils import ignore_benign_pynwb_warnings |
| 26 | + |
| 27 | + # Don't log validation warnings, as this command reports them to the user |
| 28 | + # anyway: |
| 29 | + root = logging.getLogger() |
| 30 | + for h in root.handlers: |
| 31 | + h.addFilter(lambda r: not getattr(r, "validating", False)) |
| 32 | + |
| 33 | + if not paths: |
| 34 | + paths = (os.curdir,) |
| 35 | + # below we are using load_namespaces but it causes HDMF to whine if there |
| 36 | + # is no cached name spaces in the file. It is benign but not really useful |
| 37 | + # at this point, so we ignore it although ideally there should be a formal |
| 38 | + # way to get relevant warnings (not errors) from PyNWB |
| 39 | + ignore_benign_pynwb_warnings() |
| 40 | + |
| 41 | + return list( |
| 42 | + validate_( |
| 43 | + *paths, |
| 44 | + schema_version=schema, |
| 45 | + devel_debug=devel_debug, |
| 46 | + allow_any_path=allow_any_path, |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def _filter_results( |
| 52 | + results: list[ValidationResult], |
| 53 | + min_severity: str, |
| 54 | + ignore: str | None, |
| 55 | +) -> list[ValidationResult]: |
| 56 | + """Filter results by minimum severity and ignore pattern.""" |
| 57 | + min_severity_value = Severity[min_severity].value |
| 58 | + filtered = [ |
| 59 | + r |
| 60 | + for r in results |
| 61 | + if r.severity is not None and r.severity.value >= min_severity_value |
| 62 | + ] |
| 63 | + if ignore is not None: |
| 64 | + filtered = [r for r in filtered if not re.search(ignore, r.id)] |
| 65 | + return filtered |
| 66 | + |
| 67 | + |
18 | 68 | @click.command() |
19 | 69 | @click.option( |
20 | 70 | "--schema", help="Validate against new BIDS schema version.", metavar="VERSION" |
@@ -102,49 +152,15 @@ def validate( |
102 | 152 |
|
103 | 153 | Exits with non-0 exit code if any file is not compliant. |
104 | 154 | """ |
105 | | - # Avoid heavy import by importing within function: |
106 | | - from ..pynwb_utils import ignore_benign_pynwb_warnings |
107 | | - |
108 | | - # Don't log validation warnings, as this command reports them to the user |
109 | | - # anyway: |
110 | | - root = logging.getLogger() |
111 | | - for h in root.handlers: |
112 | | - h.addFilter(lambda r: not getattr(r, "validating", False)) |
113 | | - |
114 | | - if not paths: |
115 | | - paths = (os.curdir,) |
116 | | - # below we are using load_namespaces but it causes HDMF to whine if there |
117 | | - # is no cached name spaces in the file. It is benign but not really useful |
118 | | - # at this point, so we ignore it although ideally there should be a formal |
119 | | - # way to get relevant warnings (not errors) from PyNWB |
120 | | - ignore_benign_pynwb_warnings() |
121 | | - |
122 | | - validator_result = validate_( |
123 | | - *paths, |
124 | | - schema_version=schema, |
125 | | - devel_debug=devel_debug, |
126 | | - allow_any_path=allow_any_path, |
127 | | - ) |
128 | | - |
129 | | - min_severity_value = Severity[min_severity].value |
130 | | - |
131 | | - filtered_results = [ |
132 | | - i |
133 | | - for i in validator_result |
134 | | - if i.severity is not None and i.severity.value >= min_severity_value |
135 | | - ] |
136 | | - |
137 | | - _process_issues(filtered_results, grouping, ignore) |
| 155 | + results = _collect_results(paths, schema, devel_debug, allow_any_path) |
| 156 | + filtered = _filter_results(results, min_severity, ignore) |
| 157 | + _process_issues(filtered, grouping) |
138 | 158 |
|
139 | 159 |
|
140 | 160 | def _process_issues( |
141 | | - validator_result: Iterable[ValidationResult], |
| 161 | + issues: list[ValidationResult], |
142 | 162 | grouping: str, |
143 | | - ignore: str | None = None, |
144 | 163 | ) -> None: |
145 | | - issues = [i for i in validator_result if i.severity is not None] |
146 | | - if ignore is not None: |
147 | | - issues = [i for i in issues if not re.search(ignore, i.id)] |
148 | 164 | purviews = [i.purview for i in issues] |
149 | 165 | if grouping == "none": |
150 | 166 | display_errors( |
|
0 commit comments