1 """ View validation code (using assertions, not the RNG schema). """
5 _logger = logging.getLogger(__name__)
8 def valid_page_in_book(arch):
9 """A `page` node must be below a `book` node."""
10 return not arch.xpath('//page[not(ancestor::notebook)]')
13 def valid_field_in_graph(arch):
14 """A `graph` must have `string` attribute and an immediate node of `graph` view must be `field`."""
15 if arch.xpath('//graph[not (@string)]'):
17 for child in arch.xpath('/graph/child::*'):
18 if child.tag != 'field':
23 def valid_field_in_tree(arch):
24 """A `tree` must have `string` attribute and an immediate node of `tree` view must be `field` or `button`."""
25 if arch.xpath('//tree[not (@string)]'):
27 for child in arch.xpath('/tree/child::*'):
28 if child.tag not in ('field', 'button'):
33 def valid_att_in_field(arch):
34 """A `name` attribute must be in a `field` node."""
35 return not arch.xpath('//field[not (@name)]')
38 def valid_att_in_label(arch):
39 """A `for` and `string` attribute must be on a `label` node."""
40 return not arch.xpath('//label[not ((@for) or (@string))]')
43 def valid_att_in_form(arch):
44 """A `string` attribute must be on a `form` node."""
45 return not arch.xpath('//form[not (@string)]')
48 def valid_type_in_colspan(arch):
49 """A `colspan` attribute must be an `integer` type."""
50 for attrib in arch.xpath('//*/@colspan'):
58 def valid_type_in_col(arch):
59 """A `col` attribute must be an `integer` type."""
60 for attrib in arch.xpath('//*/@col'):
69 if arch.tag == 'form':
70 for pred in [valid_page_in_book, valid_att_in_form, valid_type_in_colspan,\
71 valid_type_in_col, valid_att_in_field, valid_att_in_label]:
73 _logger.error('Invalid XML: %s', pred.__doc__)
75 elif arch.tag == 'graph':
76 for pred in [valid_field_in_graph, valid_att_in_field]:
78 _logger.error('Invalid XML: %s', pred.__doc__)
80 elif arch.tag == 'tree':
81 for pred in [valid_field_in_tree, valid_att_in_field]:
83 _logger.error('Invalid XML: %s', pred.__doc__)