Skip to content

Commit

Permalink
work on the error display + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bpinsard committed Oct 2, 2024
1 parent 263f323 commit 27f3876
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
15 changes: 3 additions & 12 deletions src/forbids/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import bids

from .init import initialize
from .validation import validate
from ..init import initialize
from ..validation import process_validation

DEBUG = bool(os.environ.get("DEBUG", False))
if DEBUG:
Expand Down Expand Up @@ -68,16 +68,7 @@ def main() -> None:
version_specific=args.version_specific,
)
elif args.command == "validate":
no_error = True
for error in validate(layout, subject=args.participant_label, session=args.session_label):
no_error = False
lgr.error(
"%s %s : %s found %s",
error.__class__.__name__,
".".join([str(e) for e in error.absolute_path]),
error.message,
error.instance if "required" not in error.message else "",
)
no_error = process_validation(layout, subject=args.participant_label, session=args.session_label)
exit(0 if no_error else 1)


Expand Down
2 changes: 1 addition & 1 deletion src/forbids/cli/init.py → src/forbids/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from apischema.json_schema import deserialization_schema
from jsonschema.exceptions import ValidationError

from .. import schema
from . import schema

configs = {}
lgr = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion src/forbids/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,5 @@ def prepare_metadata(
# rename conflictual keywords as the schema was created
sidecar_data = {k + ("__" if k in keyword.kwlist else ""): v for k, v in sidecar.get_dict().items()}
# create an aggregate tag of all schema-defined instrument tags
sidecar_data["__instrument__"] = "".join([sidecar_data.get(instr_tag, "unknown") for instr_tag in instrument_tags])
sidecar_data["__instrument__"] = "-".join([sidecar_data.get(instr_tag, "unknown") for instr_tag in instrument_tags])
return sidecar_data
36 changes: 34 additions & 2 deletions src/forbids/cli/validation.py → src/forbids/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
import os

import bids
from jsonschema._utils import Unset
from jsonschema.exceptions import ValidationError

from .. import schema
from . import schema

lgr = logging.getLogger(__name__)


class BIDSJSONError(ValidationError):

Check failure on line 15 in src/forbids/validation.py

View workflow job for this annotation

GitHub Actions / pylint

[pylint] src/forbids/validation.py#L15

C0115: Missing class docstring (missing-class-docstring)
Raw output
src/forbids/validation.py:15:0: C0115: Missing class docstring (missing-class-docstring)
# class to represent BIDS metadata error
pass


class BIDSFileError(ValidationError):

Check failure on line 20 in src/forbids/validation.py

View workflow job for this annotation

GitHub Actions / pylint

[pylint] src/forbids/validation.py#L20

C0115: Missing class docstring (missing-class-docstring)
Raw output
src/forbids/validation.py:20:0: C0115: Missing class docstring (missing-class-docstring)
# class to represent error of BIDS file missing or unexpected
pass
Expand Down Expand Up @@ -94,6 +100,32 @@ def validate(bids_layout: bids.BIDSLayout, **entities: dict[str, str | list]):
lgr.error("an error occurred")
lgr.debug("validating %s", sidecar.relpath)
sidecar_data = schema.prepare_metadata(sidecar, bidsfile_constraints["instrument_tags"])
yield from validator.iter_errors(sidecar_data)
yield from add_path_note_to_error(validator, sidecar_data, sidecar.relpath)
for extra_sidecar in all_sidecars:
yield BIDSFileError("Unexpected BIDS file %s", extra_sidecar.relpath)


def add_path_note_to_error(validator, sidecar_data, filepath):

Check failure on line 108 in src/forbids/validation.py

View workflow job for this annotation

GitHub Actions / pylint

[pylint] src/forbids/validation.py#L108

C0116: Missing function or method docstring (missing-function-docstring)
Raw output
src/forbids/validation.py:108:0: C0116: Missing function or method docstring (missing-function-docstring)
for error in validator.iter_errors(sidecar_data):
error.add_note(filepath)
yield error


def process_validation(layout, subject, session):

Check failure on line 114 in src/forbids/validation.py

View workflow job for this annotation

GitHub Actions / pylint

[pylint] src/forbids/validation.py#L114

C0116: Missing function or method docstring (missing-function-docstring)
Raw output
src/forbids/validation.py:114:0: C0116: Missing function or method docstring (missing-function-docstring)
no_error = True
for error in validate(layout, subject=subject, session=session):
no_error = False

formatted_message = error.message
if len(error.path) == 0 and not isinstance(error.instance, Unset):
formatted_message = f"non-existing schema for instrument {error.instance['__instrument__']}"

lgr.error(
"%s %s %s : %s",
error.__class__.__name__,
error.__notes__[0] if hasattr(error, "__notes__") else "",
".".join([str(e) for e in error.absolute_path]),
formatted_message,
)
lgr.debug(error)
return no_error

0 comments on commit 27f3876

Please sign in to comment.