-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
191 lines (161 loc) · 5.81 KB
/
cli.py
File metadata and controls
191 lines (161 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Command-line interface for OOJS validation.
Usage:
python -m oojs validate --schema clinical.oojs.json --type Encounter instance.json
python -m oojs validate --schema clinical.oojs.json --type Observation - < instance.json
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from .loader import Registry, SchemaError
from .validator import Validator
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="oojs",
description="OOJS v1.0 reference validator",
)
sub = parser.add_subparsers(dest="command", required=True)
# validate subcommand
val = sub.add_parser("validate", help="Validate an instance against a schema type")
val.add_argument(
"--schema", "-s",
required=True,
metavar="FILE",
help="Path to the .oojs.json schema file",
)
val.add_argument(
"--type", "-t",
required=True,
metavar="TYPE",
dest="target_type",
help="Target type name to validate against",
)
val.add_argument(
"--fail-fast", "-f",
action="store_true",
help="Stop after the first validation error",
)
val.add_argument(
"--import", "-i",
action="append",
metavar="ALIAS=FILE",
dest="imports",
help="Pre-load an imported schema (alias=path); may be repeated",
)
val.add_argument(
"instance",
metavar="INSTANCE",
help="Path to the JSON instance file, or '-' for stdin",
)
# inspect subcommand
insp = sub.add_parser("inspect", help="Print resolved type information from a schema")
insp.add_argument("schema", metavar="FILE", help="Path to the .oojs.json schema file")
insp.add_argument("--type", "-t", metavar="TYPE", dest="target_type",
help="Show details for a single type")
return parser
def cmd_validate(args: argparse.Namespace) -> int:
registry = Registry()
# Pre-load imports
for imp in args.imports or []:
if "=" not in imp:
print(f"error: --import must be in 'alias=file' format, got '{imp}'", file=sys.stderr)
return 2
alias, path = imp.split("=", 1)
try:
registry.load_file(path)
except (SchemaError, OSError, json.JSONDecodeError) as e:
print(f"error loading import '{alias}' from '{path}': {e}", file=sys.stderr)
return 2
# Load main schema
try:
schema = registry.load_file(args.schema)
except (SchemaError, OSError, json.JSONDecodeError) as e:
print(f"error loading schema '{args.schema}': {e}", file=sys.stderr)
return 2
# Resolve target type
typedef = schema.types.get(args.target_type)
if typedef is None:
print(
f"error: type '{args.target_type}' not found in schema '{schema.schema_id}'",
file=sys.stderr,
)
return 2
# Load instance
try:
if args.instance == "-":
raw = sys.stdin.read()
else:
raw = Path(args.instance).read_text(encoding="utf-8")
instance = json.loads(raw)
except (OSError, json.JSONDecodeError) as e:
print(f"error reading instance: {e}", file=sys.stderr)
return 2
# Validate
validator = Validator(registry, fail_fast=args.fail_fast)
errors = validator.validate(instance, typedef, schema)
if not errors:
print("valid")
return 0
for err in errors:
print(err)
return 1
def cmd_inspect(args: argparse.Namespace) -> int:
registry = Registry()
try:
schema = registry.load_file(args.schema)
except (SchemaError, OSError, json.JSONDecodeError) as e:
print(f"error: {e}", file=sys.stderr)
return 2
if args.target_type:
typedef = schema.types.get(args.target_type)
if typedef is None:
print(f"error: type '{args.target_type}' not found", file=sys.stderr)
return 2
_print_type(typedef, schema)
else:
for name, typedef in schema.types.items():
_print_type(typedef, schema)
print()
return 0
def _print_type(typedef, schema) -> None:
flags = []
if typedef.abstract:
flags.append("abstract")
flag_str = f" [{', '.join(flags)}]" if flags else ""
parent = f" extends {typedef.supertype.name}" if typedef.supertype else ""
print(f"Type: {typedef.name}{parent}{flag_str}")
print(f" discriminatorValue: {typedef.effective_discriminator_value}")
eff_props = typedef.effective_properties()
eff_req = set(typedef.effective_required())
if eff_props:
print(" properties:")
for pname, pdef in eff_props.items():
req_mark = "*" if pname in eff_req else " "
from .model import PrimitiveProperty, TypeRefProperty, ArrayProperty
if isinstance(pdef, PrimitiveProperty):
ptype = pdef.kind
elif isinstance(pdef, TypeRefProperty):
ptype = pdef.type_name
elif isinstance(pdef, ArrayProperty):
from .model import TypeRefProperty as TRP
item_type = (
pdef.items.kind if isinstance(pdef.items, PrimitiveProperty)
else pdef.items.type_name if isinstance(pdef.items, TRP)
else "?"
)
ptype = f"array<{item_type}>"
else:
ptype = "?"
print(f" {req_mark} {pname}: {ptype}")
def main(argv: list[str] | None = None) -> int:
parser = _build_parser()
args = parser.parse_args(argv)
if args.command == "validate":
return cmd_validate(args)
if args.command == "inspect":
return cmd_inspect(args)
parser.print_help()
return 2
if __name__ == "__main__":
sys.exit(main())