Salome HOME
02abf28353eb19dd5c52b46ba7a799e8cd687921
[tools/eficas.git] / Validation / V_MCSIMP.py
1 # -*- coding: iso-8859-1 -*-
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 #
20
21 """
22    Ce module contient la classe mixin MCSIMP qui porte les méthodes
23    nécessaires pour réaliser la validation d'un objet de type MCSIMP
24    dérivé de OBJECT.
25
26    Une classe mixin porte principalement des traitements et est
27    utilisée par héritage multiple pour composer les traitements.
28 """
29 # Modules Python
30 import traceback
31
32 # Modules EFICAS
33 from Noyau import N_CR
34 from Noyau.N_Exception import AsException
35 from Noyau.N_VALIDATOR import ValError,TypeProtocol,CardProtocol,IntoProtocol
36 from Noyau.N_VALIDATOR import listProto
37 from Noyau.strfunc import ufmt
38
39 try :
40   from Extensions.i18n import tr
41 except :
42   def tr(txt):
43     return txt
44
45 #print '___________'
46 #print (tr(u"None n'est pas une valeur autorisée"))
47 #print '___________'
48 class MCSIMP:
49    """
50       COMMENTAIRE CCAR:
51       Cette classe est quasiment identique à la classe originale d'EFICAS
52       a part quelques changements cosmétiques et des chagements pour la
53       faire fonctionner de facon plus autonome par rapport à l'environnement
54       EFICAS
55
56       A mon avis, il faudrait aller plus loin et réduire les dépendances
57       amont au strict nécessaire.
58
59           - Est il indispensable de faire l'évaluation de la valeur dans le contexte
60             du jdc dans cette classe.
61
62           - Ne pourrait on pas doter les objets en présence des méthodes suffisantes
63             pour éviter les tests un peu particuliers sur GEOM, PARAMETRE et autres. J'ai
64             d'ailleurs modifié la classe pour éviter l'import de GEOM
65    """
66
67    CR=N_CR.CR
68
69    def __init__(self):
70       self.state='undetermined'
71       self.typeProto=TypeProtocol("type",typ=self.definition.type)
72       self.intoProto=IntoProtocol("into",into=self.definition.into,val_min=self.definition.val_min,val_max=self.definition.val_max)
73       self.cardProto=CardProtocol("card",min=self.definition.min,max=self.definition.max)
74
75    def get_valid(self):
76        if hasattr(self,'valid'):
77           return self.valid
78        else:
79           self.valid=None
80           return None
81
82    def set_valid(self,valid):
83        old_valid=self.get_valid()
84        self.valid = valid
85        self.state = 'unchanged'
86        if not old_valid or old_valid != self.valid :
87            self.init_modif_up()
88
89    def isvalid(self,cr='non'):
90       """
91          Cette méthode retourne un indicateur de validité de l'objet de type MCSIMP
92
93            - 0 si l'objet est invalide
94            - 1 si l'objet est valide
95
96          Le paramètre cr permet de paramétrer le traitement. Si cr == 'oui'
97          la méthode construit également un comte-rendu de validation
98          dans self.cr qui doit avoir été créé préalablement.
99       """
100       if self.state == 'unchanged':
101         return self.valid
102       else:
103         valid = 1
104         v=self.valeur
105         #  verification presence
106         if self.isoblig() and v == None :
107           if cr == 'oui' :
108             self.cr.fatal(_(tr(u"Mot-clé : %s obligatoire non valorisé")), self.nom)
109           valid = 0
110
111         lval=listProto.adapt(v)
112         #Pour tenir compte des Tuples
113         if hasattr(self.definition.type[0],'ntuple') :
114            try :
115               if not (type(lval[0]) is tuple) : lval=(lval,)
116            except :
117               pass
118         if lval is None:
119            valid=0
120            if cr == 'oui' :
121               self.cr.fatal(_(tr(u"None n'est pas une valeur autorisée")))
122         else:
123            # type,into ...
124            #typeProto=TypeProtocol("type",typ=self.definition.type)
125            #intoProto=IntoProtocol("into",into=self.definition.into,val_min=self.definition.val_min,val_max=self.definition.val_max)
126            #cardProto=CardProtocol("card",min=self.definition.min,max=self.definition.max)
127            #typeProto=self.definition.typeProto
128            #intoProto=self.definition.intoProto
129            #cardProto=self.definition.cardProto
130            typeProto=self.typeProto
131            intoProto=self.intoProto
132            cardProto=self.cardProto
133            if cr == 'oui' :
134                #un cr est demandé : on collecte tous les types d'erreur
135                try:
136                    for val in lval:
137                        typeProto.adapt(val)
138                except ValError,e:
139                    valid=0
140                    self.cr.fatal(*e)
141                try:
142                    for val in lval:
143                        intoProto.adapt(val)
144                except ValError,e:
145                    valid=0
146                    self.cr.fatal(*e)
147                    #self.cr.fatal(unicode(e))
148                try:
149                    cardProto.adapt(lval)
150                except ValError,e:
151                    valid=0
152                    self.cr.fatal(*e)
153                    #self.cr.fatal(unicode(e))
154                #
155                # On verifie les validateurs s'il y en a et si necessaire (valid == 1)
156                #
157                if valid and self.definition.validators:
158                    try:
159                        self.definition.validators.convert(lval)
160                    except ValError,e:
161                        self.cr.fatal(_(tr(u"Mot-clé %s invalide : %s\nCritère de validité: %s")),
162                             self.nom, str(e), self.definition.validators.info())
163                        valid=0
164            else:
165                #si pas de cr demande, on sort a la toute premiere erreur
166                try:
167                    for val in lval:
168                        typeProto.adapt(val)
169                        intoProto.adapt(val)
170                    cardProto.adapt(lval)
171                    if self.definition.validators:
172                        if hasattr(self.definition.validators,'set_MCSimp'):
173                           self.definition.validators.set_MCSimp(self)
174                        self.definition.validators.convert(lval)
175                except ValError,e:
176                    valid=0
177
178         self.set_valid(valid)
179         return self.valid
180
181    def isoblig(self):
182       """ indique si le mot-clé est obligatoire
183       """
184       return self.definition.statut=='o'
185
186    def init_modif_up(self):
187       """
188          Propage l'état modifié au parent s'il existe et n'est l'objet
189          lui-meme
190       """
191       if self.parent and self.parent != self :
192         self.parent.state = 'modified'
193
194    def report(self):
195       """ génère le rapport de validation de self """
196       self.cr=self.CR()
197       self.cr.debut = tr("Mot-clef simple : ")+tr(self.nom)
198       self.cr.fin = tr(u"Fin Mot-clé simple : ")+tr(self.nom)
199       self.state = 'modified'
200       try:
201         self.isvalid(cr='oui')
202       except AsException,e:
203         if CONTEXT.debug : traceback.print_exc()
204         self.cr.fatal(_(tr(u"Mot-clé simple : %s %s")), self.nom, e)
205       return self.cr
206
207
208
209
210
211