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