1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017 EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 from __future__ import absolute_import
21 from __future__ import print_function
23 from builtins import str
24 from builtins import range
25 from builtins import object
30 from Extensions.i18n import tr
31 from six.moves import range
33 escapedQuotesRE = re.compile(r"(\\\\|\\\"|\\\')")
34 stringsAndCommentsRE = \
35 re.compile(u"(\"\"\".*?\"\"\"|'''.*?'''|\"[^\"]*\"|\'[^\']*\'|#.*?\n)", re.DOTALL)
36 #stringsAndCommentsRE = \
37 # re.compile(u"(\"\"\".*\"\"\"|'''.*'''|\"[^\"]*\"|\'[^\']*\'|#.*\n)", re.DOTALL)
40 allchars = string.maketrans(u"", "")
41 allcharsExceptNewline = allchars[: allchars.index('\n')]+allchars[allchars.index('\n')+1:]
42 allcharsExceptNewlineTranstable = string.maketrans(allcharsExceptNewline, '*'*len(allcharsExceptNewline))
44 allchars=bytes.maketrans(b"",b"")
45 allcharsExceptNewline = allchars[: allchars.index(b'\n')]+allchars[allchars.index(b'\n')+1:]
46 allcharsExceptNewlineTranstable = bytes.maketrans(allcharsExceptNewline, b'*'*len(allcharsExceptNewline))
48 #if sys.platform[0:5]=="linux" :
49 # allcharsExceptNewlineTranstable = string.maketrans(allcharsExceptNewline, '*'*len(allcharsExceptNewline))
50 #elif sys.platform[0:3]=="win" :
51 # allcharsExceptNewlineTranstable = dict((ord(char), u'*') for char in allcharsExceptNewline)#
53 # allcharsExceptNewlineTranstable = string.maketrans(allcharsExceptNewline, '*'*len(allcharsExceptNewline))
55 def maskStringsAndComments(src):
56 """Masque tous les caracteres de src contenus dans des commentaires ou des strings multilignes (triples
58 Le masquage est realise en remplacant les caracteres par des *
59 Attention : cette fonction doit etre utilisee sur un texte complet et pas ligne par ligne
61 # remplace les \\, les \" les \' par **
62 # supprime toutes les chaines ou commentaires ,y compris multiligne i
63 # entre 3 ou 1 simples ou doubles quotes (ouvrantes fermantes) ou #
64 # laisse les non fermantes ou non ouvrantes
65 # on prend 1 sur 2 en raison du split qui donne python, commentaire, python, commentaire...
67 src = escapedQuotesRE.sub("**", src)
68 allstrings = stringsAndCommentsRE.split(src)
70 # on a une liste d elements constituee successivement de (string, comment)
71 for i in range(1, len(allstrings), 2):
72 if allstrings[i].startswith(u"'''")or allstrings[i].startswith('"""'):
73 allstrings[i] = allstrings[i][:3]+ \
74 allstrings[i][3:-3].translate(allcharsExceptNewlineTranstable)+ \
77 allstrings[i] = allstrings[i][0]+ \
78 allstrings[i][1:-1].translate(allcharsExceptNewlineTranstable)+ \
81 return "".join(allstrings)
83 implicitContinuationChars = (('(', ')'), ('[', ']'), ('{', '}'))
84 linecontinueRE = re.compile(r"\\\s*(#.*)?$")
85 emptyHangingBraces = [0,0,0,0,0]
87 class parserException(Exception): pass
88 class FatalError(Exception): pass
90 #commentaire double precede d'un nombre quelconque de blancs (pas multiligne)
91 pattern_2comments = re.compile(r"^\s*##.*")
92 pattern_finComments = re.compile("^\s*##Fin Commentaire")
93 #commentaire standard precede d'un nombre quelconque de blancs (pas multiligne)
94 pattern_comment = re.compile(r"^\s*#.*")
95 #fin de ligne ; suivi d'un nombre quelconque de blancs (pas multiligne)
96 pattern_fin = re.compile(r"; *$")
97 #pattern pour supprimer les blancs, tabulations et fins de ligne
98 pattern_blancs = re.compile(r"[ \t\r\f\v]")
99 #pattern_blancs = re.compile(r"[\s\n]")
100 number_kw_pattern=re.compile(r"""
102 #groupe nombre decimal
104 #signe : on ignore le signe +
106 #groupe (avec ?: n'apparait pas en tant que groupe dans le resultat)
108 #mantisse forme entiere.fractionnaire
111 #ou forme .fractionnaire
120 """,re.VERBOSE|re.MULTILINE)
122 def construitGenea(texte,listeMc):
124 Retourne un dictionnaire dont les cles sont des reels et les valeurs sont leurs representations textuelles.
126 Realise un filtrage sur les reels :
128 - Ne garde que les reels pour lesquels str ne donne pas une bonne representation.
129 - Ne garde que les reels derriere un argument keyword dont le nom est dans listeMc
131 >>> s = '''a=+21.3e-5*85,b=-.1234,c=81.6 , d= -8 , e=_F(x=342.67,y=-1), f=+1.1, g=(1.3,-5,1.54E-3),
132 ... #POMPE_PRIMA._BOUCLE_N._2_ELEMENT_NUMERO:0239
133 ... h=_F(x=34.6,y=-1)'''
134 >>> construitGenea(s,['a','x'])
135 {0.000213: '21.3e-5'}
139 #on masque les strings et commentaires pour ne pas identifier de faux reels
140 for m in number_kw_pattern.findall(maskStringsAndComments(texte)):
145 if mot not in listeMc:continue
148 if str(key) != m: d[key]=m
152 class ENTITE_JDC(object) :
153 """Classe de base pour tous les objets crees lors de la conversion
154 Tout objet derive est enregistre aupres de son pere a sa creation
156 def __init__(self,pere):
158 pere.l_objets.append(self)
160 def setText(self,texte):
163 def appendText(self,texte):
165 Ajoute texte a self.texte en mettant un retour chariot a la fin de texte
168 self.texte = self.texte +texte
173 class COMMENTAIRE(ENTITE_JDC):
177 Retourne une chaine de caracteres representants self
178 sous une forme interpretable par EFICAS
181 return "COMMENTAIRE(u"+t+")\n"
183 #s='COMMENTAIRE(u"""'+self.texte+'""")\n\n'
186 def appendText(self,texte):
188 Ajoute texte a self.texte en enlevant le # initial
192 self.texte = self.texte+texte[1:]
194 # le diese n'est pas sur le premier caractere
195 amont,aval = texte.split('#',1) # on decoupe suivant la premiere occurrence de #
196 self.texte = self.texte +amont + aval
198 class COMMANDE(ENTITE_JDC):
204 return self.texte+'\n'
208 Retourne la difference entre le nombre de parentheses ouvrantes
209 et le nombre de parentheses fermantes presentes dans self.texte
210 Peut donc retourner un entier negatif
212 # faire attention aux commentaires contenus dans self.texte
213 # qui peuvent eux-memes contenir des parentheses !!!!
214 l_lignes = self.texte.split('\n')
216 for ligne in l_lignes:
217 ligne = ligne.split('#')[0]
218 #nb = nb + (string.count(ligne,'(')-string.count(ligne,')'))
220 nb = nb + ( ligne.count('(') - ligne.count(')') )
223 class AFFECTATION(ENTITE_JDC):
225 def appendText(self,texte):
227 Ajoute texte a self.texte en enlevant tout retour chariot et tout point virgule
228 PN et tout commentaire
230 if texte[-1] == '\n' : texte = texte[0:-1].rstrip()
231 if texte[-1] == ';' : texte = texte[0:-1].rstrip()
232 self.texte = self.texte+texte+'\n'
236 Retourne une expression de l'affectation comprehensible par ACCAS
237 et exploitable par EFICAS
239 nom,valeur = self.texte.split('=',1)
242 if valeur[-1] == '\n': valeur = valeur[:-1]
243 return n + ' = PARAMETRE(nom=\''+nom+'\',valeur='+valeur+')\n'
245 class COMMANDE_COMMENTARISEE(ENTITE_JDC):
247 def appendText(self,texte):
249 Ajoute texte a self.texte en enlevant les doubles commentaires
251 texte = texte.strip()
252 texte = texte[2:].strip()
253 self.texte = self.texte+(len(self.texte)>0)*'\n'+texte
257 Retourne une expression de la commande commentarisee comprehensible par ACCAS
258 et exploitable par EFICAS
260 return "COMMANDE_COMM(texte="+repr(self.texte)+")\n"
261 #return "COMMANDE_COMM(texte='''"+self.texte+"''')\n"
263 class AFFECTATION_EVAL(ENTITE_JDC):
265 def appendText(self,texte):
267 Ajoute texte a self.texte en enlevant tout retour chariot
269 if texte[-1] == '\n' : texte = texte[1:-1]
270 self.texte = self.texte+texte
274 Retourne une expression du parametre EVAL comprehensible par ACCAS
275 et exploitable par EFICAS
277 nom,valeur = self.texte.split('=',1)
279 if valeur[-1] == '\n': valeur = valeur[:-1]
280 valeur = valeur.strip()
281 return nom+' = PARAMETRE_EVAL(nom=\''+nom+'\',valeur=\''+valeur+'\')\n\n'
283 class PARSEUR_PYTHON(object):
285 Cette classe sert a generer un objet PARSEUR_PYTHON qui realise l'analyse d'un texte
286 representant un JDC Python en distinguant :
287 - les commentaires inter commandes
291 pattern_commande = re.compile(r'^([A-Z][a-zA-Z0-9_]+)([ \t\r\f\v]*)\(([\w\W]*)')
292 pattern_eval = re.compile(r'^(EVAL)([ \t\r\f\v]*)\(([\w\W]*)')
293 pattern_ligne_vide = re.compile(r'^[\t\r\f\v\n]+')
294 pattern_name = re.compile(r'[a-zA-Z_]\w*')
296 def __init__(self,texte):
301 def isAffectation(self,texte):
303 Methode booleenne qui retourne 1 si le texte est celui d'une affectation dans un jeu de commandes
307 if '=' not in texte : return 0
308 if self.pattern_commande.match(texte):
309 # cas d'une procedure ...
311 amont,aval = texte.split('=',1)
315 if self.pattern_commande.match(aval):
319 m= self.pattern_name.match(s)
320 if m is None : return 0
321 if m.start() != 0 :return 0
322 if m.end() != len(s):return 0
325 def isEval(self,texte):
327 Methode booleenne qui retourne 1 si le texte est celui d'une affectation de type EVAL
328 dans un jeu de commandes Aster, 0 sinon
330 if '=' not in texte : return 0
331 if self.pattern_commande.match(texte):
332 # cas d'une procedure ...
334 amont,aval = texte.split('=',1)
336 if not self.pattern_commande.match(aval) : return 0
337 if self.pattern_eval.match(aval):
342 def isCommande(self,texte):
344 Methode booleenne qui retourne 1 si le texte est celui d'une commande dans un jeu de commandes
347 if self.pattern_commande.match(texte):
348 # cas d'une procedure ...
350 # A ce stade il faut avoir un OPER ou une MACRO, bref un '=' !
351 if '=' not in texte : return 0
352 # on a un texte de la forme xxxx = yyyyy
353 # --> reste a analyser yyyy
354 amont,aval = texte.split('=',1)
356 if self.pattern_commande.match(aval):
361 def isModificationCatalogue(self,texte) :
362 if self.pattern_commande.match(texte):
367 Eclate la chaine self.texte en self.l_objets une liste lignes d'instructions
368 et de commentaires (parmi lesquels des instructions "commentarisees").
370 l_lignes = self.texte.split('\n')
371 commentaire_courant = None
372 commande_courante = None
373 affectation_courante = None
374 commande_commentarisee_courante = None
377 #initialisation du nombre de parentheses non fermees et de commentaires non termines
378 #Attention a reinitialiser en fin de ligne logique
379 #Une ligne logique peut s'etendre sur plusieurs lignes physiques avec des caracteres de continuation
380 #explicites ou implicites
381 hangingBraces = list(emptyHangingBraces)
384 #Masquage des commentaires et strings multilignes
385 srcMasked=maskStringsAndComments('\n'.join(l_lignes))
386 masked_lines=srcMasked.split('\n')
389 for ligne in l_lignes :
390 line=masked_lines[lineno]
392 #print ("ligne:",line)
393 # mise a jour du nombre total de parentheses ouvertes (non fermees)
394 # et du nombre de commentaires non termines
395 for i in range(len(implicitContinuationChars)):
396 contchar = implicitContinuationChars[i]
397 numHanging = hangingBraces[i]
398 hangingBraces[i] = numHanging+line.count(contchar[0]) - line.count(contchar[1])
400 hangingComments ^= line.count('"""') % 2
401 hangingComments ^= line.count(u"'''") % 2
402 #print (hangingComments,hangingBraces)
403 if hangingBraces[0] < 0 or hangingBraces[1] < 0 or hangingBraces[2] < 0:
404 raise parserException()
406 if ligne.strip() == '':
407 # il s'agit d'un saut de ligne
411 if pattern_2comments.match(ligne):
412 #on a trouve une commande commentarisee : double commentaire sans rien devant a part des blancs
413 if commentaire_courant:
414 #Si un commentaire ordinaire est en cours on le termine
415 commentaire_courant = None
417 if commande_courante :
418 # on a un objet commentarise a l'interieur d'une commande
419 # --> non traite pour l'instant : on l'ajoute simplement a la commande courante comme
420 # un commentaire ordinaire
421 commande_courante.appendText(ligne)
422 elif commande_commentarisee_courante :
423 # commande_commentarisee en cours : on ajoute la ligne
424 commande_commentarisee_courante.appendText(ligne)
425 # on a 2 commandes commentarisees de suite
426 if pattern_finComments.match(ligne) :
427 commande_commentarisee_courante = None
429 # debut de commande commentarisee : on cree un objet commande_commentarisee_courante
430 commande_commentarisee_courante = COMMANDE_COMMENTARISEE(self)
431 commande_commentarisee_courante.appendText(ligne)
433 #on passe a la ligne suivante
436 if pattern_comment.match(ligne):
437 #commentaire ordinaire avec seulement des blancs devant
438 if commande_commentarisee_courante :
439 # commande_commentarisee en cours : on la clot
440 commande_commentarisee_courante = None
442 if commande_courante :
443 # il s'agit d'un commentaire a l'interieur d'une commande --> on ne fait rien de special
444 #on l'ajoute au texte de la commande
445 commande_courante.appendText(ligne)
446 elif commentaire_courant :
447 # il s'agit de la nieme ligne d'un commentaire entre deux commandes
448 # --> on ajoute cette ligne au commentaire courant
449 commentaire_courant.appendText(ligne)
451 # il s'agit d'un nouveau commentaire entre deux commandes
452 # --> on le cree et il devient le commentaire courant
453 commentaire_courant = COMMENTAIRE(self)
454 commentaire_courant.appendText(ligne)
456 #on passe a la ligne suivante
459 # la ligne contient des donnees autre qu'un eventuel commentaire
460 if commentaire_courant :
461 # on clot un eventuel commentaire courant
462 commentaire_courant = None
464 if commande_commentarisee_courante :
465 # on clot une eventuelle commande commentarisee courante
466 commande_commentarisee_courante = None
468 if commande_courante :
469 #on a une commande en cours. On l'enrichit ou on la termine
470 commande_courante.appendText(ligne)
471 if not linecontinueRE.search(line) \
472 and (hangingBraces == emptyHangingBraces) \
473 and not hangingComments:
474 #la commande est terminee
475 self.analyseReel(commande_courante.texte)
476 commande_courante = None
478 #on passe a la ligne suivante
481 if affectation_courante != None :
482 #poursuite d'une affectation
483 affectation_courante.appendText(ligne)
484 if not linecontinueRE.search(line) \
485 and (hangingBraces == emptyHangingBraces) \
486 and not hangingComments:
487 #L'affectation est terminee
488 affectation_courante=None
489 #on passe a la ligne suivante
492 # il peut s'agir d'une commande ou d'une affectation ...
494 if self.isEval(ligne):
495 # --> affectation de type EVAL
496 if affectation_courante : affectation_courante = None
497 affectation = AFFECTATION_EVAL(self)
498 affectation.appendText(ligne)
499 #on passe a la ligne suivante
502 if self.isAffectation(ligne):
503 #print( '--> affectation')
505 #traitement des commentaires en fin de ligne
506 compos=line.find(u"#")
508 #commentaire en fin de ligne
509 #on cree un nouveau commentaire avant le parametre
510 COMMENTAIRE(self).appendText(ligne[compos:])
512 #si plusieurs instructions separees par des ; sur la meme ligne
513 inspos=line.find(u";")
515 #on garde seulement la premiere partie de la ligne
516 #si on a que des blancs apres le point virgule
517 if text[inspos:].strip() == ";":
520 raise FatalError(tr("Eficas ne peut pas traiter plusieurs instructions \
521 sur la meme ligne : %s", ligne))
523 affectation_courante = AFFECTATION(self)
524 affectation_courante.appendText(text)
525 if not linecontinueRE.search(line) \
526 and (hangingBraces == emptyHangingBraces) \
527 and not hangingComments:
528 #L'affectation est terminee
529 affectation_courante=None
530 #on passe a la ligne suivante
533 if self.isCommande(ligne):
534 # --> nouvelle commande
535 affectation_courante = None
536 commande_courante = COMMANDE(self)
537 commande_courante.appendText(ligne)
538 #si la commande est complete, on la termine
539 if not linecontinueRE.search(line) \
540 and (hangingBraces == emptyHangingBraces) \
541 and not hangingComments:
542 #la commande est terminee
543 self.analyseReel(commande_courante.texte)
544 commande_courante = None
545 #on passe a la ligne suivante
549 def enleve (self,texte) :
550 """Supprime de texte tous les caracteres blancs, fins de ligne, tabulations
551 Le nouveau texte est retourne
555 while (i<len(texte)):
556 if (texte[i] == " " or texte[i] == "\n" or texte[i] == "\t") :
559 chaine=chaine+texte[i]
563 def construitGenea(self,texte):
568 # traitement pour chaque caractere
569 while (indiceC < len(texte)):
571 if ( c == "," or c == "(u" or c == ")"):
574 #on doit trouver derriere soit une valeur soit une parenthese
576 nouvelindice=indiceC+1
577 if texte[nouvelindice] != "(u":
578 #pas de parenthese ouvrante derriere un signe =, on a une valeur.
579 while ( texte[nouvelindice] != "," and texte[nouvelindice] != ")"):
580 valeur=valeur+texte[nouvelindice]
581 nouvelindice=nouvelindice+1
582 if nouvelindice == len(texte) :
583 nouvelindice=nouvelindice -1
585 if mot in self.appli.liste_simp_reel:
589 if str(clef) != str(valeur) :
590 dict_reel_concept[clef]=valeur
596 #parenthese ouvrante derriere un signe =, on a un tuple de valeur ou de mots cles facteurs.
597 # s agit -il d un tuple
598 if texte[nouvelindice+1] != "(u":
599 #le suivant n'est pas une parenthese ouvrante : on a un tuple de valeurs ou un mot cle facteur
601 #on avance jusqu'a la fin du tuple de valeurs ou jusqu'a la fin du premier mot cle simple
602 #contenu dans le mot cle facteur
603 while ( texte[nouvelindice] != "="):
604 if texte[nouvelindice] == ")" :
608 nouvelindice=nouvelindice+1
609 if nouvelindice == len(texte) :
610 nouvelindice=nouvelindice -1
613 #cas du tuple de valeurs
614 valeur=texte[indiceC+1:nouvelindice+1]
615 indiceC=nouvelindice+1
616 if mot in self.appli.liste_simp_reel:
618 for val in valeur.split(',') :
619 # Attention la derniere valeur est""
623 if str(clef) != str(val) :
624 dict_reel_concept[clef]=val
630 #cas du mocle facteur simple ou
633 mot=mot+texte[indiceC]
635 # traitement du dernier inutile
637 return dict_reel_concept
639 def analyseReel(self,commande) :
641 # On verifie qu on a bien un OPER
643 if commande.find(u"=") > commande.find(u"(u") :
645 if commande.find(u"=") > 0:
646 #epure1=self.enleve(commande)
647 epure1=pattern_blancs.sub(u"",commande)
648 nomConcept,corps=epure1.split(u"=",1)
649 epure2=corps.replace(u"_F(u","(u")
650 #nomConcept=epure1.split(u"=")[0]
651 #index=epure1.find(u"=")
652 #epure2=epure1[index+1:len(epure1)].replace(u"_F(u","(u")
653 #dict_reel_concept=self.construitGenea(epure2)
655 dict_reel_concept=construitGenea(epure2,self.appli.liste_simp_reel)
658 if nomConcept == "sansnom" :
660 if nomConcept !=None :
661 if len(dict_reel_concept) != 0:
662 self.appli.dict_reels[nomConcept]=dict_reel_concept
664 def getTexte(self,appli=None):
666 Retourne le texte issu de l'analyse
671 if not self.l_objets : self.analyse()
673 for obj in self.l_objets:
676 except parserException:
677 #Impossible de convertir le texte, on le retourne tel que
682 #import parseur_python
684 doctest.testmod(parseur_python)
687 if __name__ == "__main__" :
689 #fichier = 'D:/Eficas_dev/Tests/zzzz100a.comm'
690 #fichier = 'U:/Eficas_dev/Tests/test_eval.comm'
691 texte = open(fichier,'r').read()
694 liste_simp_reel=["VALE","VALE_C","GROUP_MA","RAYON"]
697 compile(txt, '<string>', 'exec')
698 print((a.dict_reels))