bobleesj commited on
Commit
183c224
·
verified ·
1 Parent(s): 7b9bb14

refactor: run checker as python -m check_meta

Browse files
README.md CHANGED
@@ -127,20 +127,22 @@ used the instrument (one name or several). `permission.date` is when that
127
  share was granted.
128
 
129
  ```bash
130
- python templates/check_meta.py ./gold-512-bin4
131
  hf upload bobleesj/quantem-data ./gold-512-bin4 \
132
  widget-tutorials/show4dstem/gold-512-bin4/full \
133
  --repo-type dataset --create-pr
134
  ```
135
 
136
- The last path is the folder on this repo. `./gold-512-bin4` is your local
137
- folder (`data.npy` + `meta.json`). The hub path is
 
 
138
  `widget-tutorials/<widget>/<name>/<size>/`. For a new ShowBragg set that
139
  would be `widget-tutorials/showbragg/<name>/full`. Size is `small`,
140
  `medium`, `large`, or `full`. Use `widget-tutorials/shared/` only when
141
  more than one widget will read the same files.
142
 
143
- `check_meta.py` must print `ok` or do not open the PR. Resume a stopped
144
  upload with `--revision refs/pr/N` (do not pass `--create-pr` again).
145
  Merge is on the
146
  [Community tab](https://huggingface.co/datasets/bobleesj/quantem-data/discussions).
@@ -180,5 +182,5 @@ yourself. Put that receipt in `meta.json` (`permission.received`,
180
  `permission.from`, `permission.collectors`, `permission.date`). If you
181
  collected it yourself, set `from` and `collectors` to your name.
182
 
183
- Do not open a pull request without that permission. `check_meta.py` must
184
- print `ok`.
 
127
  share was granted.
128
 
129
  ```bash
130
+ python -m check_meta ./gold-512-bin4
131
  hf upload bobleesj/quantem-data ./gold-512-bin4 \
132
  widget-tutorials/show4dstem/gold-512-bin4/full \
133
  --repo-type dataset --create-pr
134
  ```
135
 
136
+ Run `python -m check_meta` from a checkout of this dataset so the
137
+ `check_meta/` package is importable. `./gold-512-bin4` is your local
138
+ folder (`data.npy` + `meta.json`). The last `hf upload` path is the
139
+ folder on this repo. The hub path is
140
  `widget-tutorials/<widget>/<name>/<size>/`. For a new ShowBragg set that
141
  would be `widget-tutorials/showbragg/<name>/full`. Size is `small`,
142
  `medium`, `large`, or `full`. Use `widget-tutorials/shared/` only when
143
  more than one widget will read the same files.
144
 
145
+ `python -m check_meta` must print `ok` or do not open the PR. Resume a stopped
146
  upload with `--revision refs/pr/N` (do not pass `--create-pr` again).
147
  Merge is on the
148
  [Community tab](https://huggingface.co/datasets/bobleesj/quantem-data/discussions).
 
182
  `permission.from`, `permission.collectors`, `permission.date`). If you
183
  collected it yourself, set `from` and `collectors` to your name.
184
 
185
+ Do not open a pull request without that permission. `python -m check_meta`
186
+ must print `ok`.
templates/check_meta.py → check_meta/__init__.py RENAMED
@@ -1,20 +1,15 @@
1
- #!/usr/bin/env python3
2
  """Refuse a folder whose meta.json is missing required keys.
3
 
4
  Required: name, source, date, sample, shape, dtype, permission
5
  (permission.received, permission.from, permission.collectors,
6
  permission.date). from and collectors are one or more names.
7
  Optional: sampling, units, and optics keys.
8
-
9
- Usage:
10
- python check_meta.py /path/to/folder
11
  """
12
 
13
  from __future__ import annotations
14
 
15
  import json
16
  import re
17
- import sys
18
  from pathlib import Path
19
 
20
  REQUIRED = ("name", "source", "date", "sample", "shape", "dtype", "permission")
@@ -22,7 +17,7 @@ PERMISSION_REQUIRED = ("received", "from", "collectors", "date")
22
  DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
23
 
24
 
25
- def _name_list(value) -> list[str] | None:
26
  """Accept one name or a list of names. Empty is missing."""
27
 
28
  if isinstance(value, str) and value.strip():
@@ -40,7 +35,7 @@ def check_meta(folder: Path) -> list[str]:
40
  if not path.is_file():
41
  return [f"missing {path}"]
42
  try:
43
- meta = json.loads(path.read_text())
44
  except json.JSONDecodeError as exc:
45
  return [f"{path} is not JSON: {exc}"]
46
  if not isinstance(meta, dict):
@@ -100,25 +95,3 @@ def check_meta(folder: Path) -> list[str]:
100
  if isinstance(shape, list) and isinstance(units, list) and len(units) != len(shape):
101
  problems.append("units length must match shape length")
102
  return problems
103
-
104
-
105
- def main(argv: list[str]) -> int:
106
- if len(argv) != 2:
107
- print("usage: python check_meta.py /path/to/folder", file=sys.stderr)
108
- return 2
109
- folder = Path(argv[1])
110
- if not folder.is_dir():
111
- print(f"not a folder: {folder}", file=sys.stderr)
112
- return 2
113
- problems = check_meta(folder)
114
- if problems:
115
- print(f"{folder / 'meta.json'}:")
116
- for item in problems:
117
- print(f" {item}")
118
- return 1
119
- print(f"{folder / 'meta.json'}: ok ({', '.join(REQUIRED)})")
120
- return 0
121
-
122
-
123
- if __name__ == "__main__":
124
- raise SystemExit(main(sys.argv))
 
 
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")
 
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():
 
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):
 
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 ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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())