|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# /// script |
| 3 | +# dependencies = [ |
| 4 | +# "jsonschema", |
| 5 | +# "referencing", |
| 6 | +# ] |
| 7 | +# /// |
| 8 | +from __future__ import annotations |
| 9 | +from dataclasses import dataclass |
| 10 | +from pathlib import Path |
| 11 | +import json |
| 12 | +from argparse import ArgumentParser |
| 13 | +from typing import Any, Self |
| 14 | + |
| 15 | +from referencing import Registry, Resource |
| 16 | +from jsonschema import Draft202012Validator as Validator |
| 17 | +from jsonschema.exceptions import ValidationError |
| 18 | + |
| 19 | +HERE = Path(__file__).resolve().parent |
| 20 | +SCHEMA_DIR = HERE / "ngff_spec" / "schemas" |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class SchemasInfo: |
| 25 | + generic_id: str |
| 26 | + base_url: str |
| 27 | + registry: Registry |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def load(cls) -> Self: |
| 31 | + """Get the ID of the top-level schema, and the mapping of schema IDs to objects""" |
| 32 | + registry = Registry() |
| 33 | + generic = None |
| 34 | + base_url = None |
| 35 | + for p in SCHEMA_DIR.glob("*.schema*"): |
| 36 | + schema = json.loads(p.read_text()) |
| 37 | + resource = Resource.from_contents(schema) |
| 38 | + registry = resource @ registry |
| 39 | + |
| 40 | + schema_id = resource.id() |
| 41 | + if schema_id is None: |
| 42 | + raise RuntimeError("schema has no ID") |
| 43 | + if p.stem == "ome_zarr": |
| 44 | + generic = schema_id |
| 45 | + base_url = generic.split("/schemas/")[0] |
| 46 | + |
| 47 | + if generic is None or base_url is None: |
| 48 | + raise RuntimeError("Could not find generic ome_zarr schema") |
| 49 | + |
| 50 | + return cls(generic, base_url, registry) |
| 51 | + |
| 52 | + |
| 53 | +def main(raw_args=None): |
| 54 | + parser = ArgumentParser() |
| 55 | + parser.add_argument("mode", choices=["attributes", "zarr"]) |
| 56 | + parser.add_argument("path", type=Path) |
| 57 | + |
| 58 | + args = parser.parse_args(raw_args) |
| 59 | + |
| 60 | + p: Path = args.path |
| 61 | + attrs: None | dict[str, Any] = None |
| 62 | + match args.mode: |
| 63 | + case "attributes": |
| 64 | + attrs = json.loads(p.read_text()) |
| 65 | + case "zarr": |
| 66 | + attrs = json.loads(p.joinpath("zarr.json").read_text())["attributes"] |
| 67 | + case _: |
| 68 | + raise RuntimeError("unreachable") |
| 69 | + |
| 70 | + if attrs is None: |
| 71 | + raise RuntimeError("unreachable") |
| 72 | + |
| 73 | + schemas = SchemasInfo.load() |
| 74 | + |
| 75 | + generic_schema = schemas.registry.get(schemas.generic_id) |
| 76 | + if generic_schema is None: |
| 77 | + raise RuntimeError("could not find generic schema") |
| 78 | + |
| 79 | + validator = Validator( |
| 80 | + generic_schema.contents, |
| 81 | + registry=schemas.registry, |
| 82 | + ) |
| 83 | + |
| 84 | + result = dict() |
| 85 | + try: |
| 86 | + validator.validate(attrs) |
| 87 | + result["valid"] = True |
| 88 | + except ValidationError as e: |
| 89 | + result["valid"] = False |
| 90 | + result["message"] = str(e) |
| 91 | + |
| 92 | + print(json.dumps(result)) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments