X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=convert%2Fparseur_python.py;h=19d73256de77e60caf79b1e4929d2c6745ff8044;hb=0a3e507740b8094749648d8292fa83b10e2ea864;hp=811c25fb4ece59c0e6829a29b7516e277cd10bc6;hpb=5a28801efd8fd7e6487d1957dabac40b1f932cb4;p=tools%2Feficas.git diff --git a/convert/parseur_python.py b/convert/parseur_python.py index 811c25fb..19d73256 100644 --- a/convert/parseur_python.py +++ b/convert/parseur_python.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2007-2017 EDF R&D +# Copyright (C) 2007-2021 EDF R&D # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -20,15 +20,13 @@ from __future__ import absolute_import from __future__ import print_function try : - from builtins import str - from builtins import range - from builtins import object + from builtins import str + from builtins import object except : - pass + pass import sys,string,re import traceback from Extensions.i18n import tr -from six.moves import range escapedQuotesRE = re.compile(r"(\\\\|\\\"|\\\')") stringsAndCommentsRE = \ @@ -36,11 +34,11 @@ stringsAndCommentsRE = \ #stringsAndCommentsRE = \ # re.compile(u"(\"\"\".*\"\"\"|'''.*'''|\"[^\"]*\"|\'[^\']*\'|#.*\n)", re.DOTALL) import six -if six.PY2 : +if six.PY2 : allchars = string.maketrans(u"", "") allcharsExceptNewline = allchars[: allchars.index('\n')]+allchars[allchars.index('\n')+1:] allcharsExceptNewlineTranstable = string.maketrans(allcharsExceptNewline, '*'*len(allcharsExceptNewline)) -else : +else : allchars=bytes.maketrans(b"",b"") allcharsExceptNewline = allchars[: allchars.index(b'\n')]+allchars[allchars.index(b'\n')+1:] allcharsExceptNewlineTranstable = bytes.maketrans(allcharsExceptNewline, b'*'*len(allcharsExceptNewline)) @@ -55,12 +53,12 @@ else : 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 * + 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 # +# entre 3 ou 1 simples ou doubles quotes (ouvrantes fermantes) ou # # laisse les non fermantes ou non ouvrantes # on prend 1 sur 2 en raison du split qui donne python, commentaire, python, commentaire... @@ -194,7 +192,7 @@ class COMMENTAIRE(ENTITE_JDC): # le diese n'est pas sur le premier caractere amont,aval = texte.split('#',1) # on decoupe suivant la premiere occurrence de # self.texte = self.texte +amont + aval - + class COMMANDE(ENTITE_JDC): def __str__(self): @@ -202,7 +200,7 @@ class COMMANDE(ENTITE_JDC): Retourne self.texte """ return self.texte+'\n' - + def getNbPar(self): """ Retourne la difference entre le nombre de parentheses ouvrantes @@ -230,7 +228,7 @@ class AFFECTATION(ENTITE_JDC): if texte[-1] == '\n' : texte = texte[0:-1].rstrip() if texte[-1] == ';' : texte = texte[0:-1].rstrip() self.texte = self.texte+texte+'\n' - + def __str__(self): """ Retourne une expression de l'affectation comprehensible par ACCAS @@ -268,7 +266,7 @@ class AFFECTATION_EVAL(ENTITE_JDC): """ if texte[-1] == '\n' : texte = texte[1:-1] self.texte = self.texte+texte - + def __str__(self): """ Retourne une expression du parametre EVAL comprehensible par ACCAS @@ -279,10 +277,10 @@ class AFFECTATION_EVAL(ENTITE_JDC): if valeur[-1] == '\n': valeur = valeur[:-1] valeur = valeur.strip() return nom+' = PARAMETRE_EVAL(nom=\''+nom+'\',valeur=\''+valeur+'\')\n\n' - + class PARSEUR_PYTHON(object): """ - Cette classe sert a generer un objet PARSEUR_PYTHON qui realise l'analyse d'un texte + Cette classe sert a generer un objet PARSEUR_PYTHON qui realise l'analyse d'un texte representant un JDC Python en distinguant : - les commentaires inter commandes - les affectations @@ -292,18 +290,17 @@ class PARSEUR_PYTHON(object): pattern_eval = re.compile(r'^(EVAL)([ \t\r\f\v]*)\(([\w\W]*)') pattern_ligne_vide = re.compile(r'^[\t\r\f\v\n]+') pattern_name = re.compile(r'[a-zA-Z_]\w*') - + def __init__(self,texte): self.texte = texte self.l_objets=None - self.appli=None + self.appliEficas=None def isAffectation(self,texte): """ Methode booleenne qui retourne 1 si le texte est celui d'une affectation dans un jeu de commandes Aster, 0 sinon """ - print (texte) if '=' not in texte : return 0 if self.pattern_commande.match(texte): # cas d'une procedure ... @@ -338,7 +335,7 @@ class PARSEUR_PYTHON(object): return 1 else: return 0 - + def isCommande(self,texte): """ Methode booleenne qui retourne 1 si le texte est celui d'une commande dans un jeu de commandes @@ -360,7 +357,7 @@ class PARSEUR_PYTHON(object): def isModificationCatalogue(self,texte) : if self.pattern_commande.match(texte): - return 1 + return 1 def analyse(self): """ @@ -400,7 +397,7 @@ class PARSEUR_PYTHON(object): hangingComments ^= line.count('"""') % 2 hangingComments ^= line.count(u"'''") % 2 #print (hangingComments,hangingBraces) - if hangingBraces[0] < 0 or hangingBraces[1] < 0 or hangingBraces[2] < 0: + if hangingBraces[0] < 0 or hangingBraces[1] < 0 or hangingBraces[2] < 0: raise parserException() if ligne.strip() == '': @@ -424,7 +421,7 @@ class PARSEUR_PYTHON(object): commande_commentarisee_courante.appendText(ligne) # on a 2 commandes commentarisees de suite if pattern_finComments.match(ligne) : - commande_commentarisee_courante = None + commande_commentarisee_courante = None else: # debut de commande commentarisee : on cree un objet commande_commentarisee_courante commande_commentarisee_courante = COMMANDE_COMMENTARISEE(self) @@ -441,7 +438,7 @@ class PARSEUR_PYTHON(object): if commande_courante : # il s'agit d'un commentaire a l'interieur d'une commande --> on ne fait rien de special - #on l'ajoute au texte de la commande + #on l'ajoute au texte de la commande commande_courante.appendText(ligne) elif commentaire_courant : # il s'agit de la nieme ligne d'un commentaire entre deux commandes @@ -471,7 +468,7 @@ class PARSEUR_PYTHON(object): if not linecontinueRE.search(line) \ and (hangingBraces == emptyHangingBraces) \ and not hangingComments: - #la commande est terminee + #la commande est terminee self.analyseReel(commande_courante.texte) commande_courante = None @@ -539,13 +536,13 @@ class PARSEUR_PYTHON(object): if not linecontinueRE.search(line) \ and (hangingBraces == emptyHangingBraces) \ and not hangingComments: - #la commande est terminee + #la commande est terminee self.analyseReel(commande_courante.texte) commande_courante = None #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 retourne @@ -553,87 +550,87 @@ class PARSEUR_PYTHON(object): i=0 chaine="" while (i', 'exec') print((a.dict_reels))