forked from 5e-bits/5e-database
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.py
More file actions
72 lines (56 loc) · 2.24 KB
/
Copy pathValidator.py
File metadata and controls
72 lines (56 loc) · 2.24 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
import fastjsonschema as fjs
import json
import os
import sys
import traceback
SCRIPT_DIRECTORY = os.path.abspath(os.path.realpath(os.path.dirname(__file__)))
DATABASE_SCHEMA_DIRECTORY = os.path.join(SCRIPT_DIRECTORY, "Schemas/DatabaseSchemas")
MISC_SCHEMA_DIRECTORY = os.path.join(SCRIPT_DIRECTORY, "Schemas/SupportingSchemas")
def load_json_file(path):
print(path)
with open(path) as json_file:
return json.load(json_file)
DATABASES = {
name: load_json_file(os.path.join(SCRIPT_DIRECTORY, name))
for name in os.listdir(SCRIPT_DIRECTORY)
if name.endswith(".json")
}
DATABASE_SCHEMAS = {
name: load_json_file(os.path.join(DATABASE_SCHEMA_DIRECTORY, name))
for name in os.listdir(DATABASE_SCHEMA_DIRECTORY)
if name.endswith(".schema.json")
}
MISC_SCHEMAS = {
name: load_json_file(os.path.join(MISC_SCHEMA_DIRECTORY, name))
for name in os.listdir(MISC_SCHEMA_DIRECTORY)
if name.endswith(".schema.json")
}
def get_schema(schema_name):
# print(schema_name)
schema_name = schema_name.split('/')[-1]
if schema_name in MISC_SCHEMAS:
return MISC_SCHEMAS[schema_name]
elif schema_name in DATABASE_SCHEMAS:
return DATABASE_SCHEMAS[schema_name]
raise Exception(f'Unable to find schema {schema_name}')
handlers = {'': get_schema}
for database_name, database in DATABASES.items():
database_base_name = database_name.replace('.json', '')
database_schema_name = database_base_name + '.schema.json'
database_schema = DATABASE_SCHEMAS.get(database_schema_name)
if not database_schema:
print(f'No schema for {database_name}, skipping validation.')
continue
try:
fjs.validate(database_schema, database, handlers=handlers)
except fjs.JsonSchemaException as jsce:
print(f'Validating database {database_name} with schema {database_schema_name}')
with open(os.path.join(SCRIPT_DIRECTORY, 'generated'), 'w') as tmp_gen:
tmp_gen.write(fjs.compile_to_code(database_schema, handlers=handlers))
traceback.print_exc()
print(f'path={jsce.path}')
print()
except Exception as e:
print(f'Validating database {database_name} with schema {database_schema_name}')
traceback.print_exc()
print()