chore: remove source-only metadata validator from data repository
Browse files- check_meta/__init__.py +0 -97
- check_meta/__main__.py +0 -31
check_meta/__init__.py
DELETED
|
@@ -1,97 +0,0 @@
|
|
| 1 |
-
"""Refuse a folder whose meta.json is missing required keys.
|
| 2 |
-
|
| 3 |
-
Required: name, source, date, sample, shape, dtype, permission
|
| 4 |
-
(permission.received, permission.from, permission.collectors,
|
| 5 |
-
permission.date). from and collectors are one or more names.
|
| 6 |
-
Optional: sampling, units, and optics keys.
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
from __future__ import annotations
|
| 10 |
-
|
| 11 |
-
import json
|
| 12 |
-
import re
|
| 13 |
-
from pathlib import Path
|
| 14 |
-
|
| 15 |
-
REQUIRED = ("name", "source", "date", "sample", "shape", "dtype", "permission")
|
| 16 |
-
PERMISSION_REQUIRED = ("received", "from", "collectors", "date")
|
| 17 |
-
DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def _name_list(value: object) -> list[str] | None:
|
| 21 |
-
"""Accept one name or a list of names. Empty is missing."""
|
| 22 |
-
|
| 23 |
-
if isinstance(value, str) and value.strip():
|
| 24 |
-
return [value.strip()]
|
| 25 |
-
if isinstance(value, list) and value and all(isinstance(item, str) and item.strip() for item in value):
|
| 26 |
-
return [item.strip() for item in value]
|
| 27 |
-
return None
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def check_meta(folder: Path) -> list[str]:
|
| 31 |
-
"""Return a list of problems. Empty means the sidecar is complete."""
|
| 32 |
-
|
| 33 |
-
problems: list[str] = []
|
| 34 |
-
path = folder / "meta.json"
|
| 35 |
-
if not path.is_file():
|
| 36 |
-
return [f"missing {path}"]
|
| 37 |
-
try:
|
| 38 |
-
meta = json.loads(path.read_text(encoding="utf-8"))
|
| 39 |
-
except json.JSONDecodeError as exc:
|
| 40 |
-
return [f"{path} is not JSON: {exc}"]
|
| 41 |
-
if not isinstance(meta, dict):
|
| 42 |
-
return [f"{path} must be a JSON object"]
|
| 43 |
-
for key in REQUIRED:
|
| 44 |
-
value = meta.get(key)
|
| 45 |
-
if value in (None, "", []):
|
| 46 |
-
problems.append(f"missing {key}")
|
| 47 |
-
if "name" in meta and not isinstance(meta.get("name"), str):
|
| 48 |
-
problems.append("name must be a string")
|
| 49 |
-
if "source" in meta and not isinstance(meta.get("source"), str):
|
| 50 |
-
problems.append("source must be a string")
|
| 51 |
-
if "sample" in meta and not isinstance(meta.get("sample"), str):
|
| 52 |
-
problems.append("sample must be a string")
|
| 53 |
-
permission = meta.get("permission")
|
| 54 |
-
if permission not in (None, "", []):
|
| 55 |
-
if not isinstance(permission, dict):
|
| 56 |
-
problems.append("permission must be an object")
|
| 57 |
-
else:
|
| 58 |
-
for key in PERMISSION_REQUIRED:
|
| 59 |
-
value = permission.get(key)
|
| 60 |
-
if value in (None, "", []):
|
| 61 |
-
problems.append(f"missing permission.{key}")
|
| 62 |
-
if permission.get("received") is not True:
|
| 63 |
-
problems.append("permission.received must be true")
|
| 64 |
-
if permission.get("from") not in (None, "", []) and _name_list(permission.get("from")) is None:
|
| 65 |
-
problems.append("permission.from must be one name or a list of names")
|
| 66 |
-
if permission.get("collectors") not in (None, "", []) and _name_list(permission.get("collectors")) is None:
|
| 67 |
-
problems.append("permission.collectors must be one name or a list of names")
|
| 68 |
-
perm_date = permission.get("date")
|
| 69 |
-
if perm_date not in (None, "") and not (isinstance(perm_date, str) and DATE_RE.match(perm_date)):
|
| 70 |
-
problems.append("permission.date must be YYYY-MM-DD")
|
| 71 |
-
date = meta.get("date")
|
| 72 |
-
if date not in (None, "", []) and not (isinstance(date, str) and DATE_RE.match(date)):
|
| 73 |
-
problems.append("date must be YYYY-MM-DD")
|
| 74 |
-
shape = meta.get("shape")
|
| 75 |
-
if shape not in (None, "", []):
|
| 76 |
-
if not (
|
| 77 |
-
isinstance(shape, list)
|
| 78 |
-
and len(shape) >= 2
|
| 79 |
-
and all(isinstance(n, int) and not isinstance(n, bool) for n in shape)
|
| 80 |
-
):
|
| 81 |
-
problems.append("shape must be a list of ints, length >= 2")
|
| 82 |
-
dtype = meta.get("dtype")
|
| 83 |
-
if dtype not in (None, "", []) and not isinstance(dtype, str):
|
| 84 |
-
problems.append("dtype must be a string")
|
| 85 |
-
sampling = meta.get("sampling")
|
| 86 |
-
if sampling not in (None, "", []):
|
| 87 |
-
if not (isinstance(sampling, list) and all(isinstance(n, (int, float)) and not isinstance(n, bool) for n in sampling)):
|
| 88 |
-
problems.append("sampling must be a list of numbers")
|
| 89 |
-
if isinstance(shape, list) and isinstance(sampling, list) and len(sampling) != len(shape):
|
| 90 |
-
problems.append("sampling length must match shape length")
|
| 91 |
-
units = meta.get("units")
|
| 92 |
-
if units not in (None, "", []):
|
| 93 |
-
if not (isinstance(units, list) and all(isinstance(u, str) for u in units)):
|
| 94 |
-
problems.append("units must be a list of strings")
|
| 95 |
-
if isinstance(shape, list) and isinstance(units, list) and len(units) != len(shape):
|
| 96 |
-
problems.append("units length must match shape length")
|
| 97 |
-
return problems
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
check_meta/__main__.py
DELETED
|
@@ -1,31 +0,0 @@
|
|
| 1 |
-
"""CLI: python -m check_meta /path/to/folder"""
|
| 2 |
-
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
-
import argparse
|
| 6 |
-
import sys
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
|
| 9 |
-
from check_meta import REQUIRED, check_meta
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def main(argv: list[str] | None = None) -> int:
|
| 13 |
-
parser = argparse.ArgumentParser(prog="python -m check_meta")
|
| 14 |
-
parser.add_argument("folder", type=Path, help="folder that contains meta.json")
|
| 15 |
-
args = parser.parse_args(argv)
|
| 16 |
-
folder: Path = args.folder
|
| 17 |
-
if not folder.is_dir():
|
| 18 |
-
print(f"not a folder: {folder}", file=sys.stderr)
|
| 19 |
-
return 2
|
| 20 |
-
problems = check_meta(folder)
|
| 21 |
-
if problems:
|
| 22 |
-
print(f"{folder / 'meta.json'}:")
|
| 23 |
-
for item in problems:
|
| 24 |
-
print(f" {item}")
|
| 25 |
-
return 1
|
| 26 |
-
print(f"{folder / 'meta.json'}: ok ({', '.join(REQUIRED)})")
|
| 27 |
-
return 0
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if __name__ == "__main__":
|
| 31 |
-
raise SystemExit(main())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|