-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdative-xml-validate.py
104 lines (78 loc) · 3.01 KB
/
dative-xml-validate.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
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
98
99
100
101
102
103
104
"""dative-xml-validate.py is a Python script that validates a Dative XML
document against a Dative Relax NG pattern (schema) in compact form.
Usage::
$ python dative-xml-validate.py xmlfile-to-be-validated.xml dative-xml.rnc
If there only one argument is provided, this script assumes there is a file in
its directory called dative-xml.rnc::
$ python dative-xml-validate.py xmlfile-to-be-validated.xml
Requirements:
- lxml
- rnc2rng
It converts the .rnc file to a .rng file and prints validation success or
errors to stdout.
Note: if you have Trang installed, you can convert the .rng file to an XML
Schema Datatype (.xsd) document with the following command::
$ trang -I rng -O xsd dative-xml.rng dative-xml.xsd
"""
from lxml import etree
from rnc2rng import parser, serializer
import os
import sys
OKGREEN = '\033[92m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def fail(txt):
print('{fail}{txt}{end}'.format(fail=FAIL, txt=txt, end=ENDC))
def ok(txt):
print('{ok}{txt}{end}'.format(ok=OKGREEN, txt=txt, end=ENDC))
def write_rnc_to_rng(rnc_file_path, rng_file_path):
if not os.path.isfile(rnc_file_path):
fail('There is no Dative Relax NG (compact) file at %s.' %
rnc_file_path)
sys.exit(1)
with open(rnc_file_path) as input_:
try:
xml = serializer.XMLSerializer().toxml(parser.parse(f=input_))
except parser.ParseError as e:
fail('rnc2rng parse error ' + e.msg)
sys.exit(1)
with open(rng_file_path, 'w') as output:
output.write(xml + '\n')
def parse_rng(rng_file_path):
with open(rng_file_path) as f:
relaxng_doc = etree.parse(f)
relaxng = etree.RelaxNG(relaxng_doc)
return relaxng
def validate(xml_file_path, rnc_file_path):
rng_file_path = '%s.rng' % os.path.splitext(rnc_file_path)[0]
write_rnc_to_rng(rnc_file_path, rng_file_path)
relaxng = parse_rng(rng_file_path)
if not os.path.isfile(xml_file_path):
fail('There is no Dative XML file at %s.' % xml_file_path)
sys.exit(1)
with open(xml_file_path) as f:
doc = etree.parse(f)
try:
relaxng.assertValid(doc)
ok('{} is a valid Dative XML document.'.format(xml_file_path))
except etree.DocumentInvalid:
fail('{} is NOT a valid Dative XML document:'.format(xml_file_path))
"""
for attr in dir(relaxng.error_log.last_error):
if not attr.startswith('__'):
print('{}: {}'.format(attr, getattr(relaxng.error_log.last_error, attr)))
"""
fail(relaxng.error_log.last_error)
def exit_usage():
sys.exit('Usage: dative-xml-validate.py file-to-be-validated.xml (dative-xml.rnc)')
if __name__ == '__main__':
if len(sys.argv) == 3:
xml_file_path, rnc_file_path = sys.argv[1:]
elif len(sys.argv) == 2:
rnc_file_path = 'dative-xml.rnc'
if not os.path.isfile(rnc_file_path):
exit_usage()
xml_file_path = sys.argv[1]
else:
exit_usage()
validate(xml_file_path, rnc_file_path)