Salome HOME
modif pour MT
[tools/eficas.git] / Ihm / I_ENTITE.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017   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 from __future__ import absolute_import
21 import six
22 _no=0
23
24 import Accas
25 def numberEntite(entite):
26    """
27       Fonction qui attribue un numero unique a tous les objets du catalogue
28       Ce numero permet de conserver l'ordre des objets
29    """
30    global _no
31    _no=_no+1
32    entite._no=_no
33
34 class ENTITE:
35   def __init__(self):
36      numberEntite(self)
37     
38   def getDocu(self):
39     if hasattr(self,'docu') :
40       if self.docu != "" : return self.docu
41       else:
42         if hasattr(self,'pere'):
43           return self.pere.getDocu()
44         else:
45           return None
46     else:
47       return None
48
49   def getSug(self):
50     if hasattr(self,'sug') :
51       if self.sug != "" : return self.sug
52     return None
53
54   def checkDefinition(self, parent):
55       """Verifie la definition d'un objet composite (commande, fact, bloc)."""
56       args = self.entites.copy()
57       mcs = set()
58       for nom, val in args.items():
59          if val.label == 'SIMP':
60             mcs.add(nom)
61             #XXX
62             #if val.max != 1 and val.type == 'TXM':
63                 #print "#CMD", parent, nom
64          elif val.label == 'FACT':
65             val.checkDefinition(parent)
66             #PNPNPN surcharge
67             # CALC_SPEC !
68             #assert self.label != 'FACT', \
69             #   'Commande %s : Mot-clef facteur present sous un mot-clef facteur : interdit !' \
70             #   % parent
71          else:
72             continue
73          del args[nom]
74       # seuls les blocs peuvent entrer en conflit avec les mcs du plus haut niveau
75       for nom, val in args.items():
76          if val.label == 'BLOC':
77             mcbloc = val.checkDefinition(parent)
78             #XXX
79             #print "#BLOC", parent, re.sub('\s+', ' ', val.condition)
80             #assert mcs.isdisjoint(mcbloc), "Commande %s : Mot(s)-clef(s) vu(s) plusieurs fois : %s" \
81             #   % (parent, tuple(mcs.intersection(mcbloc)))
82       return mcs
83
84   def enregistreXML(self,root,catalogueXml):
85       import xml.etree.ElementTree as ET
86       import types
87       moi=ET.SubElement(root,str(self.__class__))
88       nom=ET.SubElement(moi,'nom')
89       nom.text=self.nom
90
91       if hasattr(self,'validators') and (self.validators != () and self.validators != None):
92          valid=ET.SubElement(moi,'validators')
93          valid.text= str(self.validators.__class__)
94          catalogueXml.validatorsUtilises.append(self.validators)
95
96       if hasattr(self,'regles') and (self.regles !=() and self.regles != None):
97          for regle in self.regles:
98              regle.enregistreXML(moi,catalogueXml)
99          catalogueXml.reglesUtilisees.append(self.regles)
100
101       if ((self.getDocu() !="" and self.getDocu() !=None) or  \
102           (self.fr != "" and self.fr != None) or \
103           (self.ang != "" and self.ang != None) ):
104                 dico={}
105                 if self.getDocu() !=None : dico["docu"]=self.getDocu()
106                 if self.fr != None        : dico["fr"]=six.text_type(self.fr,"iso-8859-1")
107                 if self.ang != None       : dico["ang"]=self.ang
108                 doc=ET.SubElement(moi,'doc')
109                 doc.attrib=dico
110
111       if ((self.getSug() !=None) or  \
112           (hasattr(self,'defaut') and (self.defaut != None) and (self.defaut != 'None'))) :
113                 # il faut ajouter des sug dans le catalogue
114                 # les attributs sont  toujours du texte 
115                 dico={}
116                 if (self.defaut != None) and (self.defaut != 'None') :
117                     if isinstance(self.defaut,str ) : dico["defaut"]=six.text_type(self.defaut,"iso-8859-1")
118                     else :dico["defaut"]=str(self.defaut)
119                 if self.getSug() !=None:
120                     if isinstance(self.getSug(),str ) : dico["sug"]=six.text_type(self.getSug(),"iso-8859-1")
121                     else :dico["sug"]=str(self.getSug())
122                 
123                 doc=ET.SubElement(moi,'ValeurDef')
124                 doc.attrib=dico
125
126       dico={}
127       if hasattr(self,'into') and self.into!=None: dico['into']=str(self.into)
128       if hasattr(self,'val_max') and self.val_max != "**" : dico['max']=str(self.val_max)
129       if hasattr(self,'val_min') and self.val_min != "**" : dico['min']=str(self.val_min)
130       if dico != {} :
131            PV=ET.SubElement(moi,'PlageValeur')
132            PV.attrib=dico
133
134       dico={}
135       if hasattr(self,'max')  and self.max != 1 : dico['max']=str(self.max)
136       if hasattr(self,'min')  and self.min != 1 : dico['max']=str(self.min)
137       if dico != {} :
138            Card=ET.SubElement(moi,'Cardinalite')
139            Card.attrib=dico
140
141       dico={}
142       if hasattr(self,'reentrant') and self.reentrant not in ('f','n') : dico['reentrant']=str(self.reentrant)
143       if hasattr(self,'position') and self.position != "local": dico['position']=str(self.position)
144       if hasattr(self,'homo') and self.homo != 1 : dico['homogene']=str(self.homo)
145       if hasattr(self,'statut') : dico['statut']=str(self.statut)
146       if hasattr(self,'repetable') : dico['repetable']=str(self.repetable)
147       if dico != {} :
148            pos=ET.SubElement(moi,'situation')
149            pos.attrib=dico
150
151       if hasattr(self,'type') and self.type != ():
152          typeAttendu=ET.SubElement(moi,'typeAttendu')
153          l=[]
154          for t in self.type:
155              if type(t) == type : l.append(t.__name__)
156              else : l.append(t)
157          typeAttendu.text=str(l)
158
159       if hasattr(self,'sd_prod') and self.sd_prod != () and self.sd_prod !=None:
160          typeCree=ET.SubElement(moi,'typeCree')
161          typeCree.text=str(self.sd_prod.__name__) 
162  
163       if hasattr(self,'op') and self.op !=None  : 
164          subRoutine=ET.SubElement(moi,'subRoutine')
165          subRoutine.text=str(self.op)
166
167       if hasattr(self,'proc') and self.proc != None : 
168          construction=ET.SubElement(moi,'Construction')
169          construction.text=self.proc.uri
170
171       for nomFils, fils in self.entites.items() :
172           fils.enregistreXML(moi,catalogueXml)
173       
174   def enregistreXMLStructure(self,root,catalogueXml):
175       import xml.etree.ElementTree as ET
176       import types
177       moi=ET.SubElement(root,str(self.__class__))
178
179       if hasattr(self,'into') and self.into!=None: 
180           INTO=ET.SubElement(moi,'into')
181           INTO.text='into'
182
183       dico={}
184       if hasattr(self,'val_max') and self.val_max != "**" : dico['max']=str(self.val_max)
185       if hasattr(self,'val_min') and self.val_min != "**" : dico['min']=str(self.val_min)
186       if dico != {} :
187            PV=ET.SubElement(moi,'maxOrMin')
188            PV.text='maxOrMin'
189
190       dico={}
191       if hasattr(self,'max')  and self.max != 1 : dico['max']=str(self.max)
192       if hasattr(self,'min')  and self.min != 1 : dico['max']=str(self.min)
193       if dico != {} :
194            Card=ET.SubElement(moi,'liste')
195            Card.text="liste"
196
197       dico={}
198       if hasattr(self,'statut') and self.statut=="f" :
199          statut=ET.SubElement(moi,'facultatif')
200          statut.text='facultatif'
201       if hasattr(self,'statut') and self.statut !="f" :
202          statut=ET.SubElement(moi,'obligatoire')
203          statut.text='obligatoire'
204
205       if hasattr(self,'type') and self.type != ():
206         try :
207            if 'Fichier' in self.type : ty=ET.SubElement(moi,'Fichier')
208            ty.text='type'
209         except :
210            try :
211              if 'Repertoire' in self.type : ty=ET.SubElement(moi,'Repertoire')
212              ty.text='type'
213            except :
214               for t in self.type:
215                 if t == "I" : ty=ET.SubElement(moi,'typeEntier')
216                 elif t == "R" : ty=ET.SubElement(moi,'typeReel')
217                 elif t == "TXM" : ty=ET.SubElement(moi,'typeTXM')
218                 else :
219                   try :
220                     ty=ET.SubElement(moi,t.__name__) 
221                   except :
222                     ty=ET.SubElement(moi,'autre') 
223                 ty.text='type'
224
225       if hasattr(self,'sd_prod') and self.sd_prod != () and self.sd_prod !=None:
226          typeCree=ET.SubElement(moi,'typeCree')
227          typeCree.text='sd_prod'
228  
229       if hasattr(self,'op') and self.op !=None  : 
230          subRoutine=ET.SubElement(moi,'subRoutine')
231          subRoutine.text='op'
232
233       if hasattr(self,'proc') and self.proc != None : 
234          construction=ET.SubElement(moi,'Construction')
235          construction.text='proc'
236
237       for nomFils, fils in self.entites.items() :
238           fils.enregistreXMLStructure(moi,catalogueXml)
239