|
| 1 | +"""Runs rocrate_validator and adapts its output to a ValidationOutcome. |
| 2 | +
|
| 3 | +This is the boundary to the external validator. Both entry points always |
| 4 | +return a :class:`ValidationOutcome` - a validator exception becomes an ``error`` |
| 5 | +outcome rather than a string, so callers never have to type-check the result. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | + |
| 10 | +from typing import Optional |
| 11 | + |
| 12 | +from rocrate_validator import services |
| 13 | + |
| 14 | +from app.validation.results import ValidationOutcome |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +def validate_crate_path( |
| 20 | + rocrate_uri: str, |
| 21 | + profile_name: Optional[str] = None, |
| 22 | + profiles_path: Optional[str] = None, |
| 23 | + skip_checks: Optional[list] = None, |
| 24 | + created_at: Optional[str] = None, |
| 25 | +) -> ValidationOutcome: |
| 26 | + """Validate a crate on disk (a directory or zip) at ``rocrate_uri``.""" |
| 27 | + return _run( |
| 28 | + {"rocrate_uri": rocrate_uri}, |
| 29 | + profile_name=profile_name, |
| 30 | + profiles_path=profiles_path, |
| 31 | + skip_checks=skip_checks, |
| 32 | + created_at=created_at, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def validate_metadata( |
| 37 | + metadata: dict, |
| 38 | + profile_name: Optional[str] = None, |
| 39 | + profiles_path: Optional[str] = None, |
| 40 | + skip_checks: Optional[list] = None, |
| 41 | + created_at: Optional[str] = None, |
| 42 | +) -> ValidationOutcome: |
| 43 | + """Validate an in-memory RO-Crate metadata graph.""" |
| 44 | + return _run( |
| 45 | + {"metadata_only": True, "metadata_dict": metadata}, |
| 46 | + profile_name=profile_name, |
| 47 | + profiles_path=profiles_path, |
| 48 | + skip_checks=skip_checks, |
| 49 | + created_at=created_at, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def _run( |
| 54 | + base_settings: dict, |
| 55 | + profile_name: Optional[str], |
| 56 | + profiles_path: Optional[str], |
| 57 | + skip_checks: Optional[list], |
| 58 | + created_at: Optional[str], |
| 59 | +) -> ValidationOutcome: |
| 60 | + options = dict(base_settings) |
| 61 | + if profile_name: |
| 62 | + options["profile_identifier"] = profile_name |
| 63 | + if profiles_path: |
| 64 | + options["profiles_path"] = profiles_path |
| 65 | + if skip_checks: |
| 66 | + options["skip_checks"] = skip_checks |
| 67 | + |
| 68 | + try: |
| 69 | + settings = services.ValidationSettings(**options) |
| 70 | + result = services.validate(settings) |
| 71 | + except ( |
| 72 | + Exception |
| 73 | + ) as error: # noqa: BLE001 - adapt any validator failure to an outcome |
| 74 | + logger.error("Validation failed: %s", error) |
| 75 | + return ValidationOutcome.from_error( |
| 76 | + str(error), profile=profile_name, created_at=created_at |
| 77 | + ) |
| 78 | + |
| 79 | + return ValidationOutcome.from_validator_result( |
| 80 | + result, profile=profile_name, created_at=created_at |
| 81 | + ) |
0 commit comments