Verify the provided `metadata` file based on the predefined `schema`. `metadata` content must contain the `schema` field for the URL of schema file to download. The schema standard follows: http://json-schema.org/. Args: meta_file: filepath of the metadata file to verify, i
(
meta_file: str | Sequence[str] | None = None,
filepath: PathLike | None = None,
create_dir: bool | None = None,
hash_val: str | None = None,
hash_type: str | None = None,
args_file: str | None = None,
**kwargs: Any,
)
| 1045 | |
| 1046 | |
| 1047 | def verify_metadata( |
| 1048 | meta_file: str | Sequence[str] | None = None, |
| 1049 | filepath: PathLike | None = None, |
| 1050 | create_dir: bool | None = None, |
| 1051 | hash_val: str | None = None, |
| 1052 | hash_type: str | None = None, |
| 1053 | args_file: str | None = None, |
| 1054 | **kwargs: Any, |
| 1055 | ) -> None: |
| 1056 | """ |
| 1057 | Verify the provided `metadata` file based on the predefined `schema`. |
| 1058 | `metadata` content must contain the `schema` field for the URL of schema file to download. |
| 1059 | The schema standard follows: http://json-schema.org/. |
| 1060 | |
| 1061 | Args: |
| 1062 | meta_file: filepath of the metadata file to verify, if `None`, must be provided in `args_file`. |
| 1063 | if it is a list of file paths, the content of them will be merged. |
| 1064 | filepath: file path to store the downloaded schema. |
| 1065 | create_dir: whether to create directories if not existing, default to `True`. |
| 1066 | hash_val: if not None, define the hash value to verify the downloaded schema file. |
| 1067 | hash_type: if not None, define the hash type to verify the downloaded schema file. Defaults to "md5". |
| 1068 | args_file: a JSON or YAML file to provide default values for all the args in this function. |
| 1069 | so that the command line inputs can be simplified. |
| 1070 | kwargs: other arguments for `jsonschema.validate()`. for more details: |
| 1071 | https://python-jsonschema.readthedocs.io/en/stable/validate/#jsonschema.validate. |
| 1072 | |
| 1073 | """ |
| 1074 | |
| 1075 | _args = update_kwargs( |
| 1076 | args=args_file, |
| 1077 | meta_file=meta_file, |
| 1078 | filepath=filepath, |
| 1079 | create_dir=create_dir, |
| 1080 | hash_val=hash_val, |
| 1081 | hash_type=hash_type, |
| 1082 | **kwargs, |
| 1083 | ) |
| 1084 | _log_input_summary(tag="verify_metadata", args=_args) |
| 1085 | filepath_, meta_file_, create_dir_, hash_val_, hash_type_ = _pop_args( |
| 1086 | _args, "filepath", "meta_file", create_dir=True, hash_val=None, hash_type="md5" |
| 1087 | ) |
| 1088 | |
| 1089 | check_parent_dir(path=filepath_, create_dir=create_dir_) |
| 1090 | metadata = ConfigParser.load_config_files(files=meta_file_) |
| 1091 | url = metadata.get("schema") |
| 1092 | if url is None: |
| 1093 | raise ValueError("must provide the `schema` field in the metadata for the URL of schema file.") |
| 1094 | download_url(url=url, filepath=filepath_, hash_val=hash_val_, hash_type=hash_type_, progress=True) |
| 1095 | schema = ConfigParser.load_config_file(filepath=filepath_) |
| 1096 | |
| 1097 | try: |
| 1098 | # the rest key-values in the _args are for `validate` API |
| 1099 | validate(instance=metadata, schema=schema, **_args) |
| 1100 | except ValidationError as e: # pylint: disable=E0712 |
| 1101 | # as the error message is very long, only extract the key information |
| 1102 | raise ValueError( |
| 1103 | re.compile(r".*Failed validating", re.S).findall(str(e))[0] + f" against schema `{url}`." |
| 1104 | ) from e |
searching dependent graphs…