Salome HOME
bugs + translator
[tools/eficas.git] / Noyau / ascheckers.py
1 # coding=utf-8
2 # Copyright (C) 2007-2013   EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 class Parmi(object):
20
21     """Classe qui exprime une contrainte multiple pour un attribut"""
22
23     def __init__(self, *args):
24         self.values = list(args)
25
26     def add_value(self, value):
27         if value not in self.values:
28             self.values.append(value)
29
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))
34
35     def __repr__(self):
36         l = ["Parmi(", ]
37         g = [repr(v) for v in self.values]
38         l.append(", ".join(g))
39         l.append(")")
40         return "".join(l)
41
42
43 class CheckLog(object):
44
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.
49     """
50
51     def __init__(self):
52         self.msg = []
53         self.names = {}
54         self.cksums = {}
55         self.optional = False
56         self._marq = 1
57         self._lastmarq = self._marq
58         self._debug = False
59         self._profond = False  # True pour forcer des vérifications plus profondes
60
61     def log(self, level, obj, msg):
62         if obj:
63             self.msg.append((level, obj.nomj(), msg))
64         else:
65             self.msg.append((level, 'None', msg))
66
67     def err(self, obj, msg):
68         self.log(0, obj, msg)
69
70     def warn(self, obj, msg):
71         self.log(1, obj, msg)
72
73     def visitOJB(self, obj):
74         key = obj.nomj()
75         self.names[key] = self._marq
76
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
81         if obj.exists:
82             import md5
83             m = md5.new()
84             m.update(str(obj.get()))
85             cksum = m.digest()
86             nom = obj.nomj()
87             if not self.cksums.has_key(nom):
88                 self.cksums[nom] = cksum
89             else:
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():
96                         pass
97                     else:
98                         self.err(obj, 'Le checksum a changé')
99
100     def visitAsBase(self, obj):
101         key = (obj.nomj(), obj.__class__.__name__)
102         self.names[key] = self._marq
103
104     def force(self, force=False):
105         if not force:
106             self._marq = 1
107         else:
108             self._lastmarq += 1
109             self._marq = self._lastmarq
110
111     def checkedOJB(self, obj):
112         key = obj.nomj()
113         res = self.names.get(key, 0) >= self._marq
114         self.help_dbg([key, ], res)
115         return res
116
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)
121         return res
122
123     def help_dbg(self, key, res):
124         if self._debug:
125             if res:
126                 s = 'ignore'
127             else:
128                 s = 'check '
129             print '#DBG %6d %s : %s' % (self._marq, s, ', '.join(key))
130
131     def __str__(self):
132         d = {0: "E", 1: "W"}
133         return "\n".join(["%s:%s: %s" % (d[l], n, m)
134                           for l, n, m in self.msg])
135
136
137 class CheckFail(CheckLog):
138
139     """Un validateur qui lève une exception
140     dès la première erreur"""
141
142     def err(self, obj, msg):
143         raise AssertionError("%s: %s" % (obj.nomj(), msg))