Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • roboct-public/roboct-schemas
1 result
Show changes
from thd_json import Validator
from pathlib import Path
import argparse
def get_source_validator(json_suffix: str = "*.json") -> Validator:
return Validator(Path(__file__).parent / Path("source.json"), json_suffix)
def source_cli():
parser = argparse.ArgumentParser(
description="JSON THD Source Validator CLI with uv."
)
parser.add_argument("folder", help="Folder to check.", type=str)
parser.add_argument(
"suffix", help="Projection suffix.", default="*.json", type=Path, nargs="?"
)
args = parser.parse_args()
suffix = str(args.suffix)
if not suffix.startswith("*"):
raise ValueError(f'The suffix must always start with: "*". \nIt is: {suffix}')
validator = get_source_validator(suffix)
folder = Path(args.folder)
if not folder.exists():
raise FileNotFoundError(f"Folder: {folder} does not exist")
validator.folder(Path(args.folder))
if __name__ == "__main__":
source_cli()
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "THD X-Ray Source File Schema",
"type": "object",
"version": "0.1.9",
"date": "28.11.2024",
"properties": {
"voltage_kv": {
"type": "number",
"description": "Source voltage in kV.",
"minimum": 0.001
},
"current_ma": {
"type": "number",
"description": "Source current in µA.",
"minimum": 0.001
}
},
"required": [
"voltage_kv",
"current_ma"
],
"additionalProperties": false
}
\ No newline at end of file
import json
from pathlib import Path
from jsonschema import ValidationError, RefResolver
from jsonschema.validators import Draft202012Validator
class Validator:
def __init__(self, json_schema: Path, json_suffix: str = "*.json") -> None:
self.json_schema = json_schema
self.json_suffix = json_suffix
print(f"Validator for schema: {json_schema} \nSuffix: {json_suffix}\n")
def file(self, file_path: Path) -> bool:
with (
open(str(self.json_schema)) as schema_file,
open(str(file_path)) as data_file,
):
schema = json.load(schema_file)
data = json.load(data_file)
schema_path = self.json_schema.parent.absolute().as_uri()
resolver = RefResolver(base_uri=schema_path, referrer=schema)
try:
Draft202012Validator(
schema=schema,
format_checker=Draft202012Validator.FORMAT_CHECKER,
resolver=resolver,
).validate(data)
print(f"Validate file: {file_path.parent} / {file_path.name}\n")
except ValidationError as e:
print("Validation error:", e)
return True
def folder(self, folder_path: Path) -> bool:
print(f"Validate folder: {folder_path}\n")
for file in folder_path.glob(self.json_suffix):
self.file(file)
return True
def get_schema(self):
schema_path = self.json_schema.parent.absolute().as_uri()
with open(str(self.json_schema)) as schema_file:
schema = json.load(schema_file)
resolver = RefResolver(base_uri=schema_path, referrer=schema)
return self.resolve_ref(resolver, schema)
def resolve_ref(self, resolver, schema_part):
if isinstance(schema_part, dict) and "$ref" in schema_part:
# Resolve the reference
ref = schema_part["$ref"]
with resolver.resolving(ref) as resolved:
return self.resolve_ref(
resolver, resolved
) # Recursively resolve nested references
elif isinstance(schema_part, dict):
# Resolve each value in the dictionary
return {
key: self.resolve_ref(resolver, value)
for key, value in schema_part.items()
}
elif isinstance(schema_part, list):
# Resolve each item in the list
return [self.resolve_ref(resolver, item) for item in schema_part]
return schema_part
from thd_json import Validator
from pathlib import Path
import argparse
def get_volume_validator(json_suffix: str = "*.json") -> Validator:
return Validator(Path(__file__).parent / Path("volume.json"), json_suffix)
def volume_cli():
parser = argparse.ArgumentParser(
description="JSON THD Volume Validator CLI with uv."
)
parser.add_argument("folder", help="Folder to check.", type=str)
parser.add_argument(
"suffix", help="Projection suffix.", default="*.json", type=Path, nargs="?"
)
args = parser.parse_args()
suffix = str(args.suffix)
if not suffix.startswith("*"):
raise ValueError(f'The suffix must always start with: "*". \nIt is: {suffix}')
validator = get_volume_validator(suffix)
folder = Path(args.folder)
if not folder.exists():
raise FileNotFoundError(f"Folder: {folder} does not exist")
validator.folder(Path(args.folder))
if __name__ == "__main__":
volume_cli()
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "THD Projection Volume File Schema",
"type": "object",
"version": "0.1.9",
"date": "28.11.2024",
"properties": {
"header": {
"$ref": "../header/header.json"
},
"volume_path": {
"type": "string",
"description": "Path to TIFF file.",
"pattern": "^.*\\.(tif|tiff)$"
}
},
"required": [
"header",
"volume_path"
],
"additionalProperties": false
}
\ No newline at end of file
This diff is collapsed.