forked from marcvs/aarc_scim_profile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify_schema.py
executable file
·109 lines (93 loc) · 3.11 KB
/
verify_schema.py
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
#!/usr/bin/env python3
"""script to verify a SCIM resource against a (set of) SCIM schemas"""
import os
import sys
import json
import argparse
from pprint import pprint
sys.path.append(os.getcwd())
try:
from scimschema import validate
from scimschema._model.model import Model
except ImportError:
print(
"""Unable to find python lib 'schimschema'
Please run these command from the folder that also contains the `.git` folder
for this repository:
git submodule init
git submodule update
# Apply patch to allow parsing without core schema
patch -p1 < scimschema.patch
export PYTHONPATH=`pwd`/scimschema
"""
)
sys.exit(2)
def parseOptions():
"""parse commandline parameters"""
parser = argparse.ArgumentParser(description="""verify_schema.py""")
parser.add_argument("--schema", default="AARC_Schema_Parseable.json")
parser.add_argument("--vopersonschema", default="voPerson_User_Parseable.json")
parser.add_argument("--scim", default="AARC_SCIM_Example.json")
return parser.parse_args()
# def load_json_schemas(filenames: list):
# print("--------------------------------------")
# retval = {}
# for filename in filenames:
# print(F"loading {filename}")
# temp = load_json_schema(filename)
# print("--------------------------------------")
# pprint(temp)
# print(F"type: {type(temp)}")
# print("--------------------------------------")
# print(F"keys: {temp.keys()}")
# print("--------------------------------------")
# for key in temp.keys():
# retval[key]=temp
# pprint(retval)
#
#
# return retval
def load_json_schemas(filenames: list):
"""directly load a schema by filename"""
retval = {}
for filename in filenames:
with open(filename) as f:
schema = Model.load(f)
retval[schema.id] = schema
return (retval)
def load_json_schema(filename):
"""directly load a schema by filename"""
with open(filename) as f:
schema = Model.load(f)
return {schema.id: schema}
def load_json_data(filename):
"""load and parse json"""
with open(filename) as f:
data = f.read()
try:
obj = json.loads(data)
except json.decoder.JSONDecodeError as ex:
print(f"Error when decoding JSON:\n{ex}")
sys.exit(4)
return obj
args = parseOptions()
aarc_schema_file = args.schema
voperson_schema_file = args.vopersonschema
aarc_scim_file = args.scim
if not os.path.exists(aarc_schema_file) and args.schema == "AARC_Schema_Parseable.json":
print(
"""Cannot find AARC_Schema_Parseable.json.
Please create it by running:
./make_parseable.sh"""
)
sys.exit(3)
# scim_schema = load_json_schema(aarc_schema_file)
# scim_schema = load_json_schema(voperson_schema_file)
scim_schema = load_json_schemas([aarc_schema_file, voperson_schema_file])
scim_data = load_json_data(aarc_scim_file)
try:
validate(data=scim_data, extension_schema_definitions=scim_schema)
except Exception as e:
print(e)
sys.exit(1)
print("No news is good news. I.e. your validation worked")