# -*- coding: utf-8 -*-
-# CONFIGURATION MANAGEMENT OF EDF VERSION
-# ======================================================================
-# COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG
-# THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
-# IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
-# THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
-# (AT YOUR OPTION) ANY LATER VERSION.
+# Copyright (C) 2007-2013 EDF R&D
#
-# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
-# WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
-# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
-# GENERAL PUBLIC LICENSE FOR MORE DETAILS.
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
#
-# YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
-# ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
-# 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#
-# ======================================================================
import sys,string,re
import traceback
+from Extensions.i18n import tr
escapedQuotesRE = re.compile(r"(\\\\|\\\"|\\\')")
stringsAndCommentsRE = \
- re.compile("(\"\"\".*?\"\"\"|'''.*?'''|\"[^\"]*\"|\'[^\']*\'|#.*?\n)", re.DOTALL)
-allchars = string.maketrans("", "")
+ re.compile(u"(\"\"\".*?\"\"\"|'''.*?'''|\"[^\"]*\"|\'[^\']*\'|#.*?\n)", re.DOTALL)
+#stringsAndCommentsRE = \
+# re.compile(u"(\"\"\".*\"\"\"|'''.*'''|\"[^\"]*\"|\'[^\']*\'|#.*\n)", re.DOTALL)
+allchars = string.maketrans(u"", "")
allcharsExceptNewline = allchars[: allchars.index('\n')]+allchars[allchars.index('\n')+1:]
allcharsExceptNewlineTranstable = string.maketrans(allcharsExceptNewline, '*'*len(allcharsExceptNewline))
+#if sys.platform[0:5]=="linux" :
+# allcharsExceptNewlineTranstable = string.maketrans(allcharsExceptNewline, '*'*len(allcharsExceptNewline))
+#elif sys.platform[0:3]=="win" :
+# allcharsExceptNewlineTranstable = dict((ord(char), u'*') for char in allcharsExceptNewline)#
+#else :
+# allcharsExceptNewlineTranstable = string.maketrans(allcharsExceptNewline, '*'*len(allcharsExceptNewline))
+
def maskStringsAndComments(src):
"""Masque tous les caracteres de src contenus dans des commentaires ou des strings multilignes (triples
quotes et guillemets.
Le masquage est realise en remplacant les caracteres par des *
Attention : cette fonction doit etre utilisee sur un texte complet et pas ligne par ligne
"""
+# remplace les \\, les \" les \' par **
+# supprime toutes les chaines ou commentaires ,y compris multiligne i
+# entre 3 ou 1 simples ou doubles quotes (ouvrantes fermantes) ou #
+# laisse les non fermantes ou non ouvrantes
+ #src = escapedQuotesRE.sub(u"**", src)
+ # le u met le bazar dans le translate
src = escapedQuotesRE.sub("**", src)
allstrings = stringsAndCommentsRE.split(src)
# every odd element is a string or comment
for i in xrange(1, len(allstrings), 2):
- if allstrings[i].startswith("'''")or allstrings[i].startswith('"""'):
+ if allstrings[i].startswith(u"'''")or allstrings[i].startswith('"""'):
allstrings[i] = allstrings[i][:3]+ \
- allstrings[i][3:-3].translate(allcharsExceptNewlineTranstable)+ \
- allstrings[i][-3:]
+ allstrings[i][3:-3].translate(allcharsExceptNewlineTranstable)+ \
+ allstrings[i][-3:]
else:
allstrings[i] = allstrings[i][0]+ \
- allstrings[i][1:-1].translate(allcharsExceptNewlineTranstable)+ \
- allstrings[i][-1]
+ allstrings[i][1:-1].translate(allcharsExceptNewlineTranstable)+ \
+ allstrings[i][-1]
return "".join(allstrings)
#commentaire double precede d'un nombre quelconque de blancs (pas multiligne)
pattern_2comments = re.compile(r"^\s*##.*")
+pattern_finComments = re.compile("^\s*##Fin Commentaire")
#commentaire standard precede d'un nombre quelconque de blancs (pas multiligne)
pattern_comment = re.compile(r"^\s*#.*")
#fin de ligne ; suivi d'un nombre quelconque de blancs (pas multiligne)
class ENTITE_JDC :
- """Classe de base pour tous les objets créés lors de la conversion
- Tout objet dérivé est enregistré auprès de son père à sa création
+ """Classe de base pour tous les objets créés lors de la conversion
+ Tout objet dérivé est enregistré aupres de son pere a sa création
"""
def __init__(self,pere):
self.texte = ''
def append_text(self,texte):
"""
- Ajoute texte à self.texte en mettant un retour chariot à la fin de texte
+ Ajoute texte a self.texte en mettant un retour chariot a la fin de texte
"""
texte = texte+'\n'
self.texte = self.texte +texte
def __str__(self):
"""
- Retourne une chaîne de caractères représentants self
- sous une forme interprétable par EFICAS
+ Retourne une chaine de caracteres représentants self
+ sous une forme interprétable par EFICAS
"""
t=repr(self.texte)
- return "COMMENTAIRE("+t+")\n"
+ return "COMMENTAIRE(u"+t+")\n"
- #s='COMMENTAIRE("""'+self.texte+'""")\n\n'
+ #s='COMMENTAIRE(u"""'+self.texte+'""")\n\n'
#return s
def append_text(self,texte):
"""
- Ajoute texte à self.texte en enlevant le # initial
+ Ajoute texte a self.texte en enlevant le # initial
"""
texte = texte+'\n'
if texte[0] == '#':
self.texte = self.texte+texte[1:]
else:
- # le dièse n'est pas sur le premier caractère
- amont,aval = string.split(texte,'#',1) # on découpe suivant la première occurrence de #
+ # le diese n'est pas sur le premier caractere
+ amont,aval = string.split(texte,'#',1) # on découpe suivant la premiere occurrence de #
self.texte = self.texte +amont + aval
class COMMANDE(ENTITE_JDC):
def get_nb_par(self):
"""
- Retourne la différence entre le nombre de parenthèses ouvrantes
- et le nombre de parenthèses fermantes présentes dans self.texte
- Peut donc retourner un entier négatif
+ Retourne la différence entre le nombre de parentheses ouvrantes
+ et le nombre de parentheses fermantes présentes dans self.texte
+ Peut donc retourner un entier négatif
"""
# faire attention aux commentaires contenus dans self.texte
- # qui peuvent eux-mêmes contenir des parenthèses !!!!
+ # qui peuvent eux-memes contenir des parentheses !!!!
l_lignes = string.split(self.texte,'\n')
nb = 0
for ligne in l_lignes:
def append_text(self,texte):
"""
- Ajoute texte à self.texte en enlevant tout retour chariot et tout point virgule
+ Ajoute texte a self.texte en enlevant tout retour chariot et tout point virgule
PN et tout commentaire
"""
if texte[-1] == '\n' : texte = string.rstrip(texte[0:-1])
def __str__(self):
"""
- Retourne une expression de l'affectation compréhensible par ACCAS
+ Retourne une expression de l'affectation compréhensible par ACCAS
et exploitable par EFICAS
"""
nom,valeur = string.split(self.texte,'=',1)
def append_text(self,texte):
"""
- Ajoute texte à self.texte en enlevant les doubles commentaires
+ Ajoute texte a self.texte en enlevant les doubles commentaires
"""
texte = string.strip(texte)
texte = string.strip(texte[2:])
def __str__(self):
"""
- Retourne une expression de la commande commentarisée compréhensible par ACCAS
+ Retourne une expression de la commande commentarisée compréhensible par ACCAS
et exploitable par EFICAS
"""
return "COMMANDE_COMM(texte="+repr(self.texte)+")\n"
def append_text(self,texte):
"""
- Ajoute texte à self.texte en enlevant tout retour chariot
+ Ajoute texte a self.texte en enlevant tout retour chariot
"""
if texte[-1] == '\n' : texte = texte[1:-1]
self.texte = self.texte+texte
def __str__(self):
"""
- Retourne une expression du paramètre EVAL compréhensible par ACCAS
+ Retourne une expression du parametre EVAL compréhensible par ACCAS
et exploitable par EFICAS
"""
nom,valeur = string.split(self.texte,'=',1)
class PARSEUR_PYTHON:
"""
- Cette classe sert à générer un objet PARSEUR_PYTHON qui réalise l'analyse d'un texte
- représentant un JDC Python en distinguant :
+ Cette classe sert a générer un objet PARSEUR_PYTHON qui réalise l'analyse d'un texte
+ représentant un JDC Python en distinguant :
- les commentaires inter commandes
- les affectations
- les commandes
def is_affectation(self,texte):
"""
- Méthode booléenne qui retourne 1 si le texte est celui d'une affectation dans un jeu de commandes
+ Méthode booléenne qui retourne 1 si le texte est celui d'une affectation dans un jeu de commandes
Aster, 0 sinon
"""
if '=' not in texte : return 0
if self.pattern_commande.match(texte):
- # cas d'une procédure ...
+ # cas d'une procédure ...
return 0
amont,aval = string.split(texte,'=',1)
aval = string.strip(aval)
if m is None : return 0
if m.start() != 0 :return 0
if m.end() != len(s):return 0
- #print texte,amont,aval
return 1
def is_eval(self,texte):
"""
- Méthode booléenne qui retourne 1 si le texte est celui d'une affectation de type EVAL
+ Méthode booléenne qui retourne 1 si le texte est celui d'une affectation de type EVAL
dans un jeu de commandes Aster, 0 sinon
"""
if '=' not in texte : return 0
if self.pattern_commande.match(texte):
- # cas d'une procédure ...
+ # cas d'une procédure ...
return 0
amont,aval = string.split(texte,'=',1)
aval = string.strip(aval)
def is_commande(self,texte):
"""
- Méthode booléenne qui retourne 1 si le texte est celui d'une commande dans un jeu de commandes
+ Méthode booléenne qui retourne 1 si le texte est celui d'une commande dans un jeu de commandes
Aster, 0 sinon
"""
if self.pattern_commande.match(texte):
- # cas d'une procédure ...
+ # cas d'une procédure ...
return 1
# A ce stade il faut avoir un OPER ou une MACRO, bref un '=' !
if '=' not in texte : return 0
# on a un texte de la forme xxxx = yyyyy
- # --> reste à analyser yyyy
+ # --> reste a analyser yyyy
amont,aval = string.split(texte,'=',1)
aval = string.strip(aval)
if self.pattern_commande.match(aval):
else:
return 0
+ def is_modification_catalogue(self,texte) :
+ if self.pattern_commande.match(texte):
+ return 1
+
def analyse(self):
"""
Eclate la chaine self.texte en self.l_objets une liste lignes d'instructions
- et de commentaires (parmi lesquels des instructions "commentarisées").
+ et de commentaires (parmi lesquels des instructions "commentarisées").
"""
l_lignes = string.split(self.texte,'\n')
commentaire_courant = None
#Masquage des commentaires et strings multilignes
srcMasked=maskStringsAndComments('\n'.join(l_lignes))
- #print srcMasked
masked_lines=srcMasked.split('\n')
lineno=0
for ligne in l_lignes :
line=masked_lines[lineno]
lineno=lineno+1
- #print "ligne:",line
+ #print ("ligne:",line)
# mise a jour du nombre total de parentheses ouvertes (non fermees)
# et du nombre de commentaires non termines
for i in range(len(implicitContinuationChars)):
hangingBraces[i] = numHanging+line.count(contchar[0]) - line.count(contchar[1])
hangingComments ^= line.count('"""') % 2
- hangingComments ^= line.count("'''") % 2
- #print hangingComments,hangingBraces
+ hangingComments ^= line.count(u"'''") % 2
+ #print (hangingComments,hangingBraces)
if hangingBraces[0] < 0 or hangingBraces[1] < 0 or hangingBraces[2] < 0:
raise ParserException()
continue
if pattern_2comments.match(ligne):
- #on a trouvé une commande commentarisée : double commentaire sans rien devant à part des blancs
+ #on a trouvé une commande commentarisée : double commentaire sans rien devant a part des blancs
if commentaire_courant:
#Si un commentaire ordinaire est en cours on le termine
commentaire_courant = None
if commande_courante :
- # on a un objet commentarisé à l'intérieur d'une commande
- # --> non traité pour l'instant : on l'ajoute simplement a la commande courante comme
+ # on a un objet commentarisé a l'intérieur d'une commande
+ # --> non traité pour l'instant : on l'ajoute simplement a la commande courante comme
# un commentaire ordinaire
commande_courante.append_text(ligne)
elif commande_commentarisee_courante :
# commande_commentarisee en cours : on ajoute la ligne
commande_commentarisee_courante.append_text(ligne)
+ # on a 2 commandes commentarisées de suite
+ if pattern_finComments.match(ligne) :
+ commande_commentarisee_courante = None
else:
- # debut de commande commentarisée : on crée un objet commande_commentarisee_courante
+ # debut de commande commentarisée : on crée un objet commande_commentarisee_courante
commande_commentarisee_courante = COMMANDE_COMMENTARISEE(self)
commande_commentarisee_courante.append_text(ligne)
- #on passe à la ligne suivante
+ #on passe a la ligne suivante
continue
if pattern_comment.match(ligne):
commande_commentarisee_courante = None
if commande_courante :
- # il s'agit d'un commentaire à l'intérieur d'une commande --> on ne fait rien de special
+ # il s'agit d'un commentaire a l'intérieur d'une commande --> on ne fait rien de special
#on l'ajoute au texte de la commande
commande_courante.append_text(ligne)
elif commentaire_courant :
- # il s'agit de la nième ligne d'un commentaire entre deux commandes
+ # il s'agit de la nieme ligne d'un commentaire entre deux commandes
# --> on ajoute cette ligne au commentaire courant
commentaire_courant.append_text(ligne)
else :
# il s'agit d'un nouveau commentaire entre deux commandes
- # --> on le crée et il devient le commentaire courant
+ # --> on le crée et il devient le commentaire courant
commentaire_courant = COMMENTAIRE(self)
commentaire_courant.append_text(ligne)
- #on passe à la ligne suivante
+ #on passe a la ligne suivante
continue
- # la ligne contient des données autre qu'un éventuel commentaire
+ # la ligne contient des données autre qu'un éventuel commentaire
if commentaire_courant :
- # on clôt un éventuel commentaire courant
+ # on clot un éventuel commentaire courant
commentaire_courant = None
if commande_commentarisee_courante :
- # on clôt une éventuelle commande commentarisee courante
+ # on clot une éventuelle commande commentarisee courante
commande_commentarisee_courante = None
if commande_courante :
if not linecontinueRE.search(line) \
and (hangingBraces == emptyHangingBraces) \
and not hangingComments:
- #la commande est terminée
- #print "fin de commande"
+ #la commande est terminée
self.analyse_reel(commande_courante.texte)
commande_courante = None
- #on passe à la ligne suivante
+ #on passe a la ligne suivante
continue
if affectation_courante != None :
if not linecontinueRE.search(line) \
and (hangingBraces == emptyHangingBraces) \
and not hangingComments:
- #L'affectation est terminée
+ #L'affectation est terminée
affectation_courante=None
- #on passe à la ligne suivante
+ #on passe a la ligne suivante
continue
# il peut s'agir d'une commande ou d'une affectation ...
if affectation_courante : affectation_courante = None
affectation = AFFECTATION_EVAL(self)
affectation.append_text(ligne)
- #on passe à la ligne suivante
+ #on passe a la ligne suivante
continue
if self.is_affectation(ligne):
# --> affectation
text=ligne
#traitement des commentaires en fin de ligne
- compos=line.find("#")
+ compos=line.find(u"#")
if compos > 2:
#commentaire en fin de ligne
#on cree un nouveau commentaire avant le parametre
COMMENTAIRE(self).append_text(ligne[compos:])
text=ligne[:compos]
#si plusieurs instructions separees par des ; sur la meme ligne
- inspos=line.find(";")
+ inspos=line.find(u";")
if inspos > 2:
#on garde seulement la premiere partie de la ligne
#si on a que des blancs apres le point virgule
if string.strip(text[inspos:]) == ";":
text=text[:inspos]
else:
- raise FatalError("Eficas ne peut pas traiter plusieurs instructions sur la meme ligne : %s" % ligne)
+ raise FatalError(tr("Eficas ne peut pas traiter plusieurs instructions \
+ sur la meme ligne : %s", ligne))
affectation_courante = AFFECTATION(self)
affectation_courante.append_text(text)
if not linecontinueRE.search(line) \
and (hangingBraces == emptyHangingBraces) \
and not hangingComments:
- #L'affectation est terminée
+ #L'affectation est terminée
affectation_courante=None
- #on passe à la ligne suivante
+ #on passe a la ligne suivante
continue
if self.is_commande(ligne):
affectation_courante = None
commande_courante = COMMANDE(self)
commande_courante.append_text(ligne)
- #si la commande est complète, on la termine
+ #si la commande est complete, on la termine
if not linecontinueRE.search(line) \
and (hangingBraces == emptyHangingBraces) \
and not hangingComments:
- #la commande est terminée
- #print "fin de commande"
+ #la commande est terminee
self.analyse_reel(commande_courante.texte)
commande_courante = None
- #on passe à la ligne suivante
+ #on passe a la ligne suivante
continue
+
def enleve (self,texte) :
"""Supprime de texte tous les caracteres blancs, fins de ligne, tabulations
- Le nouveau texte est retourné
+ Le nouveau texte est retourne
"""
i=0
chaine=""
# traitement pour chaque caractere
while (indiceC < len(texte)):
c=texte[indiceC]
- if ( c == "," or c == "(" or c == ")"):
+ if ( c == "," or c == "(u" or c == ")"):
mot=""
elif ( c== "="):
#on doit trouver derriere soit une valeur soit une parenthese
valeur=""
nouvelindice=indiceC+1
- if texte[nouvelindice] != "(":
+ if texte[nouvelindice] != "(u":
#pas de parenthese ouvrante derriere un signe =, on a une valeur.
while ( texte[nouvelindice] != "," and texte[nouvelindice] != ")"):
valeur=valeur+texte[nouvelindice]
else:
#parenthese ouvrante derriere un signe =, on a un tuple de valeur ou de mots cles facteurs.
# s agit -il d un tuple
- if texte[nouvelindice+1] != "(":
+ if texte[nouvelindice+1] != "(u":
#le suivant n'est pas une parenthese ouvrante : on a un tuple de valeurs ou un mot cle facteur
tuple=False
#on avance jusqu'a la fin du tuple de valeurs ou jusqu'a la fin du premier mot cle simple
except :
pass
mot=""
- # ou de ( imbriqueés
+ # ou de ( imbriquees
else :
#cas du mocle facteur simple ou
mot=""
nomConcept=None
# On verifie qu on a bien un OPER
# et pas une MACRO
- if commande.find("=") > commande.find("(") :
+ if commande.find(u"=") > commande.find(u"(u") :
return
- if commande.find("=") > 0:
+ if commande.find(u"=") > 0:
#epure1=self.enleve(commande)
- epure1=pattern_blancs.sub("",commande)
- nomConcept,corps=epure1.split("=",1)
- epure2=corps.replace("_F(","(")
- #nomConcept=epure1.split("=")[0]
- #index=epure1.find("=")
- #epure2=epure1[index+1:len(epure1)].replace("_F(","(")
+ epure1=pattern_blancs.sub(u"",commande)
+ nomConcept,corps=epure1.split(u"=",1)
+ epure2=corps.replace(u"_F(u","(u")
+ #nomConcept=epure1.split(u"=")[0]
+ #index=epure1.find(u"=")
+ #epure2=epure1[index+1:len(epure1)].replace(u"_F(u","(u")
#dict_reel_concept=self.construit_genea(epure2)
if self.appli:
dict_reel_concept=construit_genea(epure2,self.appli.liste_simp_reel)
else:
dict_reel_concept={}
+ if nomConcept == "sansnom" :
+ nomConcept = ""
if nomConcept !=None :
if len(dict_reel_concept) != 0:
self.appli.dict_reels[nomConcept]=dict_reel_concept
"""
self.appli=appli
try:
+ #if 1:
if not self.l_objets : self.analyse()
txt=''
for obj in self.l_objets:
txt = txt+str(obj)
- except ParserException:
+ #else :
+ except ParserException:
#Impossible de convertir le texte, on le retourne tel que
txt=self.texte
return txt
liste_simp_reel=["VALE","VALE_C","GROUP_MA","RAYON"]
a=appli()
- if 1:
- t0=time.clock()
- txt = PARSEUR_PYTHON(texte).get_texte(a)
- print t0,time.clock()-t0
- else:
- import hotshot, hotshot.stats
- prof = hotshot.Profile("stones.prof")
- txt = prof.runcall(PARSEUR_PYTHON(texte).get_texte,a)
- prof.close()
- stats = hotshot.stats.load("stones.prof")
- stats.strip_dirs()
- stats.sort_stats('time', 'calls')
- stats.print_stats(20)
-
- print txt
compile(txt, '<string>', 'exec')
- print a.dict_reels
+ print (a.dict_reels)