Skip to content

Commit

Permalink
Add CLI argument in BidsValidator plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pvandyken committed Jul 13, 2024
1 parent 76a8fbf commit 0736d99
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
34 changes: 33 additions & 1 deletion snakebids/plugins/validator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import argparse
import json
import logging
import subprocess as sp
Expand All @@ -10,6 +11,8 @@

from snakebids import bidsapp
from snakebids.exceptions import SnakebidsPluginError
from snakebids.plugins.base import PluginBase
from snakebids.utils.utils import text_fold

_logger = logging.getLogger(__name__)

Expand All @@ -19,12 +22,20 @@ class InvalidBidsError(SnakebidsPluginError):


@attr.define
class BidsValidator:
class BidsValidator(PluginBase):
"""Perform validation of a BIDS dataset using the bids-validator.
If the dataset is not valid according to the BIDS specifications, an
InvalidBidsError is raised.
An argument --skip-bids-validation is added to the CLI. This argument can be
overriden by adding an argument with dest ``plugins.validator.skip`` before this
plugin runs.
Two entries are added to config:
- ``"plugins.validator.skip"``: Indicates if validation was skipped
- ``"plugins.validator.success"``: Indicates if validation succeeded
Parameters
----------
raise_invalid_bids : bool
Expand All @@ -35,9 +46,30 @@ class BidsValidator:

raise_invalid_bids: bool = attr.field(default=True)

PREFIX = "plugins.validator"

def __eq__(self, other: Any):
return isinstance(other, self.__class__)

@bidsapp.hookimpl
def add_cli_arguments(self, parser: argparse.ArgumentParser):
"""Add option to skip validation."""
self.try_add_argument(
parser,
"--skip-bids-validation",
dest="skip",
action="store_true",
default=False,
help=text_fold(
"""
Skip validation of BIDS dataset. BIDS validation is performed by default
using the bids-validator plugin (if installed/enabled) or with the
pybids validator implementation (if bids-validator is not
installed/enabled).
"""
),
)

@bidsapp.hookimpl
def finalize_config(self, config: dict[str, Any]) -> None:
"""Perform BIDS validation of dataset.
Expand Down
10 changes: 0 additions & 10 deletions snakebids/project_template/{{name_slug}}/config/snakebids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ pybids_inputs:
# configuration for the command-line parameters to make available
# passed on the argparse add_argument()
parse_args:
--skip-bids-validation:
help: |
Skip validation of BIDS dataset. BIDS validation is performed by default
using the bids-validator plugin (if installed/enabled) or with the pybids
validator implementation (if bids-validator is not installed/enabled).
dest: "plugins.validator.skip"
action: "store_true"
default: False



# Docker and singularity containers may be defined here
containers:
Expand Down
9 changes: 9 additions & 0 deletions snakebids/tests/test_plugins/test_validate_plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import argparse
import subprocess as sp
from pathlib import Path

Expand All @@ -9,6 +10,14 @@
from snakebids.plugins.validator import BidsValidator, InvalidBidsError


def test_skip_validation():
val = BidsValidator()
parser = argparse.ArgumentParser()
val.add_cli_arguments(parser)
parsed = parser.parse_args(["--skip-bids-validation"])
assert parsed.__dict__["plugins.validator.skip"] is True


class TestFinalizeConfig:
def test_skip_validation(self):
val = BidsValidator()
Expand Down

0 comments on commit 0736d99

Please sign in to comment.