--- /dev/null
+# -*- coding: utf-8 -*-
+import logging
+from parseur import FactNode
+debug=1
+
+
+#-----------------------------------
+def inseremotcle(jdc,recepteur,texte):
+#-----------------------------------
+# appelle la methode selon la classe
+# du recepteur
+
+ if recepteur.__class__.__name__ == "Command" :
+ if debug : print " Ajout de ", texte, "dans la commande : " ,recepteur.name
+ inseremotcleincommand(jdc,recepteur,texte)
+ return
+
+
+#--------------------------------------------
+def inseremotcleincommand(jdc,command,texte):
+#---------------------------------------------
+# insere le texte comme 1er mot cle
+# de la commande
+ if debug : print "inseremotcle ", texte , " dans ", command.name
+ numcol=chercheDebut1mot(jdc,command)
+ if numcol > 0 :
+ jdc.splitLine(command.lineno,numcol)
+ debut=chercheAlignement(jdc,command)
+ texte=debut+texte+"\n"
+ jdc.addLine(texte,command.lineno)
+ if numcol > 0 : # Les mots clefs etaient sur la même ligne
+ jdc.joinLineandNext(command.lineno)
+
+#-----------------------------------
+def chercheDebut1mot(jdc,command):
+#-----------------------------------
+# Retourne le numero de colonne si le 1er mot clef est
+# sur la meme ligne que le mot clef facteur
+# -1 sinon
+ assert (command.childNodes != [])
+ debut=-1
+ node1=command.childNodes[0]
+ if node1.lineno == command.lineno :
+ debut=node1.colno
+ return debut
+
+
+#-----------------------------------
+def chercheAlignement(jdc,command):
+#-----------------------------------
+# Retourne le nb de blanc
+# pour aligner sur le 1er mot clef fils
+ assert (command.childNodes != [])
+ node1=command.childNodes[0]
+ nbBlanc=node1.colno
+ return " "*nbBlanc
from mocles import parseKeywords
import removemocle
import renamemocle
+import movemocle
-atraiter=("DEBUT","LIRE_MAILLAGE","AFFE_MODELE","DEFI_GROUP",
- "AFFE_MATERIAU","DEFI_MATERIAU","STAT_NONLINE",
- )
+#atraiter=("DEBUT","LIRE_MAILLAGE","AFFE_MODELE","DEFI_GROUP",
+# "AFFE_MATERIAU","DEFI_MATERIAU","STAT_NONLINE",
+# )
+atraiter=("CALC_FONCTION","IMPR_GENE")
filename="toto.comm"
jdc=getJDC(filename,atraiter)
root=jdc.root
#Parse les mocles des commandes
parseKeywords(root)
-removemocle.removemocleinfact(jdc,"AFFE_MATERIAU","AFFE","TOUT")
-removemocle.removemocle(jdc,"STAT_NONLINE","SOLVEUR")
-renamemocle.renamemocleinfact(jdc,"AFFE_MODELE","AFFE","PHENOMENE","TOTO")
-renamemocle.renamemocleinfact(jdc,"AFFE_MODELE","AFFE","MODELISATION","TITI")
-renamemocle.renamemocleinfact(jdc,"DEFI_GROUP","CREA_GROUP_NO","GROUP_MA","TUTU")
-removemocle.removemocle(jdc,"LIRE_MAILLAGE","INFO")
-removemocle.removemocle(jdc,"LIRE_MAILLAGE","UNITE")
-renamemocle.renamemocle(jdc,"DEFI_MATERIAU","ELAS","ELASTIC")
-renamemocle.renamemocle(jdc,"AFFE_MATERIAU","MAILLAGE","MAILL")
-removemocle.removemocleinfact(jdc,"STAT_NONLINE","SOLV","METHOD")
-removemocle.removemocle(jdc,"STAT_NONLINE","AFFE")
+#removemocle.removemocleinfact(jdc,"AFFE_MATERIAU","AFFE","TOUT")
+#removemocle.removemocle(jdc,"STAT_NONLINE","SOLVEUR")
+#renamemocle.renamemocleinfact(jdc,"AFFE_MODELE","AFFE","PHENOMENE","TOTO")
+#renamemocle.renamemocleinfact(jdc,"AFFE_MODELE","AFFE","MODELISATION","TITI")
+#renamemocle.renamemocleinfact(jdc,"DEFI_GROUP","CREA_GROUP_NO","GROUP_MA","TUTU")
+#removemocle.removemocle(jdc,"LIRE_MAILLAGE","INFO")
+#removemocle.removemocle(jdc,"LIRE_MAILLAGE","UNITE")
+#renamemocle.renamemocle(jdc,"DEFI_MATERIAU","ELAS","ELASTIC")
+#renamemocle.renamemocle(jdc,"AFFE_MATERIAU","MAILLAGE","MAILL")
+#removemocle.removemocleinfact(jdc,"STAT_NONLINE","SOLV","METHOD")
+#removemocle.removemocle(jdc,"STAT_NONLINE","AFFE")
+
+# renamecommande(jdc,ancien-nom-de-commande,nouveau-nom-de-commande)
+#renamemocle.renamecommande(jdc,"CALC_FONCTION","INFO_FONCTION")
+
+movemocle.movemoclefromfacttofather(jdc,"IMPR_GENE","GENE","UNITE")
+movemocle.movemoclefromfacttofather(jdc,"IMPR_GENE","GENE","FORMAT")
+
+#renamemocle.renamecommande(jdc,"AFFE_CHAR_MECA","AFFE_CHAR_MECA_PN")
+#renamemocle.renamecommande(jdc,"DEBUT","DEBUT_PN")
f=open("tutu.comm",'w')
import parseur
from mocles import parseKeywords
-atraiter=("DEBUT","LIRE_MAILLAGE","AFFE_MODELE","DEFI_GROUP",
- "AFFE_MATERIAU","DEFI_MATERIAU",
- )
JDCdict={}
class JDC:
"""Cet objet conserve toutes les informations relatives à un fichier de commandes .comm"""
+
def __init__(self,filename,src,atraiter):
+ #----------------------------------------
self.filename = os.path.abspath(filename)
self.atraiter=atraiter
self.init(src,atraiter)
def init(self,src,atraiter):
+ #---------------------------
+ # construction de self.lines
self.root=parseur.parser(src,atraiter)
self.lines=src.splitlines(1)
def parseKeywords(self):
+ #-----------------------
+ # construction de fils (cf mocles.py)
parseKeywords(self.root)
def reset(self,src):
+ #-----------------------
+ # reconstruction
self.init(src,self.atraiter)
self.parseKeywords()
def getSource(self):
+ #-----------------------
+ # retourne la concatenation de
+ # toutes les lignes
return "".join(self.getLines())
def getLine(self,linenum):
+ #-----------------------
+ # retourne la linenumieme ligne
return self.getLines()[linenum-1]
def getLines(self):
+ #----------------------------
+ # retourne toutes les lignes
return self.lines
-def getJDC(filename,atraiter=atraiter):
+ def addLine(self,ligne,numero) :
+ #----------------------------
+ # insere le texte contenu dans ligne
+ # dans la liste self.lines au rang numero
+ Ldebut=self.lines[0:numero]
+ Lmilieu=[ligne,]
+ Lfin=self.lines[numero:]
+ self.lines=Ldebut+Lmilieu+Lfin
+ src=self.getSource()
+ self.reset(src)
+
+ def splitLine(self,numeroLigne,numeroColonne) :
+ #----------------------------------------------
+ # coupe la ligne numeroLigne en 2 a numeroColonne
+ # ajoute des blancs en debut de 2nde Ligne pour
+ # aligner
+ numeroLigne = numeroLigne -1
+ Ldebut=self.lines[0:numeroLigne]
+ if len(self.lines) > numeroLigne :
+ Lfin=self.lines[numeroLigne+1:]
+ else :
+ Lfin=[]
+ Lsplit=self.lines[numeroLigne]
+ LigneSplitDebut=Lsplit[0:numeroColonne]+"\n"
+ LigneSplitFin=" "*numeroColonne+Lsplit[numeroColonne:]
+ Lmilieu=[LigneSplitDebut,LigneSplitFin]
+
+ self.lines=Ldebut+Lmilieu+Lfin
+ src=self.getSource()
+ self.reset(src)
+
+ def joinLineandNext(self,numeroLigne) :
+ #--------------------------------------
+ # concatene les lignes numeroLigne et numeroLigne +1
+ # enleve les blancs de debut de la ligne (numeroLigne +1)
+ Ldebut=self.lines[0:numeroLigne-1]
+ if len(self.lines) > numeroLigne :
+ Lfin=self.lines[numeroLigne+1:]
+ else :
+ Lfin=[]
+
+ ligneMilieuDeb=self.lines[numeroLigne - 1 ]
+ ligneMilieuDeb=ligneMilieuDeb[0:-1]
+ ligneMilieuFin=self.lines[numeroLigne]
+ for i in range(len(ligneMilieuFin)):
+ if ligneMilieuFin[i] != " " :
+ ligneMilieuFin=ligneMilieuFin[i:]
+ break
+ Lmilieu=[ligneMilieuDeb+ligneMilieuFin,]
+
+ self.lines=Ldebut+Lmilieu+Lfin
+ src=self.getSource()
+ self.reset(src)
+
+def getJDC(filename,atraiter):
+#---------------------------_
+# lit le JDC
jdc=JDCdict.get(filename)
if not jdc:
f=open(filename)
--- /dev/null
+import logging
+import removemocle
+import inseremocle
+from parseur import FactNode
+debug=1
+
+def movemoclefromfacttofather(jdc,command,fact,mocle):
+ for c in jdc.root.childNodes:
+ if c.name != command:continue
+ for mc in c.childNodes:
+ if mc.name != fact:continue
+ l=mc.childNodes[:]
+ for ll in l:
+ for n in ll.childNodes:
+ if n.name != mocle:continue
+ if debug : print "Changement de place :", n.name, n.lineno, n.colno
+ MonTexte=n.getText(jdc);
+ inseremocle.inseremotcle(jdc,c,MonTexte)
+ logging.info("Changement de place : %s,%s, %s ",n.name, n.lineno, n.colno)
+
+ removemocle.removemocleinfact(jdc,command,fact,mocle)
+
+
+
def addChild(self,node):
self.childNodes.append(node)
+
class FactNode(Node):pass
class JDCNode(Node):
def __init__(self,src):
self.endline=endline
self.endcol=endcol
+ def getText(self,jdc):
+ if self.endline > self.lineno:
+ lignecourante=self.lineno + 1
+ debut=jdc.getLines()[self.lineno-1][self.colno:]
+ fin = jdc.getLines()[self.endline-1][:self.endcol]
+ texte=debut
+ lignecourante=self.lineno + 1
+ while lignecourante > self.endline :
+ texte = texte + jdc.getLines()[lignecourante]
+ lignecourante = lignecourante + 1
+ if chaineBlanche(fin) == 0 :
+ texte=texte + fin
+ if texte[-1] == "\n" :
+ texte=texte[0:-1]
+ else:
+ texte = jdc.getLines()[self.lineno-1][self.colno:self.endcol]
+ return texte
+
+def chaineBlanche(texte) :
+# retourne 1 si la chaine est composee de " "
+# retourne 0 sinon
+ bool = 1 ;
+ for i in range(len(texte)) :
+ if texte[i] != " " : bool = 0
+ return bool
+
def printNode(node):
if hasattr(node,'name'):
print node.name
"""
lines=src.splitlines(1)
maskedSrc=maskStringsAndComments(src)
- #print maskedSrc
maskedLines=maskedSrc.splitlines(1)
root=JDCNode(src)
for i in range(len(implicitContinuationChars)):
contchar = implicitContinuationChars[i]
numHanging = hangingBraces[i]
+
hangingBraces[i] = numHanging+line.count(contchar[0]) - \
line.count(contchar[1])
if level == 0:
#derniere parenthese fermante
return i
+
# -*- coding: utf-8 -*-
import logging
from parseur import FactNode
-debug=0
+debug=1
+
#on n'a qu'un mocle par commande.
#en fin de traitement, on remet à jour l'arbre syntaxique (lineno,colno,etc.)
+
def renamemocle(jdc,command,mocle,new_name):
for c in jdc.root.childNodes:
if c.name != command:continue
jdc.reset(jdc.getSource())
+def renamecommande(jdc,command,new_name):
+# nom de la commande "ancien format" , nom de la commande " nouveau format "
+ for c in jdc.root.childNodes:
+ if c.name != command:continue
+ if debug:print "Renommage de:",c.name,new_name ,c.lineno,c.colno
+ logging.info("Renommage de: %s, %s, %s, %s en %s",c.name,"",c.lineno,c.colno,new_name)
+ s=jdc.getLines()[c.lineno-1]
+ jdc.getLines()[c.lineno-1]=s[:c.colno]+new_name+s[c.colno+len(command):]
+
+ jdc.reset(jdc.getSource())
+
+