-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_validation.py
84 lines (64 loc) · 2.48 KB
/
view_validation.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
""" View validation code (using assertions, not the RNG schema). """
import logging
_logger = logging.getLogger(__name__)
def valid_page_in_book(arch):
"""A `page` node must be below a `book` node."""
return not arch.xpath('//page[not(ancestor::notebook)]')
def valid_field_in_graph(arch):
"""A `graph` must have `string` attribute and an immediate node of `graph` view must be `field`."""
if arch.xpath('//graph[not (@string)]'):
return False
for child in arch.xpath('/graph/child::*'):
if child.tag != 'field':
return False
return True
def valid_field_in_tree(arch):
"""A `tree` must have `string` attribute and an immediate node of `tree` view must be `field` or `button`."""
if arch.xpath('//tree[not (@string)]'):
return False
for child in arch.xpath('/tree/child::*'):
if child.tag not in ('field', 'button'):
return False
return True
def valid_att_in_field(arch):
"""A `name` attribute must be in a `field` node."""
return not arch.xpath('//field[not (@name)]')
def valid_att_in_label(arch):
"""A `for` and `string` attribute must be on a `label` node."""
return not arch.xpath('//label[not ((@for) or (@string))]')
def valid_att_in_form(arch):
return True
def valid_type_in_colspan(arch):
"""A `colspan` attribute must be an `integer` type."""
for attrib in arch.xpath('//*/@colspan'):
try:
int(attrib)
except:
return False
return True
def valid_type_in_col(arch):
"""A `col` attribute must be an `integer` type."""
for attrib in arch.xpath('//*/@col'):
try:
int(attrib)
except:
return False
return True
def valid_view(arch):
if arch.tag == 'form':
for pred in [valid_page_in_book, valid_att_in_form, valid_type_in_colspan,
valid_type_in_col, valid_att_in_field, valid_att_in_label]:
if not pred(arch):
_logger.error('Invalid XML: %s', pred.__doc__)
return False
elif arch.tag == 'graph':
for pred in [valid_field_in_graph, valid_att_in_field]:
if not pred(arch):
_logger.error('Invalid XML: %s', pred.__doc__)
return False
elif arch.tag == 'tree':
for pred in [valid_field_in_tree, valid_att_in_field]:
if not pred(arch):
_logger.error('Invalid XML: %s', pred.__doc__)
return False
return True