# -*- coding: iso-8859-1 -*- # Copyright 20003 - 2008: Julien Bourdaillet (julien.bourdaillet@lip6.fr), Jean-Gabriel Ganascia (jean-gabriel.ganascia@lip6.fr) # This file is part of MEDITE. # # MEDITE 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. # # MEDITE 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. # # You should have received a copy of the GNU General Public License # along with Foobar; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #---------------------------------------------------------------------------- # Nom: cInformations.py # Objet: Classe qui interface entre les fichiers xml et l'interface graphique #---------------------------------------------------------------------------- #from xml.dom.pulldom import * #from xml.dom.minidom import Document import os import posixpath import traceback import logging,gc,sys import xml.dom.minidom #import xml.dom.pulldom import Ft.Xml, Ft.Xml.XPath from Ft.Xml import MarkupWriter from xml.sax import make_parser, SAXException from xml.sax.handler import ContentHandler from string import * import numpy.oldnumeric as Numeric from Utile.constantesDonnees import * from Utile.exceptionsDonnees import * from Donnees.resultatAppli import * from Utile.codageXml import * from Donnees.arbre import * import Utile.decodage from Donnees.planTravail import * import Controleurs.DGManager import Controleurs.FInfoManager import Donnees.resultatAppli class CInformations(object) : def __init__(self, stream=False): """ stream ecrit un nouveau doc en stream, il ecrase le fichier existant mais permet d'ecrire des gros doc xml""" self.dgm = Controleurs.DGManager.DGManager() self.stream = stream # Methode qui enregistre les informations d'un couple d'états def existeInformations__(self, planTravail): """Methode qui renvoie vrai si des informations existent sur un couple de etats pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ if planTravail.getParam().isNull(): return False nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) fic = lireDocXml(fim.getFileInfo()) pParametres = planTravail.getParam() liste = fic.getElementsByTagName(B_INFORMATIONS) for node in liste : if (node.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and node.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and node.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and node.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible() and node.getAttribute(B_PARAM_1) == '%s'%pParametres.getp1() and node.getAttribute(B_PARAM_2) == '%s'%pParametres.getp2() and node.getAttribute(B_PARAM_3) == '%s'%pParametres.getp3() and node.getAttribute(B_PARAM_4) == '%s'%pParametres.getp4() and node.getAttribute(B_PARAM_5) == '%s'%pParametres.getp5() and node.getAttribute(B_PARAM_6) == '%s'%pParametres.getp6() and node.getAttribute(B_PARAM_7) == '%s'%pParametres.getp7()): return True return False # Methode qui enregistre les informations d'un couple d'états def existeInformations(self, planTravail): """Methode qui renvoie vrai si des informations existent sur un couple de etats pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ if planTravail.getParam().isNull(): return False nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) pParametres = planTravail.getParam() f = open(fim.getFileInfo()) text = f.read() f.close() # on coupe après arbre pour ne garder que les infos de l'arbre des versions # on cherhce les balises deb = text.find('') fin = text.find('') while deb != -1: # on ajoute '' xml_informations = Ft.Xml.Parse(chaine_informations) #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+')',contextNode = xml_informations) if len(nodeListe) != 0: node = nodeListe[0] if (node.getAttributeNS(node.namespaceURI, B_VERS_SOURCE)== planTravail.getVersionSource() and node.getAttributeNS(node.namespaceURI, B_ETAT_SOURCE)== planTravail.getEtatSource() and node.getAttributeNS(node.namespaceURI, B_VERS_CIBLE) == planTravail.getVersionCible() and node.getAttributeNS(node.namespaceURI, B_ETAT_CIBLE) == planTravail.getEtatCible() and node.getAttributeNS(node.namespaceURI, B_PARAM_1) == '%s'%pParametres.getp1() and node.getAttributeNS(node.namespaceURI, B_PARAM_2) == '%s'%pParametres.getp2() and node.getAttributeNS(node.namespaceURI, B_PARAM_3) == '%s'%pParametres.getp3() and node.getAttributeNS(node.namespaceURI, B_PARAM_4) == '%s'%pParametres.getp4() and node.getAttributeNS(node.namespaceURI, B_PARAM_5) == '%s'%pParametres.getp5() and node.getAttributeNS(node.namespaceURI, B_PARAM_6) == '%s'%pParametres.getp6() and node.getAttributeNS(node.namespaceURI, B_PARAM_7) == '%s'%pParametres.getp7()): return True deb = text.find('',fin+15) fin = text.find('',fin+15) return False # Methode qui enregistre les transformations d'un couple d'états def enregistrerTransformations__(self, planTravail,pTransformations): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ isinstance(pTransformations, Donnees.transformations.Transformations)""" nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) if self.stream: self.enregistrerTransformationsStream(planTravail, pTransformations) elif not self.existeInformations(planTravail): xmlInfo = self.__creerInformations(planTravail, pTransformations) fic = lireDocXml(fim.getFileInfo()) nodeRoot = fic._get_firstChild() nodeRoot.appendChild(xmlInfo) ecrireDocXml(fim.getFileInfo(),fic) # Methode qui enregistre les transformations d'un couple d'états def enregistrerTransformations(self, planTravail,pTransformations): """ Avec Ft.Xml pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ isinstance(pTransformations, Donnees.transformations.Transformations)""" nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) if self.stream: self.enregistrerTransformationsStream(planTravail, pTransformations) #elif not self.existeInformations(planTravail): else : xmlInfo = self.__creerInformations(planTravail, pTransformations) fic = lireDocXmlFt(fim.getFileInfo()) nodeRoot = fic.firstChild nodeRoot.appendChild(xmlInfo) ecrireDocXmlFt(fim.getFileInfo(),fic) # Methode qui enregistre le commentaire d'un couple d'états def enregistrerCommentaire(self, planTravail, pCommentaire): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ isinstance(pCommentaire, str)""" nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) commentaire = Commentaire(encodingNode(pCommentaire)) fic = lireDocXml(fim.getFileInfo()) liste = fic.getElementsByTagName(B_INFORMATIONS) infos = self.__rechercherInfos(liste, planTravail) lc = infos.getElementsByTagName(B_COMMENTAIRE) if lc == [] : ## creer et ajouter le noeud 'commentaire' comment = self.__creerCommentaire(commentaire) infos.appendChild(comment) ecrireDocXml(fim.getFileInfo(),fic) else : # il existe deja un commentaire, une exception est lancee c = (lc[0]._get_firstChild()).toxml() comment = Commentaire(c) raise CommentaireDejaExistant('le commentaire de"'+planTravail.getVersionSource() +'/'+planTravail.getEtatSource()+ '" et "'+planTravail.getVersionCible()+'/'+planTravail.getEtatCible()+'" est deja existant', comment) # Methode qui remplace le commentaire existant par le nouveau def remplacerCommentaire(self, planTravail, pCommentaire): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail)and \ isinstance(pCommentaire, str)""" nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) commentaire = Commentaire(pCommentaire) fic = lireDocXml(fim.getFileInfo()) liste = fic.getElementsByTagName(B_INFORMATIONS) infos = self.__rechercherInfos(liste, planTravail) lc = infos.getElementsByTagName(B_COMMENTAIRE) comment = self.__creerCommentaire(commentaire) if lc != [] : infos.removeChild(lc[0]) infos.appendChild(comment) ecrireDocXml(fim.getFileInfo(),fic) # Methode qui ajoute le nouveau commentaire a l'ancien def ajouterCommentaire(self, planTravail, pCommentaire): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ isinstance(pCommentaire, str)""" nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) commentaire = Commentaire(pCommentaire) fic = lireDocXml(fim.getFileInfo()) liste = fic.getElementsByTagName(B_INFORMATIONS) infos = self.__rechercherInfos(liste, planTravail) lc = infos.getElementsByTagName(B_COMMENTAIRE) if lc == []:## creer et ajouter le noeud 'commentaire' comment = self.__creerCommentaire(commentaire) infos.appendChild(comment) else : xmldoc=Document() comment =' '+ commentaire.getCommentaire() lc[0].appendChild(xmldoc.createTextNode(comment)) ecrireDocXml(fim.getFileInfo(), fic) # Methode qui supprime le commentaire d'un couple d'états def supprimerCommentaire(self, planTravail): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) fic = lireDocXml(fim.getFileInfo()) liste = fic.getElementsByTagName(B_INFORMATIONS) infos = self.__rechercherInfos(liste, planTravail) lc = infos.getElementsByTagName(B_COMMENTAIRE) if lc != [] : infos.removeChild(lc[0]) ecrireDocXml(fim.getFileInfo(), fic) else : raise CommentaireNonTrouve('les informations de "'+planTravail.getVersionSource() +'/'+planTravail.getEtatSource()+ '" et "'+planTravail.getVersionCible()+'/'+planTravail.getEtatCible()+'" n\'ont pas de commentaire') # Methode qui supprime les informations d'un couple d'états def supprimerInformations(self, planTravail): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) fic = lireDocXml(fim.getFileInfo()) noderoot = fic.getElementsByTagName(B_ROOT) liste = fic.getElementsByTagName(B_INFORMATIONS) infos = self.__rechercherInfos(liste, planTravail) if infos != None:## suppression du noeud noderoot[0].removeChild(infos) ecrireDocXml(fim.getFileInfo(),fic) def enregistrerInformations(self, planTravail,pTransformations, pCommentaire): """ Methode qui enregistre les informations (transformations + commentaire) d'un couple de etats pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ if not self.existeInformations(planTravail ): self.enregistrerTransformations(planTravail, pTransformations) self.enregistrerCommentaire(planTravail, pCommentaire) # Methode qui retourne toutes les informations d'un couple d'états # Methode qui renvoie toutes les informations existantes pour un dossier genetique def rechercherToutesInformationsDG__(self, pNom, pTitre): nomDG = self.dgm.getDG(pNom, pTitre) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), Controleurs.DGManager.Oeuvre(pTitre)) fic = lireDocXml(fim.getFileInfo()) liste = fic.getElementsByTagName(B_INFORMATIONS) listeInfos = [] for node in liste : vSource = node.getAttribute(B_VERS_SOURCE) fSource = node.getAttribute(B_ETAT_SOURCE) vCible = node.getAttribute(B_VERS_CIBLE) fCible = node.getAttribute(B_ETAT_CIBLE) par1 = atoi(node.getAttribute(B_PARAM_1)) par2 = atoi(node.getAttribute(B_PARAM_2)) par3 = atoi(node.getAttribute(B_PARAM_3)) par4 = atoi(node.getAttribute(B_PARAM_4)) par5 = atoi(node.getAttribute(B_PARAM_5)) par6 = atoi(node.getAttribute(B_PARAM_6)) par7 = atoi(node.getAttribute(B_PARAM_7)) listeInfos.append([vSource,fSource,vCible,fCible,par1,par2,par3,par4,par5,par6,par7]) return listeInfos def rechercherToutesInformationsDG(self, pNom, pTitre): nomDG = self.dgm.getDG(pNom, pTitre) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), Controleurs.DGManager.Oeuvre(pTitre)) f = open(fim.getFileInfo()) text = f.read() f.close() listeInfos = [] # on coupe après arbre pour ne garder que les infos de l'arbre des versions # on cherhce les balises deb = text.find('') fin = text.find('') while deb != -1: # on ajoute '' xml_informations = Ft.Xml.Parse(chaine_informations) #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+')',contextNode = xml_informations) if len(nodeListe) != 0: node = nodeListe[0] vSource = node.getAttributeNS(node.namespaceURI, B_VERS_SOURCE) fSource = node.getAttributeNS(node.namespaceURI, B_ETAT_SOURCE) vCible = node.getAttributeNS(node.namespaceURI, B_VERS_CIBLE) fCible = node.getAttributeNS(node.namespaceURI, B_ETAT_CIBLE) par1 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_1)) par2 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_2)) par3 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_3)) par4 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_4)) par5 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_5)) par6 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_6)) par7 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_7)) listeInfos.append([vSource,fSource,vCible,fCible,par1,par2,par3,par4,par5,par6,par7]) deb = text.find('',fin+15) fin = text.find('',fin+15) return listeInfos # Methode qui retourne toutes les informations # de tous les couples enregistrés d'états d'un DG # renvoie une liste d'objets Information def rechercherLInformationsDG(self, pNom, pTitre): nomDG = self.dgm.getDG(pNom, pTitre) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), Controleurs.DGManager.Oeuvre(pTitre)) fic = lireDocXml(fim.getFileInfo()) liste = fic.getElementsByTagName(B_INFORMATIONS) listeInfos = [] for infos in liste : tmpInfo = self.__getInformations(infos, pNom, pTitre) listeInfos.append(tmpInfo) return listeInfos def generateLInformationsDG__(self, pNom, pTitre): """ Générateur des informations de comparaisons Le générateur evite de charger plusieurs informations en même temps lorsque celes ci son grosses (plusieurs Mo) On evite de faire un SAX en recherchant dans le fichier XML les balises une par une et a la main, c'est pas propre mais efficace """ nomDG = self.dgm.getDG(pNom, pTitre) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), Controleurs.DGManager.Oeuvre(pTitre)) f = open(fim.getFileInfo()) text = f.read() f.close() # on coupe après arbre pour ne garder que les infos de l'arbre des versions deb = text.find('') while deb != -1: # on ajoute '' # +15 pour la longueur de la balise #logging.debug(chaine_informations) xml_informations = xml.dom.minidom.parseString(chaine_informations) liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement objet_informations = self.__getInformations(liste[0], pNom, pTitre) #objet_informations = self.__getInformations(xml_informations.getElementsByTagName(B_INFORMATIONS)[0], pNom, pTitre) #for child in liste[0].childNodes: # logging.debug(child) # child.unlink() liste = None xml_informations.unlink() ; xml_informations = None #unreachable = gc.collect() #logging.debug( 'unreachable='+str(unreachable)) yield objet_informations deb = text.find('',fin+15) def generateLInformationsDG(self, pNom, pTitre): """ Avec Ft.Xml.Domlette Générateur des informations de comparaisons Le générateur evite de charger plusieurs informations en même temps lorsque celes ci son grosses (plusieurs Mo) On evite de faire un SAX en recherchant dans le fichier XML les balises une par une et a la main, c'est pas propre mais efficace """ nomDG = self.dgm.getDG(pNom, pTitre) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), Controleurs.DGManager.Oeuvre(pTitre)) f = open(fim.getFileInfo()) text = f.read() f.close() # on coupe après arbre pour ne garder que les infos de l'arbre des versions deb = text.find('') while deb != -1: # on ajoute '' # +15 pour la longueur de la balise #logging.debug(chaine_informations) #xml_informations = xml.dom.minidom.parseString(chaine_informations) xml_informations = Ft.Xml.Parse(chaine_informations) #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+')',contextNode = xml_informations) if len(nodeListe) != 0: objet_informations = self.__getInformations(nodeListe[0], pNom, pTitre) nodeListe = None ; xml_informations = None #.unlink() logging.debug('fin rechercherInformations') yield objet_informations deb = text.find('',fin+15) def rechercherToutesInformations__(self, planTravail): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) fic = lireDocXml(fim.getFileInfo()) liste = fic.getElementsByTagName(B_INFORMATIONS) listeInfos = [] for infos in liste : if (infos.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and infos.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and infos.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and infos.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible()): tmpInfo = self.__getInformations(infos, planTravail.getAuteur(), planTravail.getOeuvre()) listeInfos.append(tmpInfo) return listeInfos # Methode qui retourne toutes les informations d'un couple d'états def rechercherToutesInformations(self, planTravail): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) listeInfos = [] f = open(fim.getFileInfo()) text = f.read() f.close() # on coupe après arbre pour ne garder que les infos de l'arbre des versions deb = text.find('') while deb != -1: # on ajoute '' # +15 pour la longueur de la balise #logging.debug(chaine_informations) #xml_informations = xml.dom.minidom.parseString(chaine_informations) xml_informations = Ft.Xml.Parse(chaine_informations) #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+')',contextNode = xml_informations) if len(nodeListe) != 0: node = nodeListe[0] if (node.getAttributeNS(node.namespaceURI, B_VERS_SOURCE) == planTravail.getVersionSource() and node.getAttributeNS(node.namespaceURI, B_ETAT_SOURCE) == planTravail.getEtatSource() and node.getAttributeNS(node.namespaceURI, B_VERS_CIBLE) == planTravail.getVersionCible() and node.getAttributeNS(node.namespaceURI, B_ETAT_CIBLE) == planTravail.getEtatCible()): listeInfos.append(self.__getInformations(node, planTravail.getAuteur(), planTravail.getOeuvre())) deb = text.find('',fin+15) return listeInfos # Methode qui retourne les informations d'un couple d'états et des parametres precis def __rechercherInformations(self, planTravail): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) pParametres = planTravail.getParam() fic = lireDocXml(fim.getFileInfo()) liste = fic.getElementsByTagName(B_INFORMATIONS) for node in liste : if (node.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and node.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and node.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and node.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible() and node.getAttribute(B_PARAM_1) == '%s'%pParametres.getp1() and node.getAttribute(B_PARAM_2) == '%s'%pParametres.getp2() and node.getAttribute(B_PARAM_3) == '%s'%pParametres.getp3() and node.getAttribute(B_PARAM_4) == '%s'%pParametres.getp4() and node.getAttribute(B_PARAM_5) == '%s'%pParametres.getp5() and node.getAttribute(B_PARAM_6) == '%s'%pParametres.getp6() and node.getAttribute(B_PARAM_7) == '%s'%pParametres.getp7()): return self.__getInformations(node,planTravail.getAuteur(),planTravail.getOeuvre()) return None def rechercherInformations__(self, planTravail): """ Recherche une information de comparaisons On recherche la chaine informations dans le fichier xml sans le parser en DOM ce qui est très lours quand le fichier est gros, une fois trouvé, on extrait la chaine conteant la bonne balise informations et on la parse effectivement en DOM pas propre mais beaucoup plus rapide que la méthode __rechercherInformations quand les fichiers sont gros """ logging.debug('begin rechercherInformations') fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) pParametres = planTravail.getParam() f = open(fim.getFileInfo()) text = f.read() f.close() # on ccherhce les balises deb = text.find('') fin = text.find('') while deb != -1: # on ajoute '' #logging.debug(chaine_informations) xml_informations = xml.dom.minidom.parseString(chaine_informations) liste = xml_informations.getElementsByTagName(B_INFORMATIONS) node = liste[0] # liste avec 1 seul element nécessairement if (node.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and node.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and node.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and node.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible() and node.getAttribute(B_PARAM_1) == '%s'%pParametres.getp1() and node.getAttribute(B_PARAM_2) == '%s'%pParametres.getp2() and node.getAttribute(B_PARAM_3) == '%s'%pParametres.getp3() and node.getAttribute(B_PARAM_4) == '%s'%pParametres.getp4() and node.getAttribute(B_PARAM_5) == '%s'%pParametres.getp5() and node.getAttribute(B_PARAM_6) == '%s'%pParametres.getp6() and node.getAttribute(B_PARAM_7) == '%s'%pParametres.getp7()): # on a le bon noeud informations, on récupère le noeud et tous ses fils chaine_informations = '' + \ text[deb:fin+15] +'' xml_informations = xml.dom.minidom.parseString(chaine_informations) liste = xml_informations.getElementsByTagName(B_INFORMATIONS) objet_informations = self.__getInformations(liste[0], planTravail.getAuteur(),planTravail.getOeuvre()) liste = None ; xml_informations.unlink() logging.debug('fin rechercherInformations') return objet_informations deb = text.find('',fin+15) fin = text.find('',fin+15) return None def rechercherInformations(self, planTravail): """ Recherche avec Ft.Xml.Domlette Recherche une information de comparaisons On recherche la chaine informations dans le fichier xml sans le parser en DOM ce qui est très lours quand le fichier est gros, une fois trouvé, on extrait la chaine conteant la bonne balise informations et on la parse effectivement en DOM pas propre mais beaucoup plus rapide que la méthode __rechercherInformations quand les fichiers sont gros """ logging.debug('begin rechercherInformations') fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) pParametres = planTravail.getParam() f = open(fim.getFileInfo()) text = f.read() f.close() # on cherhce les balises deb = text.find('') fin = text.find('') while deb != -1: # on ajoute '' #logging.debug(chaine_informations) #xml_informations = xml.dom.minidom.parseString(chaine_informations) xml_informations = Ft.Xml.Parse(chaine_informations) #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) #node = liste[0] # liste avec 1 seul element nécessairement nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+ '[@'+B_VERS_SOURCE+'="'+planTravail.getVersionSource()+'"]' + '[@'+B_ETAT_SOURCE+'="'+planTravail.getEtatSource()+'"]' + '[@'+B_VERS_CIBLE+'="'+planTravail.getVersionCible()+'"]' + '[@'+B_ETAT_CIBLE+'="'+planTravail.getEtatCible()+'"]' + '[@'+B_PARAM_1+'="'+str(pParametres.getp1())+'"]' + '[@'+B_PARAM_2+'="'+str(pParametres.getp2())+'"]' + '[@'+B_PARAM_3+'="'+str(pParametres.getp3())+'"]' + '[@'+B_PARAM_4+'="'+str(pParametres.getp4())+'"]' + '[@'+B_PARAM_5+'="'+str(pParametres.getp5())+'"]' + '[@'+B_PARAM_6+'="'+str(pParametres.getp6())+'"]'+ '[@'+B_PARAM_7+'="'+str(pParametres.getp7())+'"])' ,contextNode = xml_informations) if len(nodeListe) != 0: # on a le bon noeud informations, on récupère le noeud et tous ses fils chaine_informations = '' + \ text[deb:fin+15] +'' #xml_informations = xml.dom.minidom.parseString(chaine_informations) xml_informations = Ft.Xml.Parse(chaine_informations) #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) liste = Ft.Xml.XPath.Evaluate(u'(//'+B_INFORMATIONS+')',contextNode = xml_informations) objet_informations = self.__getInformations(liste[0], planTravail.getAuteur(),planTravail.getOeuvre()) liste = None ; xml_informations = None #.unlink() logging.debug('fin rechercherInformations') return objet_informations deb = text.find('',fin+15) fin = text.find('',fin+15) return None def __rechercherInfos(self, pListeBalise, planTravail): """ Methode privee qui renvoie la balise "informations" recherchee pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ isinstance(pListeBalise, list)""" pParametres = planTravail.getParam() for node in pListeBalise : if (node.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and node.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and node.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and node.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible() and node.getAttribute(B_PARAM_1) == '%s'%pParametres.getp1() and node.getAttribute(B_PARAM_2) == '%s'%pParametres.getp2() and node.getAttribute(B_PARAM_3) == '%s'%pParametres.getp3() and node.getAttribute(B_PARAM_4) == '%s'%pParametres.getp4() and node.getAttribute(B_PARAM_5) == '%s'%pParametres.getp5() and node.getAttribute(B_PARAM_6) == '%s'%pParametres.getp6() and node.getAttribute(B_PARAM_7) == '%s'%pParametres.getp7()): return node return None # raise InformationsNonTrouvees('les informations de "'+pVersionSource+ # '/'+pEtatSource+'" et "'+pVersionCible+ # '/'+pEtatCible+'" n\'existent pas') # Methode privee qui renvoie vrai si l'index passe en parametre est l'un # des premiers indices de la liste de couple passee en parametre def __appartientListe(self, pIndex, pListe): for i in pListe : if pIndex == i[0]: return 1 ## VRAI return 0 ## FALSE # Methode privee pour rechercher dans une liste de couples d'indices # le couple dont le 1er indice est passe en parametre def __getCpleIndices(self, pListe, indice): for i in pListe : if i[0] == indice : return i return None # Methode privee pour creer les sous balises de deplacement dans la balise # passee en parametre def __sousBalises(self, pBalise, pTextNorm, pBlocText, pLDepl): textNetBloc = pTextNorm+pBlocText lg = len(pTextNorm) xmldoc = Document() for i in range(len(pTextNorm), len(textNetBloc)): cple = self.__getCpleIndices(pLDepl, i) if cple != None: m1 = encodingNode(textNetBloc[lg : cple[0] ]) mot1 = xmldoc.createTextNode(m1) pBalise.appendChild(mot1) m2 = encodingNode(textNetBloc[cple[0] : cple[1] ]) mot2 = xmldoc.createTextNode(m2) baliseDepl = xmldoc.createElement(B_TEXT_DEPL) baliseDepl.appendChild(mot2) pBalise.appendChild(baliseDepl) i = len(textNetBloc[lg: cple[1] ]) lg += i m3 = encodingNode(textNetBloc[lg :]) mot3 = xmldoc.createTextNode(m3) pBalise.appendChild(mot3) ## Fonction privee pour recuperer les textes sans les codes xml def __recupMot(self, pMot): mot = pMot.replace('"', '"')\ .replace('&', '&')\ .replace('<', '<')\ .replace('>', '>') return mot # Methode privee pour retrouver les caracteres deplaces d'une balise def __recupereListe(self, pBalise, pListe, pListeDepl, pTextes, pBlocs): lg1 = len(pTextes) motListe= '' noeud = pBalise._get_firstChild() while noeud != None : if noeud.hasChildNodes() and noeud.tagName == B_TEXT_DEPL : nFils = noeud._get_firstChild() mot = '' while nFils != None : mot += self.__recupMot(nFils.toxml()) noeud.removeChild(nFils) nFils = noeud._get_firstChild() lg2 = len(pTextes) pTextes += mot motListe += mot pListeDepl.append( [lg2,len(pTextes)] ) else : mot = self.__recupMot(noeud.toxml()) pTextes += mot motListe += mot pBalise.removeChild(noeud) noeud = pBalise._get_firstChild() pListe.append( [lg1, len(pTextes)] ) pBlocs.append(pTextes[lg1:]) return pTextes[lg1:] # Methode privee qui creer le noeud a inserer dans le fichier xml def __creerInformations_(self, planTravail, pResultat): """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ isinstance(pResultat, Donnees.resultatAppli.Resultat) post: isinstance(__return__, xml.dom.minidom.Node) """ pParametres = planTravail.getParam() xmldoc = Document() # Balise Information xmltagInfo = xmldoc.createElement(B_INFORMATIONS) xmltagInfo.setAttribute(B_VERS_SOURCE,planTravail.getVersionSource() ) xmltagInfo.setAttribute(B_ETAT_SOURCE,planTravail.getEtatSource()) xmltagInfo.setAttribute(B_VERS_CIBLE,planTravail.getVersionCible()) xmltagInfo.setAttribute(B_ETAT_CIBLE,planTravail.getEtatCible()) xmltagInfo.setAttribute(B_PARAM_1,'%s'%pParametres.getp1()) xmltagInfo.setAttribute(B_PARAM_2,'%s'%pParametres.getp2()) xmltagInfo.setAttribute(B_PARAM_3,'%s'%pParametres.getp3()) xmltagInfo.setAttribute(B_PARAM_4,'%s'%pParametres.getp4()) xmltagInfo.setAttribute(B_PARAM_5,'%s'%pParametres.getp5()) xmltagInfo.setAttribute(B_PARAM_6,'%s'%pParametres.getp6()) xmltagInfo.setAttribute(B_PARAM_7,'%s'%pParametres.getp7()) # Balise Transformations xmlTransfo = xmldoc.createElement(B_TRANSFORMATIONS) xmlLgsource = xmldoc.createElement(B_LGSOURCE) xmlLgsource.setAttribute(B_LG, str(pResultat.getLgSource())) xmlTransfo.appendChild(xmlLgsource) # Insertions xmlInsert = xmldoc.createElement(B_INSERTIONS) listeInsert = pResultat.getListeInsertions() for deb,fin in listeInsert : node = xmldoc.createElement(B_INS) node.setAttribute(B_DEB, str(deb)) node.setAttribute(B_FIN, str(fin)) xmlInsert.appendChild(node) xmlTransfo.appendChild(xmlInsert) # Suppressions xmlSupp = xmldoc.createElement(B_SUPPRESSIONS) listeSupp = pResultat.getListeSuppressions() for deb,fin in listeSupp : node = xmldoc.createElement(B_SUP) node.setAttribute(B_DEB, str(deb)) node.setAttribute(B_FIN, str(fin)) xmlSupp.appendChild(node) xmlTransfo.appendChild(xmlSupp) # Deplacements xmlDepl = xmldoc.createElement(B_DEPLACEMENTS) listeDepl = pResultat.getListeDeplacements() for deb,fin in listeDepl : node = xmldoc.createElement(B_DEP) node.setAttribute(B_DEB, str(deb)) node.setAttribute(B_FIN, str(fin)) xmlDepl.appendChild(node) xmlTransfo.appendChild(xmlDepl) # Remplacement xmlRemp = xmldoc.createElement(B_REMPLACEMENTS) listeRemp = pResultat.getListeRemplacements() for deb,fin in listeRemp : node = xmldoc.createElement(B_REMP) node.setAttribute(B_DEB, str(deb)) node.setAttribute(B_FIN, str(fin)) xmlRemp.appendChild(node) xmlTransfo.appendChild(xmlRemp) #blocs communs xmlBlocsCom = xmldoc.createElement(B_BLOCSCOMMUNS) listeBlocsCommuns = pResultat.getBlocsCommuns() for deb,fin in listeBlocsCommuns : node = xmldoc.createElement(B_BC) node.setAttribute(B_DEB, str(deb)) node.setAttribute(B_FIN, str(fin)) xmlBlocsCom.appendChild(node) xmlTransfo.appendChild(xmlBlocsCom) # blocs déplacés xmlBlocsDep = xmldoc.createElement(B_BLOCSDEPLACES) listeBlocsDep = pResultat.getPairesBlocsDeplaces() for (b1deb,b1fin),(b2deb,b2fin) in listeBlocsDep : node = xmldoc.createElement(B_DEP) node.setAttribute(B1_D, str(b1deb)) node.setAttribute(B1_F, str(b1fin)) node.setAttribute(B2_D, str(b2deb)) node.setAttribute(B2_F, str(b2fin)) xmlBlocsDep.appendChild(node) xmlTransfo.appendChild(xmlBlocsDep) # Insertion de la balise B_TRANSFORMATIONS dans la balise B_INFORMATIONS xmltagInfo.appendChild(xmlTransfo) return xmltagInfo # Methode privee qui creer le noeud a inserer dans le fichier xml def __creerInformations(self, planTravail, pResultat): """ Avec Ft.Xml pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ isinstance(pResultat, Donnees.resultatAppli.Resultat) post: isinstance(__return__, xml.dom.minidom.Node) """ pParametres = planTravail.getParam() #xmldoc = Ft.Xml.Document() xmldoc = doc = Ft.Xml.Domlette.implementation.createDocument(Ft.Xml.EMPTY_NAMESPACE, None, None) # Balise Information xmltagInfo = xmldoc.createElementNS(xmldoc.namespaceURI, B_INFORMATIONS) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_VERS_SOURCE,planTravail.getVersionSource() ) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_ETAT_SOURCE,planTravail.getEtatSource()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_VERS_CIBLE,planTravail.getVersionCible()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_ETAT_CIBLE,planTravail.getEtatCible()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_1,'%s'%pParametres.getp1()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_2,'%s'%pParametres.getp2()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_3,'%s'%pParametres.getp3()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_4,'%s'%pParametres.getp4()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_5,'%s'%pParametres.getp5()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_6,'%s'%pParametres.getp6()) xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_7,'%s'%pParametres.getp7()) # Balise Transformations xmlTransfo = xmldoc.createElementNS(xmldoc.namespaceURI, B_TRANSFORMATIONS) xmlLgsource = xmldoc.createElementNS(xmldoc.namespaceURI, B_LGSOURCE) xmlLgsource.setAttributeNS(xmlLgsource.namespaceURI, B_LG, str(pResultat.getLgSource())) xmlTransfo.appendChild(xmlLgsource) # Insertions xmlInsert = xmldoc.createElementNS(xmldoc.namespaceURI, B_INSERTIONS) listeInsert = pResultat.getListeInsertions() for deb,fin in listeInsert : node = xmldoc.createElementNS(xmldoc.namespaceURI, B_INS) node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) node.setAttributeNS(xmldoc.namespaceURI, B_FIN, str(fin)) xmlInsert.appendChild(node) xmlTransfo.appendChild(xmlInsert) # Suppressions xmlSupp = xmldoc.createElementNS(xmldoc.namespaceURI, B_SUPPRESSIONS) listeSupp = pResultat.getListeSuppressions() for deb,fin in listeSupp : node = xmldoc.createElementNS(xmldoc.namespaceURI, B_SUP) node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) node.setAttributeNS(node.namespaceURI, B_FIN, str(fin)) xmlSupp.appendChild(node) xmlTransfo.appendChild(xmlSupp) # Deplacements xmlDepl = xmldoc.createElementNS(xmldoc.namespaceURI, B_DEPLACEMENTS) listeDepl = pResultat.getListeDeplacements() for deb,fin in listeDepl : node = xmldoc.createElementNS(xmldoc.namespaceURI, B_DEP) node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) node.setAttributeNS(node.namespaceURI, B_FIN, str(fin)) xmlDepl.appendChild(node) xmlTransfo.appendChild(xmlDepl) # Remplacement xmlRemp = xmldoc.createElementNS(xmldoc.namespaceURI, B_REMPLACEMENTS) listeRemp = pResultat.getListeRemplacements() for deb,fin in listeRemp : node = xmldoc.createElementNS(xmldoc.namespaceURI, B_REMP) node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) node.setAttributeNS(node.namespaceURI, B_FIN, str(fin)) xmlRemp.appendChild(node) xmlTransfo.appendChild(xmlRemp) #blocs communs xmlBlocsCom = xmldoc.createElementNS(xmldoc.namespaceURI, B_BLOCSCOMMUNS) listeBlocsCommuns = pResultat.getBlocsCommuns() for deb,fin in listeBlocsCommuns : node = xmldoc.createElementNS(xmldoc.namespaceURI, B_BC) node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) node.setAttributeNS(node.namespaceURI, B_FIN, str(fin)) xmlBlocsCom.appendChild(node) xmlTransfo.appendChild(xmlBlocsCom) # blocs déplacés xmlBlocsDep = xmldoc.createElementNS(xmldoc.namespaceURI, B_BLOCSDEPLACES) listeBlocsDep = pResultat.getPairesBlocsDeplaces() for (b1deb,b1fin),(b2deb,b2fin) in listeBlocsDep : node = xmldoc.createElementNS(xmldoc.namespaceURI, B_DEP) node.setAttributeNS(node.namespaceURI, B1_D, str(b1deb)) node.setAttributeNS(node.namespaceURI, B1_F, str(b1fin)) node.setAttributeNS(node.namespaceURI, B2_D, str(b2deb)) node.setAttributeNS(node.namespaceURI, B2_F, str(b2fin)) xmlBlocsDep.appendChild(node) xmlTransfo.appendChild(xmlBlocsDep) # Insertion de la balise B_TRANSFORMATIONS dans la balise B_INFORMATIONS xmltagInfo.appendChild(xmlTransfo) return xmltagInfo def __creerCommentaire(self, pCommentaire): """ Methode privee qui creer le noeud a inserer dans le fichier xml pre: isinstance(pCommentaire,Donnees.commentaire.Commentaire) post: isinstance(__return__, xml.dom.minidom.Node) """ comment = pCommentaire.getCommentaire() xmldoc = Document() xmltagComment = xmldoc.createElement(B_COMMENTAIRE) xmltagComment.appendChild(xmldoc.createTextNode(comment)) return xmltagComment def __getInformations(self,nodeInfos, pNom, pTitre): """ On recupere dans fic les informations du fichier Informations.xml dont le chemin est passe en parametre pre: isinstance(nodeInfos, xml.dom.minidom.Node) and \ isinstance(pNom,str) and isinstance(pTitre,str) """ logging.debug('begin __getInformations') pVersionSource = nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_VERS_SOURCE) pEtatSource = nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_ETAT_SOURCE) pVersionCible = nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_VERS_CIBLE) pEtatCible = nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_ETAT_CIBLE) pParametres = Parametres(atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_1)), atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_2)), atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_3)),atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_4)), atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_5)),atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_6)), atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_7))) # on concatène les 2 fichiers pathSource=self.dgm.getEtatPath(pNom,pTitre,pVersionSource,pEtatSource) pathCible=self.dgm.getEtatPath(pNom,pTitre,pVersionCible,pEtatCible) fs = open(pathSource) fc = open(pathCible) textesConca = fs.read() + fc.read() fs.close() fc.close() ## creer un objet de type 'Transformations' #i = nodeInfos.getElementsByTagName(B_LGSOURCE) #lgsource=0 #if i != []: # lgsource = int(i[0].getAttribute(B_LG)) lgsource = int(nodeInfos.xpath(u'string('+B_TRANSFORMATIONS+'/'+B_LGSOURCE+'/@' +B_LG+')')) #print lgsource listInsert = [] #i = nodeInfos.getElementsByTagName(B_INSERTIONS) #if i != [] : # li = i[0].getElementsByTagName(B_INS) # for m in li : # listInsert.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) #nodeInsertions = nodeInfos.xpath(u'string(//'+B_INSERTIONS+')') n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_INSERTIONS+')')[0] #print n.localName if n.localName == B_INSERTIONS: #for child in Ft.Xml.XPath.Evaluate(u'('+B_INS+')',n): for child in n.xpath(u'('+B_INS+')'): listInsert.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) # print (int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN))) #sys.stdout.flush() listSupp = [] #s = nodeInfos.getElementsByTagName(B_SUPPRESSIONS) #if s != [] : # ls = s[0].getElementsByTagName(B_SUP) # for m in ls : # listSupp.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_SUPPRESSIONS+')')[0] if n.localName == B_SUPPRESSIONS: #for child in Ft.Xml.XPath.Evaluate(u'(/'+B_SUP+')',n): for child in n.xpath(u'('+B_SUP+')'): listSupp.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) listDepl = [] #d = nodeInfos.getElementsByTagName(B_DEPLACEMENTS) #if d != []: # ld = d[0].getElementsByTagName(B_DEP) # for m in ld : # listDepl.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_DEPLACEMENTS+')')[0] if n.localName == B_DEPLACEMENTS: #for child in Ft.Xml.XPath.Evaluate(u'(//'+B_DEP+')',contextNode=n): #for child in n.xpath(u'(/'+B_DEP+')'): for child in n.xpath(u'('+B_DEP+')'): #print child try: listDepl.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) except Exception,e: print e print child print child.localName print child.attributes sys.stdout.flush() sys.exit() listRemp = [] #r = nodeInfos.getElementsByTagName(B_REMPLACEMENTS) #if r != []: # lr = r[0].getElementsByTagName(B_REMP) # for m in lr : # listRemp.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_REMPLACEMENTS+')')[0] if n.localName == B_REMPLACEMENTS: #for child in Ft.Xml.XPath.Evaluate(u'(/'+B_REMP+')',n): for child in n.xpath(u'('+B_REMP+')'): listRemp.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) listBlocCom = [] #r = nodeInfos.getElementsByTagName(B_BLOCSCOMMUNS) #if r != []: # lr = r[0].getElementsByTagName(B_BC) # for m in lr : # listBlocCom.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_BLOCSCOMMUNS+')')[0] if n.localName == B_BLOCSCOMMUNS: #for child in Ft.Xml.XPath.Evaluate(u'(/'+B_BC+')',n): for child in n.xpath(u'('+B_BC+')'): listBlocCom.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) listBlocDep = [] #r = nodeInfos.getElementsByTagName(B_BLOCSDEPLACES) #if r != []: # lr = r[0].getElementsByTagName(B_DEP) # for m in lr : # listBlocDep.append(((int(m.getAttribute(B1_D)),int(m.getAttribute(B1_F))), # (int(m.getAttribute(B2_D)),int(m.getAttribute(B2_F))))) n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_BLOCSDEPLACES+')')[0] if n.localName == B_BLOCSDEPLACES: #for child in Ft.Xml.XPath.Evaluate(u'(/'+B_DEP+')',n): for child in n.xpath(u'('+B_DEP+')'): listBlocDep.append(((int(child.getAttributeNS(child.namespaceURI,B1_D)),int(child.getAttributeNS(child.namespaceURI,B1_F))), (int(child.getAttributeNS(child.namespaceURI,B2_D)),int(child.getAttributeNS(child.namespaceURI,B2_F))))) #transfo = Transformations(listInsert, listSupp, listDepl, listRemp, textSource, textCible) res = Resultat(listInsert, listSupp, listDepl, listRemp,lgsource,textesConca,listBlocCom,listBlocDep) ## creer un objet de type 'Commentaire' si la balise B_COMMENTAIRE existe #c = nodeInfos.getElementsByTagName(B_COMMENTAIRE) #if c != []: # comment = Commentaire((c[0]._get_firstChild()).toxml()) #else : # comment = Commentaire('') c = Ft.Xml.XPath.Evaluate(u'(//'+B_COMMENTAIRE+')', contextNode=nodeInfos) if len(c) != 0: comment = Commentaire(c[0].nodeValue) else: comment = Commentaire('') logging.debug('fin __getInformations') ## creer et retourner un objet du type 'Informations' return Informations(pVersionSource, pEtatSource, pVersionCible, pEtatCible,pParametres, res, comment) def __getInformations_(self,nodeInfos, pNom, pTitre): """ Avec des Numeric.array plutot que des listes On recupere dans fic les informations du fichier Informations.xml dont le chemin est passe en parametre pre: isinstance(nodeInfos, xml.dom.minidom.Node) and \ isinstance(pNom,str) and isinstance(pTitre,str) """ logging.debug('begin __getInformations') pVersionSource = nodeInfos.getAttribute(B_VERS_SOURCE) pEtatSource = nodeInfos.getAttribute(B_ETAT_SOURCE) pVersionCible = nodeInfos.getAttribute(B_VERS_CIBLE) pEtatCible = nodeInfos.getAttribute(B_ETAT_CIBLE) pParametres = Parametres(atoi(nodeInfos.getAttribute(B_PARAM_1)), atoi(nodeInfos.getAttribute(B_PARAM_2)), atoi(nodeInfos.getAttribute(B_PARAM_3)),atoi(nodeInfos.getAttribute(B_PARAM_4)), atoi(nodeInfos.getAttribute(B_PARAM_5)),atoi(nodeInfos.getAttribute(B_PARAM_6)), atoi(nodeInfos.getAttribute(B_PARAM_7))) # on concatène les 2 fichiers pathSource=self.dgm.getEtatPath(pNom,pTitre,pVersionSource,pEtatSource) pathCible=self.dgm.getEtatPath(pNom,pTitre,pVersionCible,pEtatCible) fs = open(pathSource) fc = open(pathCible) textesConca = fs.read() + fc.read() fs.close() fc.close() ## creer un objet de type 'Transformations' i = nodeInfos.getElementsByTagName(B_LGSOURCE) lgsource=0 if i != []: lgsource = int(i[0].getAttribute(B_LG)) #listInsert = [] i = nodeInfos.getElementsByTagName(B_INSERTIONS) if i != [] : li = i[0].getElementsByTagName(B_INS) nb = len(li) ; i = 0 tabIns = Numeric.zeros((nb,2), Numeric.Int16) for m in li : #listInsert.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) tabIns[i][0] = int(m.getAttribute(B_DEB)) tabIns[i][1] = int(m.getAttribute(B_FIN)) i += 1 #listSupp = [] s = nodeInfos.getElementsByTagName(B_SUPPRESSIONS) if s != [] : ls = s[0].getElementsByTagName(B_SUP) nb = len(ls) ; i = 0 tabSup = Numeric.zeros((nb,2), Numeric.Int16) for m in ls : #listSupp.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) tabSup[i][0] = int(m.getAttribute(B_DEB)) tabSup[i][1] = int(m.getAttribute(B_FIN)) i += 1 #listDepl = [] d = nodeInfos.getElementsByTagName(B_DEPLACEMENTS) if d != []: ld = d[0].getElementsByTagName(B_DEP) nb = len(ld) ; i = 0 tabDep = Numeric.zeros((nb,2), Numeric.Int16) for m in ld : #listDepl.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) tabDep[i][0] = int(m.getAttribute(B_DEB)) tabDep[i][1] = int(m.getAttribute(B_FIN)) i += 1 #listRemp = [] r = nodeInfos.getElementsByTagName(B_REMPLACEMENTS) if r != []: lr = r[0].getElementsByTagName(B_REMP) nb = len(lr) ; i = 0 tabRemp = Numeric.zeros((nb,2), Numeric.Int16) for m in lr : #listRemp.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) tabRemp[i][0] = int(m.getAttribute(B_DEB)) tabRemp[i][1] = int(m.getAttribute(B_FIN)) i += 1 #listBlocCom = [] r = nodeInfos.getElementsByTagName(B_BLOCSCOMMUNS) if r != []: lr = r[0].getElementsByTagName(B_BC) nb = len(lr) ; i = 0 tabBC = Numeric.zeros((nb,2), Numeric.Int16) for m in lr : #listBlocCom.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) tabBC[i][0] = int(m.getAttribute(B_DEB)) tabBC[i][1] = int(m.getAttribute(B_FIN)) i += 1 #listBlocDep = [] r = nodeInfos.getElementsByTagName(B_BLOCSDEPLACES) if r != []: lr = r[0].getElementsByTagName(B_DEP) nb = len(lr) ; i = 0 tabBD = Numeric.zeros((nb,2,2), Numeric.Int16) for m in lr : #listBlocDep.append(((int(m.getAttribute(B1_D)),int(m.getAttribute(B1_F))), # (int(m.getAttribute(B2_D)),int(m.getAttribute(B2_F))))) tabBD[i][0][0] = int(m.getAttribute(B1_D)) tabBD[i][0][1] = int(m.getAttribute(B1_F)) tabBD[i][1][0] = int(m.getAttribute(B2_D)) tabBD[i][1][1] = int(m.getAttribute(B2_F)) i += 1 #transfo = Transformations(listInsert, listSupp, listDepl, listRemp, textSource, textCible) #res = Resultat(listInsert, listSupp, listDepl, listRemp,lgsource,textesConca,listBlocCom,listBlocDep) res = Resultat(tabIns, tabSup, tabDep, tabRemp,lgsource,textesConca, tabBC, tabBD) #listInsert = None; listSupp = None; listDepl = None; listRemp = None;listBlocCom = None;listBlocDep = None ## creer un objet de type 'Commentaire' si la balise B_COMMENTAIRE existe c = nodeInfos.getElementsByTagName(B_COMMENTAIRE) if c != []: comment = Commentaire((c[0]._get_firstChild()).toxml()) else : comment = Commentaire('') logging.debug('fin __getInformations') ## creer et retourner un objet du type 'Informations' return Informations(pVersionSource, pEtatSource, pVersionCible, pEtatCible,pParametres, res, comment) def enregistrerTransformationsStream(self, planTravail, pResultat): """ enregistre un fichier xml par streaming évite de construire un DOM complet ce qui consomme trop de memoire pour de gros fichiers source: taille des 2 fichiers supérieure à 3.5 Mo écrase le fichier existant éventuel toutes les chaines écrites doivent être en unicode (utf-8 par défaut) """ def u(x): return unicode(x) nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) pParametres = planTravail.getParam() output_file = file(fim.getFileInfo(), 'w') writer = MarkupWriter(output_file, indent=u"yes") writer.startDocument() writer.startElement(B_INFORMATIONS, attributes={ B_VERS_SOURCE : u(planTravail.getVersionSource()), B_ETAT_SOURCE : u(planTravail.getEtatSource()), B_VERS_CIBLE : u(planTravail.getVersionCible()), B_ETAT_CIBLE : u(planTravail.getEtatCible()), B_PARAM_1 : u('%s'%pParametres.getp1()), B_PARAM_2 : u('%s'%pParametres.getp2()), B_PARAM_3 : u('%s'%pParametres.getp3()), B_PARAM_4 : u('%s'%pParametres.getp4()), B_PARAM_5 : u('%s'%pParametres.getp5()), B_PARAM_6 : u('%s'%pParametres.getp6()), B_PARAM_7 : u('%s'%pParametres.getp7())}) # Balise Transformations writer.startElement(B_TRANSFORMATIONS) writer.simpleElement(B_LGSOURCE, attributes={B_LG : u(pResultat.getLgSource())}) # Insertions writer.startElement(B_INSERTIONS) listeInsert = pResultat.getListeInsertions() for deb,fin in listeInsert : writer.simpleElement(B_INS, attributes={B_DEB : u(deb), B_FIN : u(fin)}) writer.endElement(B_INSERTIONS) # Suppressions writer.startElement(B_SUPPRESSIONS) listeSupp = pResultat.getListeSuppressions() for deb,fin in listeSupp : writer.simpleElement(B_SUP, attributes={B_DEB : u(deb), B_FIN : u(fin)}) writer.endElement(B_SUPPRESSIONS) # Deplacements writer.startElement(B_DEPLACEMENTS) listeDepl = pResultat.getListeDeplacements() for deb,fin in listeDepl : writer.simpleElement(B_DEP, attributes={B_DEB : u(deb), B_FIN : u(fin)}) writer.endElement(B_DEPLACEMENTS) # Remplacement writer.startElement(B_REMPLACEMENTS) listeRemp = pResultat.getListeRemplacements() for deb,fin in listeRemp : writer.simpleElement(B_REMP, attributes={B_DEB : u(deb), B_FIN : u(fin)}) writer.endElement(B_REMPLACEMENTS) #blocs communs writer.startElement(B_BLOCSCOMMUNS) listeBlocsCommuns = pResultat.getBlocsCommuns() for deb,fin in listeBlocsCommuns : writer.simpleElement(B_BC, attributes={B_DEB : u(deb), B_FIN : u(fin)}) writer.endElement(B_BLOCSCOMMUNS) # blocs déplacés writer.startElement(B_BLOCSDEPLACES) listeBlocsDep = pResultat.getPairesBlocsDeplaces() for (b1deb,b1fin),(b2deb,b2fin) in listeBlocsDep : writer.simpleElement(B_DEP, attributes={B1_D : u(b1deb), B1_F : u(b1fin), B2_D : u(b2deb), B2_F : u(b2fin)}) writer.endElement(B_BLOCSDEPLACES) writer.endElement(B_TRANSFORMATIONS) writer.endElement(B_INFORMATIONS) writer.endDocument() class InformationsContentHandler(ContentHandler): def __init__(self,name): self.look_for = name self.is_name, self.is_mobile = None, None self.buffer = '' def startElement(self,name,attrs): if name == 'phone' and attrs.get('type') == 'mobile': self.is_mobile = 1 elif name == 'name': self.is_name = 1 def endElement(self,name): if self.is_name: self.current_name = self.buffer.strip() self.buffer = '' self.is_name = None elif self.is_mobile and self.current_name == self.look_for: self.mobile = self.buffer raise SAXException('Found mobile phone') # stop parsing def characters(self,chars): if self.is_name or self.is_mobile: self.buffer += chars def find_mobile_phone(name): handler = PhoneContentHandler(name) parser = make_parser() parser.setContentHandler(handler) try: parser.parse(open('addressbook.xml')) except SAXException: return handler.mobile return None if __name__ == '__main__': import sys name = ' '.join(sys.argv[1:]) phone = find_mobile_phone(name) if phone: print 'Mobile phone is',phone else: print 'No mobile phone found for',name