2012-06-22 13:10:04 +00:00
|
|
|
""" View validation code (using assertions, not the RNG schema). """
|
|
|
|
|
2012-07-30 05:37:22 +00:00
|
|
|
|
2012-06-22 13:10:04 +00:00
|
|
|
def valid_page_in_book(arch):
|
|
|
|
"""A `page` node must be below a `book` node."""
|
|
|
|
return not arch.xpath('//page[not(ancestor::notebook)]')
|
|
|
|
|
2012-07-30 05:37:22 +00:00
|
|
|
|
2012-07-03 06:07:37 +00:00
|
|
|
def valid_field_in_graph(arch):
|
|
|
|
"""A `field` node must be below a `graph` node."""
|
2012-07-26 09:09:13 +00:00
|
|
|
return not arch.xpath('//graph[not ((field) and (@string))]')
|
2012-07-03 06:07:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def valid_field_in_tree(arch):
|
|
|
|
"""A `field` and `button` node must be below a `tree` node."""
|
2012-07-26 09:09:13 +00:00
|
|
|
return not arch.xpath('//tree[not((field) and (button) and (@string))]')
|
2012-07-03 06:07:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def valid_att_in_field(arch):
|
|
|
|
"""A `name` attribute must be in a `field` node."""
|
|
|
|
return not arch.xpath('//field[not (@name)]')
|
|
|
|
|
2012-07-30 05:37:22 +00:00
|
|
|
|
2012-07-03 06:07:37 +00:00
|
|
|
def valid_att_in_label(arch):
|
2012-07-03 08:51:27 +00:00
|
|
|
"""A `for` and `string` attribute must be in a `label` node."""
|
2012-07-03 06:07:37 +00:00
|
|
|
return not arch.xpath('//label[not ((@for) or (@string))]')
|
|
|
|
|
|
|
|
|
|
|
|
def valid_att_in_form(arch):
|
|
|
|
"""A `string` attribute must be in a `form` node."""
|
|
|
|
return not arch.xpath('//form[not (@string)]')
|
|
|
|
|
|
|
|
|
|
|
|
def valid_type_in_colspan(arch):
|
|
|
|
"""A `colspan` attribute must be an `integer` type."""
|
2012-07-03 08:46:20 +00:00
|
|
|
for attrib in arch.xpath('//*/@colspan'):
|
|
|
|
try:
|
|
|
|
int(attrib)
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return True
|
2012-07-30 05:37:22 +00:00
|
|
|
|
|
|
|
|
2012-07-03 06:07:37 +00:00
|
|
|
def valid_type_in_col(arch):
|
|
|
|
"""A `col` attribute must be an `integer` type."""
|
2012-07-03 08:46:20 +00:00
|
|
|
for attrib in arch.xpath('//*/@col'):
|
|
|
|
try:
|
|
|
|
int(attrib)
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return True
|
2012-07-03 06:07:37 +00:00
|
|
|
|
|
|
|
|
2012-06-22 13:10:04 +00:00
|
|
|
def valid_view(arch):
|
|
|
|
if arch.tag == 'form':
|
2012-07-30 05:37:22 +00:00
|
|
|
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]:
|
2012-06-22 13:10:04 +00:00
|
|
|
if not pred(arch):
|
|
|
|
return False
|
2012-07-03 06:07:37 +00:00
|
|
|
elif arch.tag == 'graph':
|
2012-07-30 05:37:22 +00:00
|
|
|
for pred in [valid_field_in_graph,valid_type_in_colspan,valid_type_in_col,\
|
|
|
|
valid_att_in_field,valid_att_in_label]:
|
2012-07-03 06:07:37 +00:00
|
|
|
if not pred(arch):
|
|
|
|
return False
|
|
|
|
elif arch.tag == 'tree':
|
2012-07-30 05:37:22 +00:00
|
|
|
for pred in [valid_field_in_tree,valid_type_in_colspan,valid_type_in_col,\
|
|
|
|
valid_att_in_field,valid_att_in_label]:
|
2012-07-03 06:07:37 +00:00
|
|
|
if not pred(arch):
|
|
|
|
return False
|
2012-06-22 13:10:04 +00:00
|
|
|
return True
|