-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidator.py
51 lines (46 loc) · 1.29 KB
/
validator.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
from jsonschema import validate
import yaml
import os.path
import sys
if '--help' in sys.argv or '-h' in sys.argv or '?' in sys.argv:
print("USAGE:", sys.argv[0], 'file_to_validate.yaml', '[another_file.yaml ...]')
print('If given no arguments, reads YAML from stdin')
quit()
try:
schema = yaml.safe_load(open(os.path.join(os.path.dirname(__file__), 'GEDCOM.io', 'yaml-schema.yaml')))
except:
print("Fatal Error! GEDCOM.io/yaml-schema.yaml not found.", file=sys.stderr)
exit(1)
def check(data, name):
global schema
try:
validate(data, schema)
return True
except BaseException as ex:
print("="*30, file=sys.stderr)
print("Validation error with", name, file=sys.stderr)
print(ex)
return False
count = 0
ok = 0
if len(sys.argv) > 1:
for arg in sys.argv[1:]:
count += 1
try:
data = yaml.safe_load(open(arg))
except BaseException as ex:
print("="*30, file=sys.stderr)
print("Failed to load", arg, file=sys.stderr)
print(ex, file=sys.stderr)
continue
if check(data, arg): ok += 1
else:
for data in yaml.safe_load_all(sys.stdin):
count += 1
if check(data, 'YAML sent to stdin'): ok += 1
if ok != count:
print("="*30+'\n')
print("YAML files checked:",count)
print("YAML files passed:",ok)
if ok != count:
sys.exit(1)