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