forked from IHEC/ihec-ecosystems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ihec_validator_base.py
61 lines (48 loc) · 2.22 KB
/
ihec_validator_base.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
from .utils import logger
from . import egautils
class IHECJsonValidator(object):
def __init__(self, validators):
self.validators = validators
self.errorlog = list()
self.semanticlog = list()
def is_valid_ihec(self):
validated = list()
invalid = list()
for (xml, attrs) in self.xmljson:
(version, title_sanitized) = self.latest_valid_spec(attrs)
assert title_sanitized, '__regression_failure__'
try:
#if True:
semantics_ok, failed_rules = self.validate_semantics(attrs)
#else:
except Exception as err:
semantics_ok, failed_rules = False, ['__exception_in_semantic_validation__:' + str(err)]
if not semantics_ok: assert failed_rules
if version and semantics_ok:
validated.append((version, xml, egautils.obj_id(attrs) ))
logger("# is valid ihec spec:{0} version:{1} [{2}]\n".format('True', version, title_sanitized))
else:
logger("# is valid ihec spec:{0} version:{1} [{2}]\n".format('False', '__invalid__', title_sanitized))
if version and not semantics_ok:
logger("# found a valid spec version but failed semantic validation:{2}\n".format('', '', title_sanitized))
if not semantics_ok: assert failed_rules
self.semanticlog.append({title_sanitized : {'semantics_ok' : semantics_ok, "failed_rules": failed_rules}})
return validated
def validate_semantics(self, attrs):
raise NotImplementedError('__mustOverride__')
def latest_valid_spec(self, attributes):
attrs = attributes['attributes']
all_valid_versions = list()
for version in self.validators:
validator = self.validators[version]
print('__checking_against_schema:', version, self.validators[version].f)
valid, errlog = validator.validate(attrs, details=attributes, schema_version=version)
title_sanitized = egautils.obj_id(attributes) #logger.trystr(attributes['title']) # .decode('ascii', 'ignore')
#logger("# is valid ihec spec:{0} version:{1} [{2}]\n".format(valid, version if valid else '__invalid__', title_sanitized ))
self.errorlog.append(errlog)
if valid:
all_valid_versions.append((version, title_sanitized))
return (version, title_sanitized)
return (None, title_sanitized) if len(all_valid_versions) == 0 else all_valid_versions[0]
def validate():
return False