2 # Copyright (C) 2007-2013 EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 """Classe qui exprime une contrainte multiple pour un attribut"""
23 def __init__(self, *args):
24 self.values = list(args)
26 def add_value(self, value):
27 if value not in self.values:
28 self.values.append(value)
30 def __call__(self, obj, name, value, log):
31 if value not in self.values:
32 log.err(obj, "l'attribut %s=%r n'est pas dans %r" %
33 (name, value, self.values))
37 g = [repr(v) for v in self.values]
38 l.append(", ".join(g))
43 class CheckLog(object):
45 """Un validateur qui enregistre toutes les erreurs trouvées.
46 checkedXXX répond True si la "marq" courante est inférieure ou égale
47 à la celle de la dernière vérification.
48 Si on incrémentait "marq" à chaque étape, on revérifie à chaque fois.
57 self._lastmarq = self._marq
59 self._profond = False # True pour forcer des vérifications plus profondes
61 def log(self, level, obj, msg):
63 self.msg.append((level, obj.nomj(), msg))
65 self.msg.append((level, 'None', msg))
67 def err(self, obj, msg):
70 def warn(self, obj, msg):
73 def visitOJB(self, obj):
75 self.names[key] = self._marq
77 def checkSumOJB(self, obj, sd, maj='non'):
78 # vérifie que le checksum de obj n'a pas changé
79 # sd : concept qui contient obj
80 # maj='maj', l'opérateur a le droit de modifier ojb
84 m.update(str(obj.get()))
87 if not self.cksums.has_key(nom):
88 self.cksums[nom] = cksum
90 if self.cksums[nom] != cksum:
91 self.cksums[nom] = cksum
92 # if maj.strip()=='maj' and nom[0:8].strip()==sd.nomj.nomj[0:8].strip() :
93 # Remarque : ne pas tester 'maj' premet de résoudre (un peu) le problème
94 # posé par la commande DETRUIRE
95 if nom[0:8].strip() == sd.nomj.nomj[0:8].strip():
98 self.err(obj, 'Le checksum a changé')
100 def visitAsBase(self, obj):
101 key = (obj.nomj(), obj.__class__.__name__)
102 self.names[key] = self._marq
104 def force(self, force=False):
109 self._marq = self._lastmarq
111 def checkedOJB(self, obj):
113 res = self.names.get(key, 0) >= self._marq
114 self.help_dbg([key, ], res)
117 def checkedAsBase(self, obj):
118 key = (obj.nomj(), obj.__class__.__name__)
119 res = self.names.get(key, 0) >= self._marq
120 self.help_dbg(key, res)
123 def help_dbg(self, key, res):
129 print '#DBG %6d %s : %s' % (self._marq, s, ', '.join(key))
133 return "\n".join(["%s:%s: %s" % (d[l], n, m)
134 for l, n, m in self.msg])
137 class CheckFail(CheckLog):
139 """Un validateur qui lève une exception
140 dès la première erreur"""
142 def err(self, obj, msg):
143 raise AssertionError("%s: %s" % (obj.nomj(), msg))