|
| 1 | +""" |
| 2 | +Module for all functionality regarding the compliance of the integrated standards and |
| 3 | +requirments from other oefamily software modules like omi. |
| 4 | +
|
| 5 | +- check if the metadata will pass oep's metadata check that is performed before the saves the metadata. |
| 6 | +
|
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +from distutils.errors import CompileError |
| 11 | +import pathlib |
| 12 | +from omi.dialects.oep.parser import ParserException |
| 13 | +from omi.structure import Compilable |
| 14 | + |
| 15 | +from omi.dialects.oep import OEP_V_1_4_Dialect, OEP_V_1_5_Dialect |
| 16 | +from omi.dialects.oep.compiler import JSONCompiler |
| 17 | + |
| 18 | +import json |
| 19 | + |
| 20 | +# instances of metadata parsers / compilers, order of priority |
| 21 | +METADATA_PARSERS = [OEP_V_1_5_Dialect(), OEP_V_1_4_Dialect()] |
| 22 | +METADATA_COMPILERS = [OEP_V_1_5_Dialect(), OEP_V_1_4_Dialect(), JSONCompiler()] |
| 23 | + |
| 24 | + |
| 25 | +def read_input_json(file_path: pathlib.Path = "tests/data/metadata_v15.json"): |
| 26 | + with open(file_path, "r", encoding="utf-8") as f: |
| 27 | + jsn = json.load(f) |
| 28 | + |
| 29 | + return jsn |
| 30 | + |
| 31 | + |
| 32 | +def try_parse_metadata(inp): |
| 33 | + """ |
| 34 | + Args: |
| 35 | + inp: string or dict or OEPMetadata |
| 36 | + Returns: |
| 37 | + Tuple[OEPMetadata or None, string or None]: |
| 38 | + The first component is the result of the parsing procedure or `None` if |
| 39 | + the parsing failed. The second component is None, if the parsing failed, |
| 40 | + otherwise an error message. |
| 41 | + Examples: |
| 42 | + >>> from api.actions import try_parse_metadata |
| 43 | + >>> result, error = try_parse_metadata('{"id":"id"}') |
| 44 | + >>> error is None |
| 45 | + True |
| 46 | + """ |
| 47 | + |
| 48 | + if isinstance(inp, Compilable): |
| 49 | + # already parsed |
| 50 | + return inp, None |
| 51 | + elif not isinstance(inp, (str, bytes)): |
| 52 | + # in order to use the omi parsers, input needs to be str (or bytes) |
| 53 | + try: |
| 54 | + inp = json.dumps(inp) |
| 55 | + except Exception: |
| 56 | + return None, "Could not serialize json" |
| 57 | + |
| 58 | + last_err = None |
| 59 | + # try all the dialects |
| 60 | + for parser in METADATA_PARSERS: |
| 61 | + try: |
| 62 | + return parser.parse(inp), None |
| 63 | + except ParserException as e: |
| 64 | + return None, str(e) |
| 65 | + except Exception as e: |
| 66 | + last_err = e |
| 67 | + # APIError(f"Metadata could not be parsed: {last_err}") |
| 68 | + # try next dialect |
| 69 | + |
| 70 | + raise Exception(f"Metadata could not be parsed: {last_err}") |
| 71 | + |
| 72 | + |
| 73 | +def try_compile_metadata(inp): |
| 74 | + """ |
| 75 | + Args: |
| 76 | + inp: OEPMetadata |
| 77 | + Returns: |
| 78 | + Tuple[str or None, str or None]: |
| 79 | + The first component is the result of the compiling procedure or `None` if |
| 80 | + the compiling failed. The second component is None if the compiling failed, |
| 81 | + otherwise an error message. |
| 82 | + """ |
| 83 | + last_err = None |
| 84 | + # try all the dialects |
| 85 | + for compiler in METADATA_COMPILERS: |
| 86 | + try: |
| 87 | + return compiler.compile(inp), None |
| 88 | + except Exception as e: |
| 89 | + last_err = e |
| 90 | + # APIError(f"Metadata could not be compiled: {last_err}") |
| 91 | + # try next dialect |
| 92 | + |
| 93 | + raise Exception(f"Metadata could not be compiled: {last_err}") |
| 94 | + |
| 95 | + |
| 96 | +def check_oemetadata_is_oep_compatible(metadata): |
| 97 | + """Check if metadata is oep compliant. Metadata can be oemetadata version 1.4 or 1.5. |
| 98 | + Args: |
| 99 | + metadata: OEPMetadata or metadata object (dict) or metadata str |
| 100 | + """ |
| 101 | + |
| 102 | + # --------------------------------------- |
| 103 | + # metadata parsing |
| 104 | + # --------------------------------------- |
| 105 | + |
| 106 | + # parse the metadata object (various types) into proper OEPMetadata instance |
| 107 | + metadata_oep, err = try_parse_metadata(metadata) |
| 108 | + if err: |
| 109 | + raise ValueError(err) |
| 110 | + # compile OEPMetadata instance back into native python object (dict) |
| 111 | + # TODO: we should try to convert to the latest standard in this step? |
| 112 | + metadata_obj, err = try_compile_metadata(metadata_oep) |
| 113 | + if err: |
| 114 | + raise ValueError(err) |
| 115 | + # dump the metadata dict into json string |
| 116 | + try: |
| 117 | + metadata_str = json.dumps(metadata_obj, ensure_ascii=False) |
| 118 | + except Exception: |
| 119 | + raise TypeError("Cannot serialize metadata") |
| 120 | + |
| 121 | + |
| 122 | +# ----------- USE Cecks and Validation ------ |
| 123 | +# Make use of omis validation to mock the |
| 124 | +# oemetadata check that is performed on the |
| 125 | +# OEP for ech metadata upload |
| 126 | +# ------------------------------------------- |
| 127 | + |
| 128 | + |
| 129 | +def run_metadata_checks(oemetadata: dict = None, oemetadata_path: str = None): |
| 130 | + """ |
| 131 | + Runs metadata checks includes: |
| 132 | + - basic oep compliant check - tested by using omi's parsing and compiling |
| 133 | +
|
| 134 | + Not included: |
| 135 | + - jsonschema valdiation |
| 136 | +
|
| 137 | + Args: |
| 138 | + oemetadata (dict, optional): OEPMetadata or metadata object (dict) or metadata str. Defaults to None. |
| 139 | + oemetadata_path (str, optional): Relative path to file as string. Defaults to None. |
| 140 | +
|
| 141 | + Raises: |
| 142 | + Exception: _description_ |
| 143 | + Exception: _description_ |
| 144 | + """ |
| 145 | + |
| 146 | + if oemetadata and oemetadata_path: |
| 147 | + raise Exception( |
| 148 | + "Providing both parmaters at the same time is not permitted. Please provide a oemetadata dict or a oemetadata json file path" |
| 149 | + ) |
| 150 | + |
| 151 | + if oemetadata is None and oemetadata_path is None: |
| 152 | + raise Exception( |
| 153 | + "Please provide a parsed oemetadata dict or a path to the oemetadata json file" |
| 154 | + ) |
| 155 | + |
| 156 | + if oemetadata_path is not None and oemetadata is None: |
| 157 | + metadata = read_input_json(oemetadata_path) |
| 158 | + |
| 159 | + if oemetadata is not None and oemetadata_path is None: |
| 160 | + metadata = oemetadata |
| 161 | + |
| 162 | + check_oemetadata_is_oep_compatible(metadata=metadata) |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + correct_v15_test_data = "tests/data/metadata_v15.json" |
| 167 | + false_v15_test_data = "tests/data/bad_metadata_v15.json" |
| 168 | + v14_test_data = "tests/data/metadata_v14.json" |
| 169 | + |
| 170 | + meta = read_input_json(file_path=correct_v15_test_data) |
| 171 | + print("Check v15 metadata from file!") |
| 172 | + run_metadata_checks(oemetadata_path=correct_v15_test_data) |
| 173 | + print("Check v15 metadata from object!") |
| 174 | + run_metadata_checks(oemetadata=meta) |
| 175 | + |
| 176 | + print("Check v14 metadata!") |
| 177 | + meta = read_input_json(file_path=correct_v15_test_data) |
| 178 | + |
| 179 | + # print("test expected error case: false usage") |
| 180 | + # run_metadata_checks(oemetadata=meta, oemetadata_path=correct_test_data) |
| 181 | + # print("test expected error case: bad data") |
| 182 | + # run_metadata_checks(oemetadata_path=false_v15_test_data) |
0 commit comments