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