]> SALOME platform Git repositories - tools/eficas.git/blob - convert/Parserv5/conv.py
Salome HOME
Version initiale de EFICAS 1.2
[tools/eficas.git] / convert / Parserv5 / conv.py
1 #@ MODIF conv Parsers  DATE 17/10/2001   AUTEUR DURAND C.DURAND 
2 #            CONFIGURATION MANAGEMENT OF EDF VERSION
3 # ======================================================================
4 # COPYRIGHT (C) 1991 - 2001  EDF R&D                  WWW.CODE-ASTER.ORG
5 #              SEE THE FILE "LICENSE.TERMS" FOR INFORMATION ON USAGE AND
6 #              REDISTRIBUTION OF THIS FILE.
7 # ======================================================================
8 import sys,string
9 import TextTools
10 from tables import ERRORS
11
12 #
13 __version__="$Name: V1_1p1 $"
14 __Id__="$Id: conv.py,v 1.1.1.1 2001/12/04 15:38:23 eficas Exp $"
15 #
16
17 Keywords=('MAILLE_1','MAILLE_2','MAILLE_ESCL','MAILLE_FOND','MAILLE_MAIT','MAILLE_ORIG','MAILLE',
18           'NOEUD', 'NOEUD_1','NOEUD_2','NOEUD_INIT','NOEUD_FIN', 'NOEUD_ORIG','NOEUD_REFE','NOEUD_EXTR',
19           'NOEUD_I', 'NOEUD_J','NOEUD_CHOC','NOEUD_ANCRAGE','NOEUD_CENTRE','NOEUD_CMP','NOEUD_DOUBLE',
20           'NOEUD_ESCL','NOEUD_FOND','NOEUD_PARA','NOEUD_POIN_TANG',
21           'GROUP_MA', 'GROUP_MA_1','GROUP_MA_2','GROUP_MA_INT','GROUP_MA_EXT', 'GROUP_MA_ORIG',
22           'GROUP_MA_BORD','GROUP_MA_INTE','GROUP_MA_FLUIDE', 'GROUP_MA_INTERF','GROUP_MA_BETON',
23           'GROUP_MA_ESCL','GROUP_MA_FINAL','GROUP_MA_FLU_SOL','GROUP_MA_FLU_STR','GROUP_MA_FOND',
24           'GROUP_MA_MAIT','GROUP_MA_RADIER','GROUP_MA_SOL_SOL','GROUP_MA_INIT',
25           'GROUP_NO', 'GROUP_NO_1','GROUP_NO_2','GROUP_NO_EXT', 'GROUP_NO_ORIG','GROUP_NO_CHOC',
26           'GROUP_NO_ANCRAGE','GROUP_NO_CENTRE','GROUP_NO_ESCL','GROUP_NO_EXTR','GROUP_NO_FIN',
27           'GROUP_NO_FOND','GROUP_NO_INIT','GROUP_NO_POIN_TG','GROUP_NO_RADIER',
28           'NOM','NOM_GROUP_MA',
29           'SANS_NOEUD', 'SANS_GROUP_NO',
30           'INTERSEC', 'UNION','DIFFE',
31           'VECT_GRNO_ORIG','VECT_GRNO_EXTR',
32           'VALE_CO'
33          )
34
35 liste_macros=('MACRO_MATR_ASSE','MACRO_ELAS_MULT','MACR_ASCOUF_MAIL','MACR_ASCOUF_CALC','MACR_ASPIC_MAIL',
36               'MACR_ASPIC_CALC','MACRO_MATR_AJOU','MACRO_ELAS_MULT','MACRO_MODE_MECA','MACRO_PROJ_BASE',
37               'MACR_ADAP_MAIL',
38               )
39 liste_concepts_produits=[]
40 commande_courante=''
41
42 def text_nom_ope(text,tags,left,right):
43   global commande_courante
44   if len(tags) :
45     tag,l,r,subtags=tags[0]
46     commande_courante=text[left:l]
47     return text[left:l]+'('+text[l:r]
48   else :
49     commande_courante=text[left:right]
50     return text[left:right]+'('
51     
52 def text_reuse(text,tags):
53   s=''
54   for tag,l,r,subtags in tags:
55     if tag == 'ident' :
56       sd=text[l:r]
57       s=s+ sd
58     elif tag == 'nom_ope' : s=s+ '='+text_nom_ope(text,subtags,l,r)
59     elif tag == 'affe' : 
60       s=s+ '='+text_affe(text,subtags)
61     elif tag == 'comm' :
62       if commande_courante in liste_macros:
63         s=s+'reuse='+sd+','+text_macro(text,subtags)+')'
64       else:
65         s=s+'reuse='+sd+','+text_com(text,subtags)+')'
66     else:pass
67   s=s+'\n'
68   return s
69
70 def text_noreuse(text,tags):
71   global commande_courante
72   s=''
73   for tag,l,r,subtags in tags:
74     if tag == 'ident' :
75       sd=text[l:r]
76       s=s+ text[l:r]
77     elif tag == 'nom_ope' :
78       s=s+ '='+ text_nom_ope(text,subtags,l,r)
79     elif tag == 'affe' :
80       liste_concepts_produits.append(sd)
81       s=s+ '='+text_affe(text,subtags)
82     elif tag == 'comm' :
83       if oldtag=='ident':
84         if sd in liste_macros:
85           s=s+'('+text_macro(text,subtags)+')'
86         else:
87           s=s+'('+text_com(text,subtags)+')'
88       else:
89         liste_concepts_produits.append(sd)
90         if commande_courante in liste_macros:
91           s=s+text_macro(text,subtags)+')'
92         else:
93           s=s+text_com(text,subtags)+')'
94     else:pass
95     oldtag=tag
96   s=s+'\n'
97   return s
98
99 def list_mc(lmc,mcs):
100   s=''
101   for k in lmc:
102     v=mcs[k]
103     if len(v) ==1:
104       va,c=v[0]
105       s=s+c+k+'='+va+','
106     elif len(v) > 1:
107       s=s+k+'=('
108       for va,c in v:
109         s=s+string.join((c,va,','),'')
110       s=s[:-1]+'),'
111   s=s[:-1]
112   return s
113
114 def text_com(text,tags):
115   mcs={}
116   lmc=[]
117   currid=None
118   comment=''
119   for tag,l,r,subtags in tags:
120     if tag == 'ident' :
121       currid=text[l:r]
122       if not mcs.has_key(currid):
123         mcs[currid]=[]
124         lmc.append(currid)
125     elif tag == 'mcf':
126       ll=text_mcf(text,subtags)
127       mcs[currid].append((ll,comment))
128       comment=''
129     elif tag == 'num' :
130       a=string.replace(text[l:r],'D','E')
131       mcs[currid].append((a,comment))
132       comment=''
133     elif tag == 'CPLX' :
134       a=text_cplx(text,text[l:r],subtags)
135       mcs[currid].append((a,comment))
136       comment=''
137     elif tag == 'arg' :
138       a=''
139       if currid in Keywords :
140         # FR : (SGDG) il faut tester s'il n'y a pas déjà des cotes !!!
141         if text[l]!="'" and text[r-1]!="'":
142           a=a+"'"+text[l:r]+"'"
143         else:
144           a=a+text[l:r]
145       else:
146         a=a+text[l:r]
147       mcs[currid].append((a,comment))
148       comment=''
149     elif tag == 'EVAL' :
150       a=text_eval(text,subtags)
151       mcs[currid].append((a,comment))
152       comment=''
153     elif tag == 'comment' :
154       comment=comment + '#'+text[l+1:r]
155     elif tag == 'comments' :
156       comment=comment + text[l:r]
157     elif tag == 'larg' :
158       if currid in Keywords:mcs[currid].append((text_larg2(text,subtags),comment))
159       else:mcs[currid].append((text_larg(text,subtags),comment))
160       comment=''
161     else :pass
162   s=list_mc(lmc,mcs)
163   if comment :s=s+comment
164   return s
165
166 def text_macro(text,tags):
167   mcs={}
168   lmc=[]
169   currid=None
170   comment=''
171   for tag,l,r,subtags in tags:
172     if tag == 'ident' :
173       currid=text[l:r]
174       if not mcs.has_key(currid):
175         mcs[currid]=[]
176         lmc.append(currid)
177     elif tag == 'mcf':
178       ll=text_macro_mcf(text,subtags)
179       mcs[currid].append((ll,comment))
180       comment=''
181     elif tag == 'num' :
182       a=string.replace(text[l:r],'D','E')
183       mcs[currid].append((a,comment))
184       comment=''
185     elif tag == 'CPLX' :
186       a=text_cplx(text,text[l:r],subtags)
187       mcs[currid].append((a,comment))
188       comment=''
189     elif tag == 'arg' :
190       a=''
191       if text[l] == "'":
192         # FR : (SGDG) il faut tester s'il n'y a pas déjà des cotes !!!
193         a=a+text[l:r]
194       elif currid in Keywords :
195         a=a+"'"+text[l:r]+"'"
196       else:
197         sd=text[l:r]
198         if sd not in liste_concepts_produits:
199           # Il s agit d un concept produit par la macro mais situe à droite de =
200           a=a+'CO("'+sd+'")'
201           liste_concepts_produits.append(sd)
202         else:
203           a=a+sd
204       mcs[currid].append((a,comment))
205       comment=''
206     elif tag == 'EVAL' :
207       a=text_eval(text,subtags)
208       mcs[currid].append((a,comment))
209       comment=''
210     elif tag == 'comment' :
211       comment=comment + '#'+text[l+1:r]
212     elif tag == 'comments' :
213       comment=comment + text[l:r]
214     elif tag == 'larg' :
215       if currid in Keywords:mcs[currid].append((text_larg2(text,subtags),comment))
216       else:mcs[currid].append((text_larg(text,subtags),comment))
217       comment=''
218     else :pass
219   s=list_mc(lmc,mcs)
220   if comment :s=s+comment
221   return s
222
223 def comments_text(text):
224   l=string.replace(text,'%','#')
225   return l
226
227 def text_eval(text,tags):
228   # on retourne l expression sans conversion dans un objet EVAL et entre quotes
229   for tag,l,r,subtags in tags:
230     if tag == 'vexpr':
231       s='EVAL("""'+text[l:r]+'""")'
232       return s
233   return ''
234
235 def text_mcf(text,tags):
236   s='_F( '
237   comment=''
238   for tag,l,r,subtags in tags:
239     if tag == 'ident' :
240       s=s+comment
241       comment=''
242       currid=text[l:r]
243       s=s+ currid +' = '
244     elif tag == 'arg' :
245       if currid in Keywords :
246         # FR : (SGDG) il faut tester s'il n'y a pas déjà des cotes !!!
247         if text[l]!="'" and text[r-1]!="'":
248           s=s+"'"+text[l:r]+"',"
249         else:
250           s=s+text[l:r]+","
251       else:s=s+text[l:r]+","
252     elif tag == 'num' :
253       s=s+string.replace(text[l:r],'D','E')+','
254     elif tag == 'CPLX' :
255       s=s+text_cplx(text,text[l:r],subtags)+','
256     elif tag == 'EVAL' :
257       s=s+text_eval(text,subtags)+','
258     elif tag == 'larg' :
259       if currid in Keywords:s=s+text_larg2(text,subtags)+','
260       else: s=s+text_larg(text,subtags)+','
261     elif tag == 'comments' :
262       comment=comment+text[l:r]
263   if comment != '':
264     s=s+comment
265   return s+')'
266
267 def text_macro_mcf(text,tags):
268   s='_F( '
269   comment=''
270   for tag,l,r,subtags in tags:
271     if tag == 'ident' :
272       s=s+comment
273       currid=text[l:r]
274       s=s+ currid +' = '
275     elif tag == 'arg' :
276       if text[l] == "'":
277         # FR : (SGDG) il faut tester s'il n'y a pas déjà des cotes !!!
278         s=s+text[l:r]+","
279       elif currid in Keywords :
280         s=s+"'"+text[l:r]+"',"
281       else:
282         sd=text[l:r]
283         if sd not in liste_concepts_produits:
284           # Il s agit d un concept produit par la macro mais situe à droite de =
285           s=s+'CO("'+sd+'"),'
286           liste_concepts_produits.append(sd)
287         else:
288           s=s+sd+','
289       comment=''
290     elif tag == 'num' :
291       s=s+string.replace(text[l:r],'D','E')+','
292     elif tag == 'CPLX' :
293       s=s+text_cplx(text,text[l:r],subtags)+','
294       comment=''
295     elif tag == 'EVAL' :
296       s=s+text_eval(text,subtags)+','
297       comment=''
298     elif tag == 'larg' :
299       if currid in Keywords:s=s+text_larg2(text,subtags)+','
300       else: s=s+text_larg(text,subtags)+','
301       comment=''
302     elif tag == 'comments' :
303       comment=comment+text[l:r]
304   return s[:-1]+')'
305
306 def text_cplx(texte,text,tags):
307   """ Retourne une chaîne de caractères représentant un complexe """
308   s="('"+text[0:2]+"'," #text[0:2] = RI ou MP
309   for tag,l,r,subtags in tags:
310     if tag == 'num' :
311       s=s+string.replace(texte[l:r],'D','E')+','
312   s=s+')'
313   return s
314   
315 def text_larg2(text,tags):
316   """ Pareil que text_larg mais ajoute des cotes autour des arg """
317   ll=[]
318   for tag,l,r,subtags in tags:
319     if tag == 'arg' :
320       # FR : (SGDG) il faut tester le cas où les cotes sont déjà là !!!!
321       if text[l] != "'" and text[r-1] != "'":
322         ll.append( "'"+text[l:r]+"',")
323       else:
324         ll.append(text[l:r]+",")
325     elif tag == 'num' :
326       ll.append(string.replace(text[l:r],'D','E')+',')
327     elif tag == 'CPLX' :
328       ll.append(text_cplx(text,text[l:r],subtags)+',')
329     elif tag == 'comments' :
330       ll.append(text[l:r])
331   return '('+string.join(ll,'')+')'
332
333 def text_larg(text,tags):
334   # Pour les listes d arguments il semble plus rapide de construire 
335   # une liste puis de faire join (ne pas exagerer : voir ajout ,)
336   ll=[]
337   for tag,l,r,subtags in tags:
338     if tag == 'arg' :
339       ll.append((text,l,r))
340       ll.append(',')
341     elif tag == 'num' :
342  # cette facon de faire est un peu plus rapide que la suivante
343       ll.append(string.replace(text[l:r],'D','E')+',')
344     elif tag == 'comments' :
345       ll.append((text,l,r))
346     elif tag == 'EVAL' :
347       ll.append(text_eval(text,subtags)+',')
348     else:
349       print "Argument ignore: ",text[l:r]
350   return '('+TextTools.join(ll,'')+')'
351
352 def comment_text(text):
353   l=string.replace(text,'\n','\n#')
354   if l[-1]=='#':return '#'+l[:-1]
355   else:return '#'+l
356
357 def text_affe(text,tags):
358   s=''
359   for tag,l,r,subtags in tags:
360     if tag == 'arg' :
361       s=s+text[l:r]
362     elif tag == 'EVAL' :
363       s=s+text_eval(text,subtags)
364     elif tag == 'larg' :
365       s=s+text_larg(text,subtags)
366     elif tag == 'num' :
367       s=s+string.replace(text[l:r],'D','E')
368     elif tag == 'CPLX' :
369       s=s+text_cplx(text,text[l:r],subtags)+','
370   return s
371
372 def text_commande(text,tags):
373   """
374      Convertit une taglist de type commande en une chaine de caracteres
375      à la syntaxe Python représentative d'une commande
376   """
377   s=''
378   for tag,l,r,subtags in tags:
379     if tag == 'noreuse':
380       s=s+text_noreuse(text,subtags)
381     elif tag == 'reuse':s=s+text_reuse(text,subtags)
382   return s
383
384 def text_formule(text,tags):
385   """
386      Convertit une taglist de type formule en une chaine de caracteres
387      à la syntaxe Python représentative d'une formule
388   """
389   s=''
390   count=0
391   typ=''
392   for tag,l,r,subtags in tags:
393     if tag == 'id':
394       if count == 0:
395         s=text[l:r]+' = FORMULE('+ty+'="""('
396       else:
397         if count > 1:s=s+','
398         s=s+typ+text[l:r]
399         typ=''
400       count = count +1
401     elif tag == 'typ':
402       typ=text[l:r]
403     elif tag == 'vexpr':
404       s=s+ ') =\n'+text[l:r]
405     elif tag == 'type':
406       ty=text[l:r]
407   return s +'""")\n'
408
409 def text_comms(text,tags):
410   """
411      Convertit une taglist resultat d'un appel à TextTools.tag avec une table de type Aster
412      en une chaine de caracteres à la syntaxe Python
413   """
414   # On met la liste globale des concepts produits à zero
415   global liste_concepts_produits
416   liste_concepts_produits=[]
417
418   s=''
419   for tag,l,r,subtags in tags:
420     if tag == 'comment':
421       s=s+ '#'+text[l+1:r]
422     elif tag == 'Null':
423       s=s+ '\n'
424     elif tag == 'formule':
425       s=s+ text_formule(text,subtags)
426     elif tag == 'commande' :
427       s=s+text_commande(text,subtags)
428     else:
429       s=s+ comment_text(text[l:r])
430   return s
431
432 def format_errs(text,tags):
433   s=''
434   warnings=''
435   for tag,l,r,subtags in tags:
436     if subtags:
437        err,warn=format_errs(text,subtags)
438        s=s+err
439        warnings=warnings+warn
440     if tag in ERRORS:
441        s=s+ tag+" ligne : "+`TextTools.countlines(text[:l])`+" texte erroné : "+text[l-10:l]+'?'+text[l:r]+'\n'
442     if tag == 'passline':
443        warnings=warnings+ " ligne "+`TextTools.countlines(text[:l])`+" ignorée : " +text[l:r]+'\n'
444   return s,warnings
445
446 def conver(text):
447    from tables import aster_script
448    import re
449    text=string.upper(text)
450    result, taglist, next = TextTools.tag(text,aster_script)
451    # Pour vérifier les résultats intermédiaires décommenter la ligne suivante
452    #TextTools.print_tags(text,taglist)
453    text=string.replace(text,'%','#')
454    s_errors,warnings = format_errs(text,taglist)
455    if s_errors:
456       return None,s_errors,warnings
457    else:
458       ss=text_comms(text,taglist)
459       return string.replace(ss,'\r\n','\n'),s_errors,warnings
460
461
462
463