refactor: run checker as python -m check_meta
Browse filesFiles changed (3)
- README.md+8-6
- templates/check_meta.py → check_meta/__init__.pyRenamed+2-29
- check_meta/__main__.pyAdded+31
126 unmodified lines12712812913013113213313413513613713813914014114214314414514633 unmodified lines180181182183184126 unmodified linesshare was granted.```bashpython templates/check_meta.py ./gold-512-bin4hf upload bobleesj/quantem-data ./gold-512-bin4 \widget-tutorials/show4dstem/gold-512-bin4/full \--repo-type dataset --create-pr```The last path is the folder on this repo. `./gold-512-bin4` is your localfolder (`data.npy` + `meta.json`). The hub path is`widget-tutorials/<widget>/<name>/<size>/`. For a new ShowBragg set thatwould be `widget-tutorials/showbragg/<name>/full`. Size is `small`,`medium`, `large`, or `full`. Use `widget-tutorials/shared/` only whenmore than one widget will read the same files.`check_meta.py` must print `ok` or do not open the PR. Resume a stoppedupload with `--revision refs/pr/N` (do not pass `--create-pr` again).Merge is on the[Community tab](https://huggingface.co/datasets/bobleesj/quantem-data/discussions).33 unmodified lines`permission.from`, `permission.collectors`, `permission.date`). If youcollected it yourself, set `from` and `collectors` to your name.Do not open a pull request without that permission. `check_meta.py` mustprint `ok`.126 unmodified lines12712812913013113213313413513613713813914014114214314414514614714833 unmodified lines182183184185186126 unmodified linesshare was granted.```bashpython -m check_meta ./gold-512-bin4hf upload bobleesj/quantem-data ./gold-512-bin4 \widget-tutorials/show4dstem/gold-512-bin4/full \--repo-type dataset --create-pr```Run `python -m check_meta` from a checkout of this dataset so the`check_meta/` package is importable. `./gold-512-bin4` is your localfolder (`data.npy` + `meta.json`). The last `hf upload` path is thefolder on this repo. The hub path is`widget-tutorials/<widget>/<name>/<size>/`. For a new ShowBragg set thatwould be `widget-tutorials/showbragg/<name>/full`. Size is `small`,`medium`, `large`, or `full`. Use `widget-tutorials/shared/` only whenmore than one widget will read the same files.`python -m check_meta` must print `ok` or do not open the PR. Resume a stoppedupload with `--revision refs/pr/N` (do not pass `--create-pr` again).Merge is on the[Community tab](https://huggingface.co/datasets/bobleesj/quantem-data/discussions).33 unmodified lines`permission.from`, `permission.collectors`, `permission.date`). If youcollected it yourself, set `from` and `collectors` to your name.Do not open a pull request without that permission. `python -m check_meta`must print `ok`.
12345678910111213141516171819201 unmodified line2223242526272811 unmodified lines4041424344454653 unmodified lines100101102103104105106107108109110111112113114115116117118119120121122123124#!/usr/bin/env python3"""Refuse a folder whose meta.json is missing required keys.Required: name, source, date, sample, shape, dtype, permission(permission.received, permission.from, permission.collectors,permission.date). from and collectors are one or more names.Optional: sampling, units, and optics keys.Usage:python check_meta.py /path/to/folder"""from __future__ import annotationsimport jsonimport reimport sysfrom pathlib import PathREQUIRED = ("name", "source", "date", "sample", "shape", "dtype", "permission")1 unmodified lineDATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")def _name_list(value) -> list[str] | None:"""Accept one name or a list of names. Empty is missing."""if isinstance(value, str) and value.strip():11 unmodified linesif not path.is_file():return [f"missing {path}"]try:meta = json.loads(path.read_text())except json.JSONDecodeError as exc:return [f"{path} is not JSON: {exc}"]if not isinstance(meta, dict):53 unmodified linesif isinstance(shape, list) and isinstance(units, list) and len(units) != len(shape):problems.append("units length must match shape length")return problemsdef main(argv: list[str]) -> int:if len(argv) != 2:print("usage: python check_meta.py /path/to/folder", file=sys.stderr)return 2folder = Path(argv[1])if not folder.is_dir():print(f"not a folder: {folder}", file=sys.stderr)return 2problems = check_meta(folder)if problems:print(f"{folder / 'meta.json'}:")for item in problems:print(f" {item}")return 1print(f"{folder / 'meta.json'}: ok ({', '.join(REQUIRED)})")return 0if __name__ == "__main__":raise SystemExit(main(sys.argv))1234567891011121314151 unmodified line1718192021222311 unmodified lines3536373839404153 unmodified lines959697"""Refuse a folder whose meta.json is missing required keys.Required: name, source, date, sample, shape, dtype, permission(permission.received, permission.from, permission.collectors,permission.date). from and collectors are one or more names.Optional: sampling, units, and optics keys."""from __future__ import annotationsimport jsonimport refrom pathlib import PathREQUIRED = ("name", "source", "date", "sample", "shape", "dtype", "permission")1 unmodified lineDATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")def _name_list(value: object) -> list[str] | None:"""Accept one name or a list of names. Empty is missing."""if isinstance(value, str) and value.strip():11 unmodified linesif not path.is_file():return [f"missing {path}"]try:meta = json.loads(path.read_text(encoding="utf-8"))except json.JSONDecodeError as exc:return [f"{path} is not JSON: {exc}"]if not isinstance(meta, dict):53 unmodified linesif isinstance(shape, list) and isinstance(units, list) and len(units) != len(shape):problems.append("units length must match shape length")return problems
12345678910111213141516171819202122232425262728293031"""CLI: python -m check_meta /path/to/folder"""
from __future__ import annotations
import argparseimport sysfrom pathlib import Path
from check_meta import REQUIRED, check_meta
def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(prog="python -m check_meta") parser.add_argument("folder", type=Path, help="folder that contains meta.json") args = parser.parse_args(argv) folder: Path = args.folder if not folder.is_dir(): print(f"not a folder: {folder}", file=sys.stderr) return 2 problems = check_meta(folder) if problems: print(f"{folder / 'meta.json'}:") for item in problems: print(f" {item}") return 1 print(f"{folder / 'meta.json'}: ok ({', '.join(REQUIRED)})") return 0
if __name__ == "__main__": raise SystemExit(main())README.md+8-6
templates/check_meta.py → check_meta/__init__.pyRenamed+2-29
check_meta/__main__.pyAdded+31