[FIX] Report: get the wkhtmltopdf version in a cleaner way with a simple regex
[odoo/odoo.git] / openerp / tools / view_validation.py
1 """ View validation code (using assertions, not the RNG schema). """
2
3 import logging
4
5 _logger = logging.getLogger(__name__)
6
7
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)]')
11
12
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)]'):
16         return False
17     for child in arch.xpath('/graph/child::*'):
18         if child.tag != 'field':
19             return False
20     return True
21
22
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)]'):
26         return False
27     for child in arch.xpath('/tree/child::*'):
28         if child.tag not in ('field', 'button'):
29             return False
30     return True
31
32
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)]')
36
37
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))]')
41
42
43 def valid_att_in_form(arch):
44     return True
45
46
47 def valid_type_in_colspan(arch):
48     """A `colspan` attribute must be an `integer` type."""
49     for attrib in arch.xpath('//*/@colspan'):
50         try:
51             int(attrib)
52         except:
53             return False
54     return True
55
56
57 def valid_type_in_col(arch):
58     """A `col` attribute must be an `integer` type."""
59     for attrib in arch.xpath('//*/@col'):
60         try:
61             int(attrib)
62         except:
63             return False
64     return True
65
66
67 def valid_view(arch):
68     if arch.tag == 'form':
69         for pred in [valid_page_in_book, valid_att_in_form, valid_type_in_colspan,
70                      valid_type_in_col, valid_att_in_field, valid_att_in_label]:
71             if not pred(arch):
72                 _logger.error('Invalid XML: %s', pred.__doc__)
73                 return False
74     elif arch.tag == 'graph':
75         for pred in [valid_field_in_graph, valid_att_in_field]:
76             if not pred(arch):
77                 _logger.error('Invalid XML: %s', pred.__doc__)
78                 return False
79     elif arch.tag == 'tree':
80         for pred in [valid_field_in_tree, valid_att_in_field]:
81             if not pred(arch):
82                 _logger.error('Invalid XML: %s', pred.__doc__)
83                 return False
84     return True