Skip to content

Commit

Permalink
Allow custom log paths from the top-level validation function and use…
Browse files Browse the repository at this point in the history
… such for testing.
  • Loading branch information
TheChymera committed Apr 8, 2022
1 parent 842f0a4 commit 0359c53
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
13 changes: 12 additions & 1 deletion dandi/bids_validator_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ def write_report(
"""

report_path = report_path.format(datetime.datetime.now().strftime(datetime_format))
report_path = os.path.abspath(os.path.expanduser(report_path))
total_file_count = len(validation_result["path_listing"])
validated_files_count = total_file_count - len(validation_result["path_tracking"])
with open(report_path, "w") as f:
Expand Down Expand Up @@ -601,6 +602,7 @@ def validate_bids(
schema_version=None,
force_select=False,
debug=False,
report_path=False,
):
"""
Validate paths according to BIDS schema.
Expand All @@ -623,6 +625,10 @@ def validate_bids(
If None, the `dataset_description.json` fie will be queried for the dataset schema version.
force_select : bool, optional
Whether to fall back to newest version of schema if no version is given or found.
report_path : bool or str, optional
If `True` a log will be written using the standard output path of `.write_report()`.
If string, the string will be used as the output path.
If the variable evaluates as False, no log will be written.
Examples
--------
Expand All @@ -644,6 +650,11 @@ def validate_bids(
regex_schema,
debug=debug,
)
write_report(validation_result)

if report_path:
if isinstance(report_path, str):
write_report(validation_result, report_path=report_path)
else:
write_report(validation_result)

return validation_result
16 changes: 15 additions & 1 deletion dandi/cli/cmd_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,29 @@
@devel_option(
"--schema", help="Validate against new BIDS schema version", metavar="VERSION"
)
@click.option("--report", help="Specify path to write a report under.")
@click.option(
"--report-flag",
"-r",
is_flag=True,
help="Whether to write a report under a"
"unique path in the current directory. Only usable if `--report` is not already used.",
)
@click.argument("paths", nargs=-1, type=click.Path(exists=True, dir_okay=True))
@devel_debug_option()
@map_to_click_exceptions
def validate_bids(paths, schema=None, devel_debug=False):
def validate_bids(
paths, schema=None, devel_debug=False, report=False, report_flag=False
):
"""Validate BIDS paths."""
from ..validate import validate_bids as validate_bids_

if report_flag and not report:
report = report_flag

validate_bids_(
*paths,
report=report,
schema_version=schema,
devel_debug=devel_debug,
)
Expand Down
7 changes: 0 additions & 7 deletions dandi/tests/data/output_bids_validator_xs_write.log

This file was deleted.

9 changes: 8 additions & 1 deletion dandi/tests/test_bids_validator_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_write_report(tmp_path):
assert report_text == expected_report_text


def test_bids_datasets(bids_examples):
def test_bids_datasets(bids_examples, tmp_path):
from dandi.bids_validator_xs import validate_bids

whitelist = [
Expand Down Expand Up @@ -328,5 +328,12 @@ def test_bids_datasets(bids_examples):
selected_path = os.path.join(root, f)
selected_paths.append(selected_path)
result = validate_bids(selected_paths, schema_version=schema_path, debug=True)
# Does custom log path specification work?
result = validate_bids(
selected_paths,
schema_version=schema_path,
debug=True,
report_path=os.path.join(tmp_path, "test_bids.log"),
)
# Have all files been validated?
assert len(result["path_tracking"]) == 0
11 changes: 7 additions & 4 deletions dandi/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def validate_bids(
*paths: str,
schema_version: Optional[str] = None,
devel_debug: bool = False,
report: Optional[str] = None,
) -> Any:
"""Validate BIDS paths.
Expand All @@ -20,18 +21,20 @@ def validate_bids(
BIDS schema version to use, this setting will override the version specified in the dataset.
devel_debug : bool, optional
Whether to trigger debugging in the BIDS validator.
report_path : bool or str, optional
If `True` a log will be written using the standard output path of `.write_report()`.
If string, the string will be used as the output path.
If the variable evaluates as False, no log will be written.
Notes
-----
Can be used from bash, as:
DANDI_DEVEL=1 dandi validate-bids --schema="1.7.0+012+dandi001" /data/paths
DANDI_DEVEL=1 dandi validate-bids --schema="1.7.0+012+dandi001" --report="my.log" /my/path
"""
from .bids_validator_xs import validate_bids as validate_bids_

return validate_bids_(
paths,
schema_version=schema_version,
debug=devel_debug,
paths, schema_version=schema_version, debug=devel_debug, report_path=report
)


Expand Down

0 comments on commit 0359c53

Please sign in to comment.