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