14
14
15
15
use_thread = True
16
16
17
- skip_path = (
18
- "snapshots/ebrainsquery/v1" ,
19
- "venv"
20
- )
17
+ skip_path = ("snapshots/ebrainsquery/v1" , "venv" )
21
18
22
19
skip_types = []
23
20
21
+
24
22
class ValidationResult (Enum ):
25
- SKIPPED = "SKIPPED"
26
- PASSED = "PASSED"
27
- FAILED = "FAILED"
23
+ SKIPPED = "SKIPPED"
24
+ PASSED = "PASSED"
25
+ FAILED = "FAILED"
26
+
28
27
29
28
BASE_URI = "http://example.com"
30
29
31
- ROOT_DIR = os .path .abspath (
32
- f"{ os .path .dirname (os .path .realpath (__file__ ))} /.."
33
- )
30
+ ROOT_DIR = os .path .abspath (f"{ os .path .dirname (os .path .realpath (__file__ ))} /.." )
31
+
34
32
35
33
def get_ref (schema ):
36
34
resolver = RefResolver (base_uri = BASE_URI , referrer = schema )
@@ -40,7 +38,6 @@ def get_ref(schema):
40
38
if not filename .endswith (".json" ):
41
39
continue
42
40
with open (f"{ dirpath } /{ filename } " , "r" ) as fp :
43
-
44
41
relative_path = dirpath .replace (walk_path , "" ) + "/" + filename
45
42
46
43
key0 = urljoin (BASE_URI , f"config_schema/{ relative_path } " )
@@ -50,9 +47,10 @@ def get_ref(schema):
50
47
51
48
resolver .store [key0 ] = schema
52
49
resolver .store [key1 ] = schema
53
-
50
+
54
51
return resolver
55
52
53
+
56
54
def validate_json (path_to_json , fail_fast = False ):
57
55
if any ([path_fragment in path_to_json for path_fragment in skip_path ]):
58
56
return (
@@ -65,43 +63,22 @@ def validate_json(path_to_json, fail_fast=False):
65
63
66
64
# skip list
67
65
if isinstance (json_obj , list ):
68
- return (
69
- path_to_json ,
70
- ValidationResult .SKIPPED ,
71
- None
72
- )
66
+ return (path_to_json , ValidationResult .SKIPPED , None )
73
67
_type = json_obj .get ("@type" , None )
74
68
if not _type :
75
69
# TODO consolidate how error are raied
76
70
if fail_fast :
77
71
raise ValidationError (f"type does not exist: { path_to_json } " )
78
- return (
79
- path_to_json ,
80
- ValidationResult .FAILED ,
81
- None
82
- )
83
-
72
+ return (path_to_json , ValidationResult .FAILED , None )
73
+
84
74
# assert _schema is None
85
75
if not _type or not _type .startswith ("siibra" ):
86
- return (
87
- path_to_json ,
88
- ValidationResult .SKIPPED ,
89
- None
90
- )
76
+ return (path_to_json , ValidationResult .SKIPPED , None )
91
77
if _type in skip_types :
92
- return (
93
- path_to_json ,
94
- ValidationResult .SKIPPED ,
95
- None
96
- )
97
- abspath = os .path .join (
98
- ROOT_DIR , "config_schema" , (_type + ".json" )
99
- )
78
+ return (path_to_json , ValidationResult .SKIPPED , None )
79
+ abspath = os .path .join (ROOT_DIR , "config_schema" , (_type + ".json" ))
100
80
path_to_schema = os .path .abspath (abspath )
101
- with open (
102
- path_to_schema ,
103
- "r"
104
- ) as fp :
81
+ with open (path_to_schema , "r" ) as fp :
105
82
schema = json .load (fp )
106
83
try :
107
84
resolver = get_ref (schema )
@@ -110,52 +87,48 @@ def validate_json(path_to_json, fail_fast=False):
110
87
if fail_fast :
111
88
# TODO consolidate how error are raied
112
89
raise e
113
- return (
114
- path_to_json ,
115
- ValidationResult .FAILED ,
116
- e
117
- )
118
- return (
119
- path_to_json ,
120
- ValidationResult .PASSED ,
121
- None
122
- )
123
-
90
+ return (path_to_json , ValidationResult .FAILED , e )
91
+ return (path_to_json , ValidationResult .PASSED , None )
92
+
124
93
125
94
def main (path_to_configuration : str , * args ):
126
- json_files = [f"{ dirpath } /{ filename } "
127
- for dirpath , dirnames , filenames in os .walk (path_to_configuration )
128
- for filename in filenames
129
- if filename .endswith (".json" )
130
- ]
95
+ json_files = [
96
+ f"{ dirpath } /{ filename } "
97
+ for dirpath , dirnames , filenames in os .walk (path_to_configuration )
98
+ for filename in filenames
99
+ if filename .endswith (".json" )
100
+ ]
131
101
# TODO use argparse
132
102
fail_fast = "--fail-fast" in args
133
103
if use_thread :
134
-
135
- with ThreadPoolExecutor (max_workers = 4 ) as executor :
136
- result = [progress for progress in tqdm (
137
- executor .map (
138
- validate_json ,
139
- json_files ,
140
- repeat (fail_fast )
141
- ),
142
- total = len (json_files )
143
- )]
104
+ with ThreadPoolExecutor (max_workers = 1 ) as executor :
105
+ result = [
106
+ progress
107
+ for progress in tqdm (
108
+ executor .map (validate_json , json_files , repeat (fail_fast )),
109
+ total = len (json_files ),
110
+ )
111
+ ]
144
112
else :
145
113
result = [validate_json (f , fail_fast ) for f in json_files ]
146
114
147
115
passed = [r for r in result if r [1 ] == ValidationResult .PASSED ]
148
116
failed = [r for r in result if r [1 ] == ValidationResult .FAILED ]
149
117
skipped = [r for r in result if r [1 ] == ValidationResult .SKIPPED ]
150
- print (f"Validation results: PASSED: { len (passed )} SKIPPED: { len (skipped )} FAILED: { len (failed )} " )
118
+ print (
119
+ f"Validation results: PASSED: { len (passed )} SKIPPED: { len (skipped )} FAILED: { len (failed )} "
120
+ )
151
121
152
122
if len (failed ) > 0 :
153
123
print (failed )
154
124
# TODO consolidate how error are raied
155
- raise ValidationError (message = "\n -----\n " .join ([f"{ f [0 ]} : { str (f [2 ])} " for f in failed ]))
125
+ raise ValidationError (
126
+ message = "\n -----\n " .join ([f"{ f [0 ]} : { str (f [2 ])} " for f in failed ])
127
+ )
128
+
156
129
157
130
if __name__ == "__main__" :
158
131
args = sys .argv [1 :]
159
132
if len (args ) == 0 :
160
- raise RuntimeError (f "Need path to configuration directory" )
133
+ raise RuntimeError ("Need path to configuration directory" )
161
134
main (* args )
0 commit comments