-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
98 lines (85 loc) · 3.37 KB
/
script.py
File metadata and controls
98 lines (85 loc) · 3.37 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
import json
import jsonschema
from jsonschema import validate, Draft6Validator, Draft7Validator
import glob
import logging
logging.basicConfig(filename="sample.log", level=logging.INFO)
def validate_schemas():
path = 'schema/*.schema'
files = glob.glob(path)
for filename in files:
try:
with open(filename) as data:
jsonschema = json.load(data)
Draft7Validator.check_schema(jsonschema)
print(filename + ' scheme validation successful')
except jsonschema.exceptions.ValidationError as err:
print(err.message)
return False
except IOError as exc:
if exc.errno != errno.EISDIR:
raise
return True
def validate_cmarker_created(jsonData, filename):
try:
with open('schema/cmarker_created.schema') as schema:
jsonSchema = json.load(schema)
Draft7Validator(jsonSchema).validate(jsonData)
except jsonschema.exceptions.ValidationError as err:
logging.error(filename + ' is invalid with cmarker_created.schema. The error is: ' + err.message)
print(err.message)
return False
return True
def validate_label_selected(jsonData, filename):
try:
with open('schema/label_selected.schema') as schema:
jsonSchema = json.load(schema)
Draft7Validator(jsonSchema).validate(jsonData)
except jsonschema.exceptions.ValidationError as err:
logging.error(filename + ' is invalid with label_selected.schema. The error is: ' + err.message)
print(err.message)
return False
return True
def validate_sleep_created(jsonData, filename):
try:
with open('schema/sleep_created.schema') as schema:
jsonSchema = json.load(schema)
Draft7Validator(jsonSchema).validate(jsonData)
except jsonschema.exceptions.ValidationError as err:
logging.error(filename + ' is invalid with sleep_created.schema. The error is: ' + err.message)
print(err.message)
return False
return True
def validate_workout_created(jsonData, filename):
try:
with open('schema/workout_created.schema') as schema:
jsonSchema = json.load(schema)
Draft7Validator(jsonSchema).validate(jsonData)
except jsonschema.exceptions.ValidationError as err:
logging.error(filename + ' is invalid with workout_created.schema. The error is: ' + err.message)
print(err.message)
return False
return True
if __name__ == '__main__':
# validating schemas
print('Check if schemas are valid.........')
if validate_schemas():
print('...all schemas are valid')
logging.info("Given schemas are valid")
else:
print('...schemas are invalid')
logging.error("Given schemas are invalid")
# validating json with schemas
path = 'event/*.json'
files = glob.glob(path)
for filename in files:
try:
with open(filename) as data:
jsondata = json.load(data)
validate_cmarker_created(jsondata, filename)
validate_label_selected(jsondata, filename)
validate_sleep_created(jsondata, filename)
validate_workout_created(jsondata, filename)
except IOError as exc:
if exc.errno != errno.EISDIR:
raise