|
| 1 | +# Copyright (c) 2024-2025 CRS4 |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any |
| 16 | +import re |
| 17 | + |
| 18 | +import rocrate_validator.log as logging |
| 19 | +from rocrate_validator.models import Severity, ValidationContext |
| 20 | +from rocrate_validator.requirements.python import (PyFunctionCheck, check, |
| 21 | + requirement) |
| 22 | +from rocrate_validator.utils import HttpRequester |
| 23 | + |
| 24 | +# set up logging |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +@requirement(name="RO-Crate context version") |
| 29 | +class FileDescriptorExistence(PyFunctionCheck): |
| 30 | + """The RO-Crate metadata file MUST include the RO-Crate context version 1.2 (or later minor version) in `@context`""" |
| 31 | + |
| 32 | + @check(name="RO-Crate context version", severity=Severity.REQUIRED) |
| 33 | + def test_existence(self, context: ValidationContext) -> bool: |
| 34 | + """ |
| 35 | + The RO-Crate metadata file MUST include the RO-Crate context version 1.2 (or later minor version) in `@context` |
| 36 | + """ |
| 37 | + try: |
| 38 | + json_dict = context.ro_crate.metadata.as_dict() |
| 39 | + context_value = json_dict["@context"] |
| 40 | + pattern = re.compile(r"https://w3id\.org/ro/crate/1\.[2-9](-DRAFT)?/context") |
| 41 | + passed = True |
| 42 | + if isinstance(context_value, list): |
| 43 | + if not any(pattern.match(item) for item in context_value if isinstance(item, str)): |
| 44 | + passed = False |
| 45 | + else: |
| 46 | + if not pattern.match(context_value): |
| 47 | + passed = False |
| 48 | + if not passed: |
| 49 | + context.result.add_issue( |
| 50 | + f"The RO-Crate metadata file MUST include the RO-Crate context " |
| 51 | + "version 1.2 (or later minor version) in `@context`", self) |
| 52 | + return passed |
| 53 | + |
| 54 | + except Exception as e: |
| 55 | + if logger.isEnabledFor(logging.DEBUG): |
| 56 | + logger.exception(e) |
| 57 | + return True |
0 commit comments