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.
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.
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.
20 # ======================================================================
28 from tables import ERRORS
33 __version__="$Name: $"
34 __Id__="$Id: conv.py,v 1.5 2003/09/18 16:16:54 eficas Exp $"
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',
49 'SANS_NOEUD', 'SANS_GROUP_NO',
50 'INTERSEC', 'UNION','DIFFE',
51 'VECT_GRNO_ORIG','VECT_GRNO_EXTR',
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',
59 liste_concepts_produits=[]
62 def text_nom_ope(text,tags,left,right):
63 global commande_courante
65 tag,l,r,subtags=tags[0]
66 commande_courante=text[left:l]
67 return text[left:l]+'('+text[l:r]
69 commande_courante=text[left:right]
70 return text[left:right]+'('
72 def text_reuse(text,tags):
74 for tag,l,r,subtags in tags:
78 elif tag == 'nom_ope' : s=s+ '='+text_nom_ope(text,subtags,l,r)
80 s=s+ '='+text_affe(text,subtags)
82 if commande_courante in liste_macros:
83 s=s+'reuse='+sd+','+text_macro(text,subtags)+')'
85 s=s+'reuse='+sd+','+text_com(text,subtags)+')'
90 def text_noreuse(text,tags):
91 global commande_courante
93 for tag,l,r,subtags in tags:
97 elif tag == 'nom_ope' :
98 s=s+ '='+ text_nom_ope(text,subtags,l,r)
100 liste_concepts_produits.append(sd)
101 s=s+ '='+text_affe(text,subtags)
104 if sd in liste_macros:
105 s=s+'('+text_macro(text,subtags)+')'
107 s=s+'('+text_com(text,subtags)+')'
109 liste_concepts_produits.append(sd)
110 if commande_courante in liste_macros:
111 s=s+text_macro(text,subtags)+')'
113 s=s+text_com(text,subtags)+')'
119 def list_mc(lmc,mcs):
129 s=s+string.join((c,va,','),'')
134 def text_com(text,tags):
139 for tag,l,r,subtags in tags:
142 if not mcs.has_key(currid):
146 ll=text_mcf(text,subtags)
147 mcs[currid].append((ll,comment))
150 a=string.replace(text[l:r],'D','E')
151 mcs[currid].append((a,comment))
154 a=text_cplx(text,text[l:r],subtags)
155 mcs[currid].append((a,comment))
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]+"'"
167 mcs[currid].append((a,comment))
170 a=text_eval(text,subtags)
171 mcs[currid].append((a,comment))
173 elif tag == 'comment' :
174 comment=comment + '#'+text[l+1:r]
175 elif tag == 'comments' :
176 comment=comment + text[l:r]
178 if currid in Keywords:mcs[currid].append((text_larg2(text,subtags),comment))
179 else:mcs[currid].append((text_larg(text,subtags),comment))
183 if comment :s=s+comment
186 def text_macro(text,tags):
191 for tag,l,r,subtags in tags:
194 if not mcs.has_key(currid):
198 ll=text_macro_mcf(text,subtags)
199 mcs[currid].append((ll,comment))
202 a=string.replace(text[l:r],'D','E')
203 mcs[currid].append((a,comment))
206 a=text_cplx(text,text[l:r],subtags)
207 mcs[currid].append((a,comment))
212 # FR : (SGDG) il faut tester s'il n'y a pas déjà des cotes !!!
214 elif currid in Keywords :
215 a=a+"'"+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 =
221 liste_concepts_produits.append(sd)
224 mcs[currid].append((a,comment))
227 a=text_eval(text,subtags)
228 mcs[currid].append((a,comment))
230 elif tag == 'comment' :
231 comment=comment + '#'+text[l+1:r]
232 elif tag == 'comments' :
233 comment=comment + text[l:r]
235 if currid in Keywords:mcs[currid].append((text_larg2(text,subtags),comment))
236 else:mcs[currid].append((text_larg(text,subtags),comment))
240 if comment :s=s+comment
243 def comments_text(text):
244 l=string.replace(text,'%','#')
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:
251 s='EVAL("""'+text[l:r]+'""")'
255 def text_mcf(text,tags):
258 for tag,l,r,subtags in tags:
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]+"',"
271 else:s=s+text[l:r]+","
273 s=s+string.replace(text[l:r],'D','E')+','
275 s=s+text_cplx(text,text[l:r],subtags)+','
277 s=s+text_eval(text,subtags)+','
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]
287 def text_macro_mcf(text,tags):
290 for tag,l,r,subtags in tags:
297 # FR : (SGDG) il faut tester s'il n'y a pas déjà des cotes !!!
299 elif currid in Keywords :
300 s=s+"'"+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 =
306 liste_concepts_produits.append(sd)
311 s=s+string.replace(text[l:r],'D','E')+','
313 s=s+text_cplx(text,text[l:r],subtags)+','
316 s=s+text_eval(text,subtags)+','
319 if currid in Keywords:s=s+text_larg2(text,subtags)+','
320 else: s=s+text_larg(text,subtags)+','
322 elif tag == 'comments' :
323 comment=comment+text[l:r]
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:
331 s=s+string.replace(texte[l:r],'D','E')+','
335 def text_larg2(text,tags):
336 """ Pareil que text_larg mais ajoute des cotes autour des arg """
338 for tag,l,r,subtags in tags:
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]+"',")
344 ll.append(text[l:r]+",")
346 ll.append(string.replace(text[l:r],'D','E')+',')
348 ll.append(text_cplx(text,text[l:r],subtags)+',')
349 elif tag == 'comments' :
351 return '('+string.join(ll,'')+')'
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 ,)
357 for tag,l,r,subtags in tags:
359 ll.append((text,l,r))
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))
367 ll.append(text_eval(text,subtags)+',')
369 print "Argument ignore: ",text[l:r]
370 return '('+TextTools.join(ll,'')+')'
372 def comment_text(text):
373 l=string.replace(text,'\n','\n#')
374 if l[-1]=='#':return '#'+l[:-1]
377 def text_affe(text,tags):
379 for tag,l,r,subtags in tags:
383 s=s+text_eval(text,subtags)
385 s=s+text_larg(text,subtags)
387 s=s+string.replace(text[l:r],'D','E')
389 s=s+text_cplx(text,text[l:r],subtags)+','
392 def text_commande(text,tags):
394 Convertit une taglist de type commande en une chaine de caracteres
395 à la syntaxe Python représentative d'une commande
398 for tag,l,r,subtags in tags:
400 s=s+text_noreuse(text,subtags)
401 elif tag == 'reuse':s=s+text_reuse(text,subtags)
404 def text_formule(text,tags):
406 Convertit une taglist de type formule en une chaine de caracteres
407 à la syntaxe Python représentative d'une formule
412 for tag,l,r,subtags in tags:
415 s=text[l:r]+' = FORMULE('+ty+'="""('
424 s=s+ ') =\n'+text[l:r]
429 def text_comms(text,tags):
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
434 # On met la liste globale des concepts produits à zero
435 global liste_concepts_produits
436 liste_concepts_produits=[]
439 for tag,l,r,subtags in tags:
444 elif tag == 'formule':
445 s=s+ text_formule(text,subtags)
446 elif tag == 'commande' :
447 s=s+text_commande(text,subtags)
449 s=s+ comment_text(text[l:r])
452 def format_errs(text,tags):
455 for tag,l,r,subtags in tags:
457 err,warn=format_errs(text,subtags)
459 warnings=warnings+warn
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'
467 from tables import aster_script
469 # On ajoute un '\n' en fin au cas ou il serait absent
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)
478 return None,s_errors,warnings
480 ss=text_comms(text,taglist)
481 return string.replace(ss,'\r\n','\n'),s_errors,warnings