1 # -*- coding: iso-8859-1 -*-
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
22 """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" % (name, value, self.values) )
36 g = [ repr(v) for v in self.values ]
37 l.append( ", ".join(g) )
41 class CheckLog(object):
42 """Un validateur qui enregistre toutes les erreurs trouvées.
43 checkedXXX répond True si la "marq" courante est inférieure ou égale
44 à la celle de la dernière vérification.
45 Si on incrémentait "marq" à chaque étape, on revérifie à chaque fois.
54 self._lastmarq = self._marq
56 self._profond = False # True pour forcer des vérifications plus profondes
58 def log(self, level, obj, msg ):
60 self.msg.append( (level, obj.nomj(), msg) )
62 self.msg.append( (level, 'None', msg) )
64 def err(self, obj, msg ):
65 self.log( 0, obj, msg )
67 def warn(self, obj, msg ):
68 self.log( 1, obj, msg )
70 def visitOJB(self, obj):
72 self.names[key] = self._marq
74 def checkSumOJB(self, obj, sd, maj='non'):
75 # vérifie que le checksum de obj n'a pas changé
76 # sd : concept qui contient obj
77 # maj='maj', l'opérateur a le droit de modifier ojb
81 m.update(str(obj.get()))
84 if not self.cksums.has_key(nom) :
85 self.cksums[nom]=cksum
87 if self.cksums[nom] != cksum :
88 self.cksums[nom] = cksum
89 #if maj.strip()=='maj' and nom[0:8].strip()==sd.nomj.nomj[0:8].strip() :
90 # Remarque : ne pas tester 'maj' premet de résoudre (un peu) le problème
91 # posé par la commande DETRUIRE
92 if nom[0:8].strip()==sd.nomj.nomj[0:8].strip() :
95 self.err(obj,'Le checksum a changé')
97 def visitAsBase(self, obj):
98 key = (obj.nomj(), obj.__class__.__name__)
99 self.names[key] = self._marq
101 def force(self, force=False):
106 self._marq = self._lastmarq
108 def checkedOJB(self, obj):
110 res = self.names.get(key, 0) >= self._marq
111 self.help_dbg([key,], res)
114 def checkedAsBase(self, obj):
115 key = (obj.nomj(), obj.__class__.__name__)
116 res = self.names.get(key, 0) >= self._marq
117 self.help_dbg(key, res)
120 def help_dbg(self, key, res):
126 print '#DBG %6d %s : %s' % (self._marq, s, ', '.join(key))
129 d = { 0: "E", 1:"W" }
130 return "\n".join( [ "%s:%s: %s" % (d[l],n,m)
131 for l,n,m in self.msg ])
133 class CheckFail(CheckLog):
134 """Un validateur qui lève une exception
135 dès la première erreur"""
136 def err(self, obj, msg ):
137 raise AssertionError("%s: %s" % (obj.nomj(), msg) )