forked from yahuarkuntur/gdimm2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathval.py
108 lines (77 loc) · 3.33 KB
/
val.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
105
#!/usr/bin/env python
###
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
###
import os
from lxml import etree
import glob
from datetime import date, datetime
class Validator:
xml_parser = None
val_xml = None
val_xsl = None
validations = None
def __init__(self):
self.parser = etree.XMLParser(remove_comments=True, encoding='utf8')
self.validations = []
def load_xml(self, version):
files = glob.glob(os.path.join('XSL', 'VAL*.xml'))
for filename in files:
xml = etree.parse(filename, self.parser)
root = xml.getroot()
if root.attrib.get('version') == version:
self.val_xml = xml
return True
return False
def load_xsl(self, filename):
self.val_xsl = etree.XSLT(etree.parse(os.path.join('XSLT', filename), self.parser))
def get_validations(self):
return self.validations
def validate(self, test_xml):
if self.val_xml is None:
print 'XML de validaciones no definido!'
return
if self.val_xsl is None:
print 'XSL de validaciones no definido!'
return
self.validations = []
# iteramos los campos de la declaracion
for node in test_xml.find('detalle'):
numero = node.attrib.get('numero')
valor = node.text
# obtenemos las formulas de validacion del campo
campos = self.val_xml.find('/campo[@numero="'+numero+'"]')
# no hay validaciones para este campo... continuar
if campos is None:
continue
# iterar y aplicar cada formula de calculo
for formula in campos:
tipo = formula.attrib.get('tipoFormula')
if tipo != "V":
continue
validacion = formula.attrib.get('validacion')
mensajeError = formula.attrib.get('mensajeError')
severidad = formula.attrib.get('severidad')
condicionFormulaCalculo = formula.attrib.get('condicionFormulaCalculo')
fecha_vigencia = formula.attrib.get('fechaVigenciaHasta')
fecha_vigencia.strip()
# solo calculos vigentes
if fecha_vigencia != "" and datetime.today() > datetime.strptime(fecha_vigencia, "%Y%m%d"):
continue
result = self.val_xsl(test_xml, formula=validacion, condicion=condicionFormulaCalculo)
new_val = str(result.find('value').text).strip()
if new_val != 'true':
self.validations.append({'campo': numero, 'severidad': severidad, 'error': mensajeError})