-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcli.py
More file actions
68 lines (56 loc) · 1.98 KB
/
Copy pathcli.py
File metadata and controls
68 lines (56 loc) · 1.98 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
import sys
import argparse
from pydantic import ValidationError
import yaml
from pydantic_yaml import parse_yaml_raw_as
from opltools.schema import Library
UNIQUE_FIELDS = ["name"]
UNIQUE_WARNING_FIELDS = ["reference", "implementation"]
def cmd_validate(args):
try:
with open(args.file, "r") as f:
raw = f.read()
lib = parse_yaml_raw_as(Library, raw)
Library.model_validate(
lib,
context={
"unique_error_fields": args.unique_error_field,
"unique_warning_fields": args.unique_warning_field,
},
)
print(f"{args.file}: OK")
return 0
except ValidationError as e:
for error in e.errors():
loc = (
" -> ".join(str(p) for p in error["loc"]) if error["loc"] else "(root)"
)
print(f"{args.file}: {loc}: {error['msg']}")
return 1
def main():
parser = argparse.ArgumentParser(prog="opl", description="OPL tools")
subparsers = parser.add_subparsers(dest="command", required=True)
validate_parser = subparsers.add_parser(
"validate", help="Validate a YAML file against the Library schema"
)
validate_parser.add_argument("file", help="YAML file to validate")
# Add unique error fields
validate_parser.add_argument(
"--unique-error-field",
action="append",
help="Field that must be unique across all entries (can be specified multiple times)",
)
validate_parser.add_argument(
"--unique-warning-field",
action="append",
help="Field that should be unique across all entries (can be specified multiple times)",
)
# specify default unique fields if not provided
validate_parser.set_defaults(
unique_error_field=UNIQUE_FIELDS, unique_warning_field=UNIQUE_WARNING_FIELDS
)
args = parser.parse_args()
if args.command == "validate":
sys.exit(cmd_validate(args))
if __name__ == "__main__":
main()