Package medite :: Package Controleurs :: Module cInformations
[hide private]
[frames] | no frames]

Source Code for Module medite.Controleurs.cInformations

   1  # -*- coding: iso-8859-1 -*- 
   2  # Copyright 20003 - 2008: Julien Bourdaillet (julien.bourdaillet@lip6.fr), Jean-Gabriel Ganascia (jean-gabriel.ganascia@lip6.fr) 
   3  # This file is part of MEDITE. 
   4  # 
   5  #    MEDITE is free software; you can redistribute it and/or modify 
   6  #    it under the terms of the GNU General Public License as published by 
   7  #    the Free Software Foundation; either version 2 of the License, or 
   8  #    (at your option) any later version. 
   9  # 
  10  #    MEDITE is distributed in the hope that it will be useful, 
  11  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13  #    GNU General Public License for more details. 
  14  # 
  15  #    You should have received a copy of the GNU General Public License 
  16  #    along with Foobar; if not, write to the Free Software 
  17  #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
  18  #---------------------------------------------------------------------------- 
  19  # Nom:          cInformations.py 
  20  # Objet:        Classe qui interface entre les fichiers xml et l'interface graphique 
  21  #---------------------------------------------------------------------------- 
  22   
  23   
  24  #from xml.dom.pulldom import * 
  25  #from xml.dom.minidom import Document 
  26  import os 
  27  import posixpath 
  28  import traceback 
  29  import logging,gc,sys 
  30  import xml.dom.minidom 
  31  #import xml.dom.pulldom 
  32  import Ft.Xml, Ft.Xml.XPath 
  33  from Ft.Xml import MarkupWriter 
  34  from xml.sax import make_parser, SAXException 
  35  from xml.sax.handler import ContentHandler 
  36   
  37  from string import * 
  38  import numpy.oldnumeric as Numeric 
  39   
  40  from Utile.constantesDonnees import * 
  41  from Utile.exceptionsDonnees import * 
  42   
  43  from Donnees.resultatAppli import * 
  44  from Utile.codageXml import * 
  45  from Donnees.arbre import * 
  46  import Utile.decodage 
  47  from Donnees.planTravail import * 
  48  import Controleurs.DGManager 
  49  import Controleurs.FInfoManager 
  50   
  51  import Donnees.resultatAppli 
  52   
53 -class CInformations(object) :
54 - def __init__(self, stream=False):
55 """ stream ecrit un nouveau doc en stream, il ecrase le fichier existant 56 mais permet d'ecrire des gros doc xml""" 57 self.dgm = Controleurs.DGManager.DGManager() 58 self.stream = stream
59 60 # Methode qui enregistre les informations d'un couple d'états
61 - def existeInformations__(self, planTravail):
62 """Methode qui renvoie vrai si des informations existent sur un couple de etats 63 pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ 64 if planTravail.getParam().isNull(): return False 65 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 66 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 67 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 68 fic = lireDocXml(fim.getFileInfo()) 69 pParametres = planTravail.getParam() 70 liste = fic.getElementsByTagName(B_INFORMATIONS) 71 for node in liste : 72 if (node.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and 73 node.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and 74 node.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and 75 node.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible() and 76 node.getAttribute(B_PARAM_1) == '%s'%pParametres.getp1() and 77 node.getAttribute(B_PARAM_2) == '%s'%pParametres.getp2() and 78 node.getAttribute(B_PARAM_3) == '%s'%pParametres.getp3() and 79 node.getAttribute(B_PARAM_4) == '%s'%pParametres.getp4() and 80 node.getAttribute(B_PARAM_5) == '%s'%pParametres.getp5() and 81 node.getAttribute(B_PARAM_6) == '%s'%pParametres.getp6() and 82 node.getAttribute(B_PARAM_7) == '%s'%pParametres.getp7()): 83 return True 84 return False
85 86 # Methode qui enregistre les informations d'un couple d'états
87 - def existeInformations(self, planTravail):
88 """Methode qui renvoie vrai si des informations existent sur un couple de etats 89 pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ 90 if planTravail.getParam().isNull(): return False 91 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 92 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 93 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 94 pParametres = planTravail.getParam() 95 f = open(fim.getFileInfo()) 96 text = f.read() 97 f.close() 98 # on coupe après arbre pour ne garder que les infos de l'arbre des versions 99 # on cherhce les balises <informations atributs...> 100 deb = text.find('<informations') ; transfo = text.find('<transformations>') 101 fin = text.find('</informations>') 102 while deb != -1: 103 # on ajoute '<xml ...' pour avoir un Document et extraire le noeud informations 104 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 105 text[deb:transfo] +'</informations></root>' 106 xml_informations = Ft.Xml.Parse(chaine_informations) 107 #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement 108 nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+')',contextNode = xml_informations) 109 if len(nodeListe) != 0: 110 node = nodeListe[0] 111 if (node.getAttributeNS(node.namespaceURI, B_VERS_SOURCE)== planTravail.getVersionSource() and 112 node.getAttributeNS(node.namespaceURI, B_ETAT_SOURCE)== planTravail.getEtatSource() and 113 node.getAttributeNS(node.namespaceURI, B_VERS_CIBLE) == planTravail.getVersionCible() and 114 node.getAttributeNS(node.namespaceURI, B_ETAT_CIBLE) == planTravail.getEtatCible() and 115 node.getAttributeNS(node.namespaceURI, B_PARAM_1) == '%s'%pParametres.getp1() and 116 node.getAttributeNS(node.namespaceURI, B_PARAM_2) == '%s'%pParametres.getp2() and 117 node.getAttributeNS(node.namespaceURI, B_PARAM_3) == '%s'%pParametres.getp3() and 118 node.getAttributeNS(node.namespaceURI, B_PARAM_4) == '%s'%pParametres.getp4() and 119 node.getAttributeNS(node.namespaceURI, B_PARAM_5) == '%s'%pParametres.getp5() and 120 node.getAttributeNS(node.namespaceURI, B_PARAM_6) == '%s'%pParametres.getp6() and 121 node.getAttributeNS(node.namespaceURI, B_PARAM_7) == '%s'%pParametres.getp7()): 122 return True 123 deb = text.find('<informations',fin+15) ; transfo = text.find('<transformations>',fin+15) 124 fin = text.find('</informations>',fin+15) 125 return False
126 127 # Methode qui enregistre les transformations d'un couple d'états
128 - def enregistrerTransformations__(self, planTravail,pTransformations):
129 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ 130 isinstance(pTransformations, Donnees.transformations.Transformations)""" 131 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 132 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 133 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 134 if self.stream: 135 self.enregistrerTransformationsStream(planTravail, pTransformations) 136 elif not self.existeInformations(planTravail): 137 xmlInfo = self.__creerInformations(planTravail, pTransformations) 138 fic = lireDocXml(fim.getFileInfo()) 139 nodeRoot = fic._get_firstChild() 140 nodeRoot.appendChild(xmlInfo) 141 ecrireDocXml(fim.getFileInfo(),fic)
142 143 # Methode qui enregistre les transformations d'un couple d'états
144 - def enregistrerTransformations(self, planTravail,pTransformations):
145 """ 146 Avec Ft.Xml 147 pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ 148 isinstance(pTransformations, Donnees.transformations.Transformations)""" 149 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 150 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 151 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 152 if self.stream: 153 self.enregistrerTransformationsStream(planTravail, pTransformations) 154 elif not self.existeInformations(planTravail): 155 xmlInfo = self.__creerInformations(planTravail, pTransformations) 156 fic = lireDocXmlFt(fim.getFileInfo()) 157 nodeRoot = fic.firstChild 158 nodeRoot.appendChild(xmlInfo) 159 ecrireDocXmlFt(fim.getFileInfo(),fic)
160 161 # Methode qui enregistre le commentaire d'un couple d'états
162 - def enregistrerCommentaire(self, planTravail, pCommentaire):
163 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ 164 isinstance(pCommentaire, str)""" 165 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 166 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 167 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 168 commentaire = Commentaire(encodingNode(pCommentaire)) 169 fic = lireDocXml(fim.getFileInfo()) 170 liste = fic.getElementsByTagName(B_INFORMATIONS) 171 infos = self.__rechercherInfos(liste, planTravail) 172 lc = infos.getElementsByTagName(B_COMMENTAIRE) 173 if lc == [] : ## creer et ajouter le noeud 'commentaire' 174 comment = self.__creerCommentaire(commentaire) 175 infos.appendChild(comment) 176 ecrireDocXml(fim.getFileInfo(),fic) 177 else : # il existe deja un commentaire, une exception est lancee 178 c = (lc[0]._get_firstChild()).toxml() 179 comment = Commentaire(c) 180 raise CommentaireDejaExistant('le commentaire de"'+planTravail.getVersionSource() +'/'+planTravail.getEtatSource()+ 181 '" et "'+planTravail.getVersionCible()+'/'+planTravail.getEtatCible()+'" est deja existant', comment)
182 183 # Methode qui remplace le commentaire existant par le nouveau
184 - def remplacerCommentaire(self, planTravail, pCommentaire):
185 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail)and \ 186 isinstance(pCommentaire, str)""" 187 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 188 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 189 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 190 commentaire = Commentaire(pCommentaire) 191 fic = lireDocXml(fim.getFileInfo()) 192 liste = fic.getElementsByTagName(B_INFORMATIONS) 193 infos = self.__rechercherInfos(liste, planTravail) 194 lc = infos.getElementsByTagName(B_COMMENTAIRE) 195 comment = self.__creerCommentaire(commentaire) 196 if lc != [] : 197 infos.removeChild(lc[0]) 198 infos.appendChild(comment) 199 ecrireDocXml(fim.getFileInfo(),fic)
200 201 # Methode qui ajoute le nouveau commentaire a l'ancien
202 - def ajouterCommentaire(self, planTravail, pCommentaire):
203 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ 204 isinstance(pCommentaire, str)""" 205 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 206 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 207 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 208 commentaire = Commentaire(pCommentaire) 209 fic = lireDocXml(fim.getFileInfo()) 210 liste = fic.getElementsByTagName(B_INFORMATIONS) 211 infos = self.__rechercherInfos(liste, planTravail) 212 lc = infos.getElementsByTagName(B_COMMENTAIRE) 213 if lc == []:## creer et ajouter le noeud 'commentaire' 214 comment = self.__creerCommentaire(commentaire) 215 infos.appendChild(comment) 216 else : 217 xmldoc=Document() 218 comment =' '+ commentaire.getCommentaire() 219 lc[0].appendChild(xmldoc.createTextNode(comment)) 220 ecrireDocXml(fim.getFileInfo(), fic)
221 222 # Methode qui supprime le commentaire d'un couple d'états
223 - def supprimerCommentaire(self, planTravail):
224 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ 225 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 226 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 227 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 228 fic = lireDocXml(fim.getFileInfo()) 229 liste = fic.getElementsByTagName(B_INFORMATIONS) 230 infos = self.__rechercherInfos(liste, planTravail) 231 lc = infos.getElementsByTagName(B_COMMENTAIRE) 232 if lc != [] : 233 infos.removeChild(lc[0]) 234 ecrireDocXml(fim.getFileInfo(), fic) 235 else : 236 raise CommentaireNonTrouve('les informations de "'+planTravail.getVersionSource() +'/'+planTravail.getEtatSource()+ 237 '" et "'+planTravail.getVersionCible()+'/'+planTravail.getEtatCible()+'" n\'ont pas de commentaire')
238 239 # Methode qui supprime les informations d'un couple d'états
240 - def supprimerInformations(self, planTravail):
241 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ 242 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 243 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 244 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 245 fic = lireDocXml(fim.getFileInfo()) 246 noderoot = fic.getElementsByTagName(B_ROOT) 247 liste = fic.getElementsByTagName(B_INFORMATIONS) 248 infos = self.__rechercherInfos(liste, planTravail) 249 if infos != None:## suppression du noeud 250 noderoot[0].removeChild(infos) 251 ecrireDocXml(fim.getFileInfo(),fic)
252
253 - def enregistrerInformations(self, planTravail,pTransformations, pCommentaire):
254 """ Methode qui enregistre les informations (transformations + commentaire) d'un couple de etats 255 pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ 256 if not self.existeInformations(planTravail ): 257 self.enregistrerTransformations(planTravail, pTransformations) 258 self.enregistrerCommentaire(planTravail, pCommentaire)
259 260 # Methode qui retourne toutes les informations d'un couple d'états 261 # Methode qui renvoie toutes les informations existantes pour un dossier genetique
262 - def rechercherToutesInformationsDG__(self, pNom, pTitre):
263 nomDG = self.dgm.getDG(pNom, pTitre) 264 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), 265 Controleurs.DGManager.Oeuvre(pTitre)) 266 fic = lireDocXml(fim.getFileInfo()) 267 liste = fic.getElementsByTagName(B_INFORMATIONS) 268 listeInfos = [] 269 for node in liste : 270 vSource = node.getAttribute(B_VERS_SOURCE) 271 fSource = node.getAttribute(B_ETAT_SOURCE) 272 vCible = node.getAttribute(B_VERS_CIBLE) 273 fCible = node.getAttribute(B_ETAT_CIBLE) 274 par1 = atoi(node.getAttribute(B_PARAM_1)) 275 par2 = atoi(node.getAttribute(B_PARAM_2)) 276 par3 = atoi(node.getAttribute(B_PARAM_3)) 277 par4 = atoi(node.getAttribute(B_PARAM_4)) 278 par5 = atoi(node.getAttribute(B_PARAM_5)) 279 par6 = atoi(node.getAttribute(B_PARAM_6)) 280 par7 = atoi(node.getAttribute(B_PARAM_7)) 281 listeInfos.append([vSource,fSource,vCible,fCible,par1,par2,par3,par4,par5,par6,par7]) 282 return listeInfos
283
284 - def rechercherToutesInformationsDG(self, pNom, pTitre):
285 nomDG = self.dgm.getDG(pNom, pTitre) 286 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), 287 Controleurs.DGManager.Oeuvre(pTitre)) 288 f = open(fim.getFileInfo()) 289 text = f.read() 290 f.close() 291 listeInfos = [] 292 # on coupe après arbre pour ne garder que les infos de l'arbre des versions 293 # on cherhce les balises <informations atributs...> 294 deb = text.find('<informations') ; transfo = text.find('<transformations>') 295 fin = text.find('</informations>') 296 while deb != -1: 297 # on ajoute '<xml ...' pour avoir un Document et extraire le noeud informations 298 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 299 text[deb:transfo] +'</informations></root>' 300 xml_informations = Ft.Xml.Parse(chaine_informations) 301 #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement 302 nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+')',contextNode = xml_informations) 303 if len(nodeListe) != 0: 304 node = nodeListe[0] 305 vSource = node.getAttributeNS(node.namespaceURI, B_VERS_SOURCE) 306 fSource = node.getAttributeNS(node.namespaceURI, B_ETAT_SOURCE) 307 vCible = node.getAttributeNS(node.namespaceURI, B_VERS_CIBLE) 308 fCible = node.getAttributeNS(node.namespaceURI, B_ETAT_CIBLE) 309 par1 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_1)) 310 par2 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_2)) 311 par3 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_3)) 312 par4 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_4)) 313 par5 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_5)) 314 par6 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_6)) 315 par7 = atoi(node.getAttributeNS(node.namespaceURI, B_PARAM_7)) 316 listeInfos.append([vSource,fSource,vCible,fCible,par1,par2,par3,par4,par5,par6,par7]) 317 deb = text.find('<informations',fin+15) ; transfo = text.find('<transformations>',fin+15) 318 fin = text.find('</informations>',fin+15) 319 return listeInfos
320 321 # Methode qui retourne toutes les informations 322 # de tous les couples enregistrés d'états d'un DG 323 # renvoie une liste d'objets Information
324 - def rechercherLInformationsDG(self, pNom, pTitre):
325 nomDG = self.dgm.getDG(pNom, pTitre) 326 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), 327 Controleurs.DGManager.Oeuvre(pTitre)) 328 fic = lireDocXml(fim.getFileInfo()) 329 liste = fic.getElementsByTagName(B_INFORMATIONS) 330 listeInfos = [] 331 for infos in liste : 332 tmpInfo = self.__getInformations(infos, pNom, pTitre) 333 listeInfos.append(tmpInfo) 334 return listeInfos
335
336 - def generateLInformationsDG__(self, pNom, pTitre):
337 """ Générateur des informations de comparaisons 338 Le générateur evite de charger plusieurs informations en même temps lorsque celes ci son grosses (plusieurs Mo) 339 On evite de faire un SAX en recherchant dans le fichier XML les balises <informations></informations> 340 une par une et a la main, c'est pas propre mais efficace """ 341 nomDG = self.dgm.getDG(pNom, pTitre) 342 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), 343 Controleurs.DGManager.Oeuvre(pTitre)) 344 f = open(fim.getFileInfo()) 345 text = f.read() 346 f.close() 347 # on coupe après arbre pour ne garder que les infos de l'arbre des versions 348 deb = text.find('<informations') ; fin = text.find('</informations>') 349 while deb != -1: 350 # on ajoute '<xml ...' pour avoir un Document et extraire le noeud informations 351 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 352 text[deb:fin+15] +'</root>' # +15 pour la longueur de la balise </informations> 353 #logging.debug(chaine_informations) 354 xml_informations = xml.dom.minidom.parseString(chaine_informations) 355 liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement 356 objet_informations = self.__getInformations(liste[0], pNom, pTitre) 357 #objet_informations = self.__getInformations(xml_informations.getElementsByTagName(B_INFORMATIONS)[0], pNom, pTitre) 358 #for child in liste[0].childNodes: 359 # logging.debug(child) 360 # child.unlink() 361 liste = None 362 xml_informations.unlink() ; xml_informations = None 363 #unreachable = gc.collect() 364 #logging.debug( 'unreachable='+str(unreachable)) 365 yield objet_informations 366 deb = text.find('<informations',fin+15) ; fin = text.find('</informations>',fin+15)
367
368 - def generateLInformationsDG(self, pNom, pTitre):
369 """ Avec Ft.Xml.Domlette 370 Générateur des informations de comparaisons 371 Le générateur evite de charger plusieurs informations en même temps lorsque celes ci son grosses (plusieurs Mo) 372 On evite de faire un SAX en recherchant dans le fichier XML les balises <informations></informations> 373 une par une et a la main, c'est pas propre mais efficace """ 374 nomDG = self.dgm.getDG(pNom, pTitre) 375 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(pNom), 376 Controleurs.DGManager.Oeuvre(pTitre)) 377 f = open(fim.getFileInfo()) 378 text = f.read() 379 f.close() 380 # on coupe après arbre pour ne garder que les infos de l'arbre des versions 381 deb = text.find('<informations') ; fin = text.find('</informations>') 382 while deb != -1: 383 # on ajoute '<xml ...' pour avoir un Document et extraire le noeud informations 384 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 385 text[deb:fin+15] +'</root>' # +15 pour la longueur de la balise </informations> 386 #logging.debug(chaine_informations) 387 #xml_informations = xml.dom.minidom.parseString(chaine_informations) 388 xml_informations = Ft.Xml.Parse(chaine_informations) 389 #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement 390 nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+')',contextNode = xml_informations) 391 if len(nodeListe) != 0: 392 objet_informations = self.__getInformations(nodeListe[0], pNom, pTitre) 393 nodeListe = None ; xml_informations = None #.unlink() 394 logging.debug('fin rechercherInformations') 395 yield objet_informations 396 deb = text.find('<informations',fin+15) ; fin = text.find('</informations>',fin+15)
397
398 - def rechercherToutesInformations__(self, planTravail):
399 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ 400 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 401 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 402 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 403 fic = lireDocXml(fim.getFileInfo()) 404 liste = fic.getElementsByTagName(B_INFORMATIONS) 405 listeInfos = [] 406 for infos in liste : 407 if (infos.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and 408 infos.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and 409 infos.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and 410 infos.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible()): 411 tmpInfo = self.__getInformations(infos, planTravail.getAuteur(), planTravail.getOeuvre()) 412 listeInfos.append(tmpInfo) 413 return listeInfos
414 415 # Methode qui retourne toutes les informations d'un couple d'états
416 - def rechercherToutesInformations(self, planTravail):
417 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ 418 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 419 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 420 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 421 listeInfos = [] 422 f = open(fim.getFileInfo()) 423 text = f.read() 424 f.close() 425 # on coupe après arbre pour ne garder que les infos de l'arbre des versions 426 deb = text.find('<informations') ; fin = text.find('</informations>') 427 while deb != -1: 428 # on ajoute '<xml ...' pour avoir un Document et extraire le noeud informations 429 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 430 text[deb:fin+15] +'</root>' # +15 pour la longueur de la balise </informations> 431 #logging.debug(chaine_informations) 432 #xml_informations = xml.dom.minidom.parseString(chaine_informations) 433 xml_informations = Ft.Xml.Parse(chaine_informations) 434 #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) # liste avec 1 seul element nécessairement 435 nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+')',contextNode = xml_informations) 436 if len(nodeListe) != 0: 437 node = nodeListe[0] 438 if (node.getAttributeNS(node.namespaceURI, B_VERS_SOURCE) == planTravail.getVersionSource() and 439 node.getAttributeNS(node.namespaceURI, B_ETAT_SOURCE) == planTravail.getEtatSource() and 440 node.getAttributeNS(node.namespaceURI, B_VERS_CIBLE) == planTravail.getVersionCible() and 441 node.getAttributeNS(node.namespaceURI, B_ETAT_CIBLE) == planTravail.getEtatCible()): 442 listeInfos.append(self.__getInformations(node, planTravail.getAuteur(), planTravail.getOeuvre())) 443 deb = text.find('<informations',fin+15) ; fin = text.find('</informations>',fin+15) 444 return listeInfos
445 446 447 448 # Methode qui retourne les informations d'un couple d'états et des parametres precis
449 - def __rechercherInformations(self, planTravail):
450 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) """ 451 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 452 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 453 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 454 pParametres = planTravail.getParam() 455 fic = lireDocXml(fim.getFileInfo()) 456 liste = fic.getElementsByTagName(B_INFORMATIONS) 457 for node in liste : 458 if (node.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and 459 node.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and 460 node.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and 461 node.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible() and 462 node.getAttribute(B_PARAM_1) == '%s'%pParametres.getp1() and 463 node.getAttribute(B_PARAM_2) == '%s'%pParametres.getp2() and 464 node.getAttribute(B_PARAM_3) == '%s'%pParametres.getp3() and 465 node.getAttribute(B_PARAM_4) == '%s'%pParametres.getp4() and 466 node.getAttribute(B_PARAM_5) == '%s'%pParametres.getp5() and 467 node.getAttribute(B_PARAM_6) == '%s'%pParametres.getp6() and 468 node.getAttribute(B_PARAM_7) == '%s'%pParametres.getp7()): 469 return self.__getInformations(node,planTravail.getAuteur(),planTravail.getOeuvre()) 470 return None
471
472 - def rechercherInformations__(self, planTravail):
473 """ Recherche une information de comparaisons 474 On recherche la chaine informations dans le fichier xml sans le parser en DOM ce qui est 475 très lours quand le fichier est gros, 476 une fois trouvé, on extrait la chaine conteant la bonne balise informations et on 477 la parse effectivement en DOM 478 pas propre mais beaucoup plus rapide que la méthode __rechercherInformations 479 quand les fichiers sont gros """ 480 logging.debug('begin rechercherInformations') 481 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 482 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 483 pParametres = planTravail.getParam() 484 f = open(fim.getFileInfo()) 485 text = f.read() 486 f.close() 487 # on ccherhce les balises <informations atributs...> 488 deb = text.find('<informations') ; transfo = text.find('<transformations>') 489 fin = text.find('</informations>') 490 while deb != -1: 491 # on ajoute '<xml ...' pour avoir un Document et extraire le noeud informations 492 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 493 text[deb:transfo] +'</informations></root>' 494 #logging.debug(chaine_informations) 495 xml_informations = xml.dom.minidom.parseString(chaine_informations) 496 liste = xml_informations.getElementsByTagName(B_INFORMATIONS) 497 node = liste[0] # liste avec 1 seul element nécessairement 498 if (node.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and 499 node.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and 500 node.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and 501 node.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible() and 502 node.getAttribute(B_PARAM_1) == '%s'%pParametres.getp1() and 503 node.getAttribute(B_PARAM_2) == '%s'%pParametres.getp2() and 504 node.getAttribute(B_PARAM_3) == '%s'%pParametres.getp3() and 505 node.getAttribute(B_PARAM_4) == '%s'%pParametres.getp4() and 506 node.getAttribute(B_PARAM_5) == '%s'%pParametres.getp5() and 507 node.getAttribute(B_PARAM_6) == '%s'%pParametres.getp6() and 508 node.getAttribute(B_PARAM_7) == '%s'%pParametres.getp7()): 509 # on a le bon noeud informations, on récupère le noeud et tous ses fils 510 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 511 text[deb:fin+15] +'</root>' 512 xml_informations = xml.dom.minidom.parseString(chaine_informations) 513 liste = xml_informations.getElementsByTagName(B_INFORMATIONS) 514 objet_informations = self.__getInformations(liste[0], planTravail.getAuteur(),planTravail.getOeuvre()) 515 liste = None ; xml_informations.unlink() 516 logging.debug('fin rechercherInformations') 517 return objet_informations 518 deb = text.find('<informations',fin+15) ; transfo = text.find('<transformations>',fin+15) 519 fin = text.find('</informations>',fin+15) 520 return None
521
522 - def rechercherInformations(self, planTravail):
523 """ 524 Recherche avec Ft.Xml.Domlette 525 Recherche une information de comparaisons 526 On recherche la chaine informations dans le fichier xml sans le parser en DOM ce qui est 527 très lours quand le fichier est gros, 528 une fois trouvé, on extrait la chaine conteant la bonne balise informations et on 529 la parse effectivement en DOM 530 pas propre mais beaucoup plus rapide que la méthode __rechercherInformations 531 quand les fichiers sont gros """ 532 logging.debug('begin rechercherInformations') 533 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 534 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 535 pParametres = planTravail.getParam() 536 f = open(fim.getFileInfo()) 537 text = f.read() 538 f.close() 539 # on cherhce les balises <informations atributs...> 540 deb = text.find('<informations') ; transfo = text.find('<transformations>') 541 fin = text.find('</informations>') 542 while deb != -1: 543 # on ajoute '<xml ...' pour avoir un Document et extraire le noeud informations 544 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 545 text[deb:transfo] +'</informations></root>' 546 #logging.debug(chaine_informations) 547 #xml_informations = xml.dom.minidom.parseString(chaine_informations) 548 xml_informations = Ft.Xml.Parse(chaine_informations) 549 #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) 550 #node = liste[0] # liste avec 1 seul element nécessairement 551 nodeListe = Ft.Xml.XPath.Evaluate(u'(/root/'+B_INFORMATIONS+ 552 '[@'+B_VERS_SOURCE+'="'+planTravail.getVersionSource()+'"]' + 553 '[@'+B_ETAT_SOURCE+'="'+planTravail.getEtatSource()+'"]' + 554 '[@'+B_VERS_CIBLE+'="'+planTravail.getVersionCible()+'"]' + 555 '[@'+B_ETAT_CIBLE+'="'+planTravail.getEtatCible()+'"]' + 556 '[@'+B_PARAM_1+'="'+str(pParametres.getp1())+'"]' + 557 '[@'+B_PARAM_2+'="'+str(pParametres.getp2())+'"]' + 558 '[@'+B_PARAM_3+'="'+str(pParametres.getp3())+'"]' + 559 '[@'+B_PARAM_4+'="'+str(pParametres.getp4())+'"]' + 560 '[@'+B_PARAM_5+'="'+str(pParametres.getp5())+'"]' + 561 '[@'+B_PARAM_6+'="'+str(pParametres.getp6())+'"]'+ 562 '[@'+B_PARAM_7+'="'+str(pParametres.getp7())+'"])' 563 ,contextNode = xml_informations) 564 565 if len(nodeListe) != 0: 566 # on a le bon noeud informations, on récupère le noeud et tous ses fils 567 chaine_informations = '<?xml version="1.0" encoding="iso-8859-1" ?><root>' + \ 568 text[deb:fin+15] +'</root>' 569 #xml_informations = xml.dom.minidom.parseString(chaine_informations) 570 xml_informations = Ft.Xml.Parse(chaine_informations) 571 #liste = xml_informations.getElementsByTagName(B_INFORMATIONS) 572 liste = Ft.Xml.XPath.Evaluate(u'(//'+B_INFORMATIONS+')',contextNode = xml_informations) 573 objet_informations = self.__getInformations(liste[0], planTravail.getAuteur(),planTravail.getOeuvre()) 574 liste = None ; xml_informations = None #.unlink() 575 logging.debug('fin rechercherInformations') 576 return objet_informations 577 deb = text.find('<informations',fin+15) ; transfo = text.find('<transformations>',fin+15) 578 fin = text.find('</informations>',fin+15) 579 return None
580 581
582 - def __rechercherInfos(self, pListeBalise, planTravail):
583 """ Methode privee qui renvoie la balise "informations" recherchee 584 pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ 585 isinstance(pListeBalise, list)""" 586 pParametres = planTravail.getParam() 587 for node in pListeBalise : 588 if (node.getAttribute(B_VERS_SOURCE)== planTravail.getVersionSource() and 589 node.getAttribute(B_ETAT_SOURCE)== planTravail.getEtatSource() and 590 node.getAttribute(B_VERS_CIBLE) == planTravail.getVersionCible() and 591 node.getAttribute(B_ETAT_CIBLE) == planTravail.getEtatCible() and 592 node.getAttribute(B_PARAM_1) == '%s'%pParametres.getp1() and 593 node.getAttribute(B_PARAM_2) == '%s'%pParametres.getp2() and 594 node.getAttribute(B_PARAM_3) == '%s'%pParametres.getp3() and 595 node.getAttribute(B_PARAM_4) == '%s'%pParametres.getp4() and 596 node.getAttribute(B_PARAM_5) == '%s'%pParametres.getp5() and 597 node.getAttribute(B_PARAM_6) == '%s'%pParametres.getp6() and 598 node.getAttribute(B_PARAM_7) == '%s'%pParametres.getp7()): 599 return node 600 601 return None
602 # raise InformationsNonTrouvees('les informations de "'+pVersionSource+ 603 # '/'+pEtatSource+'" et "'+pVersionCible+ 604 # '/'+pEtatCible+'" n\'existent pas') 605 606 607 # Methode privee qui renvoie vrai si l'index passe en parametre est l'un 608 # des premiers indices de la liste de couple passee en parametre
609 - def __appartientListe(self, pIndex, pListe):
610 for i in pListe : 611 if pIndex == i[0]: 612 return 1 ## VRAI 613 return 0 ## FALSE
614 615 616 617 # Methode privee pour rechercher dans une liste de couples d'indices 618 # le couple dont le 1er indice est passe en parametre
619 - def __getCpleIndices(self, pListe, indice):
620 for i in pListe : 621 if i[0] == indice : 622 return i 623 return None
624 625 # Methode privee pour creer les sous balises de deplacement dans la balise 626 # passee en parametre
627 - def __sousBalises(self, pBalise, pTextNorm, pBlocText, pLDepl):
628 textNetBloc = pTextNorm+pBlocText 629 lg = len(pTextNorm) 630 xmldoc = Document() 631 for i in range(len(pTextNorm), len(textNetBloc)): 632 cple = self.__getCpleIndices(pLDepl, i) 633 if cple != None: 634 m1 = encodingNode(textNetBloc[lg : cple[0] ]) 635 mot1 = xmldoc.createTextNode(m1) 636 pBalise.appendChild(mot1) 637 m2 = encodingNode(textNetBloc[cple[0] : cple[1] ]) 638 mot2 = xmldoc.createTextNode(m2) 639 baliseDepl = xmldoc.createElement(B_TEXT_DEPL) 640 baliseDepl.appendChild(mot2) 641 pBalise.appendChild(baliseDepl) 642 i = len(textNetBloc[lg: cple[1] ]) 643 lg += i 644 m3 = encodingNode(textNetBloc[lg :]) 645 mot3 = xmldoc.createTextNode(m3) 646 pBalise.appendChild(mot3)
647 648 649 ## Fonction privee pour recuperer les textes sans les codes xml
650 - def __recupMot(self, pMot):
651 mot = pMot.replace('&quot;', '"')\ 652 .replace('&amp;', '&')\ 653 .replace('&lt;', '<')\ 654 .replace('&gt;', '>') 655 return mot
656 657 # Methode privee pour retrouver les caracteres deplaces d'une balise
658 - def __recupereListe(self, pBalise, pListe, pListeDepl, pTextes, pBlocs):
659 lg1 = len(pTextes) 660 motListe= '' 661 noeud = pBalise._get_firstChild() 662 while noeud != None : 663 if noeud.hasChildNodes() and noeud.tagName == B_TEXT_DEPL : 664 nFils = noeud._get_firstChild() 665 mot = '' 666 while nFils != None : 667 mot += self.__recupMot(nFils.toxml()) 668 noeud.removeChild(nFils) 669 nFils = noeud._get_firstChild() 670 lg2 = len(pTextes) 671 pTextes += mot 672 motListe += mot 673 pListeDepl.append( [lg2,len(pTextes)] ) 674 else : 675 mot = self.__recupMot(noeud.toxml()) 676 pTextes += mot 677 motListe += mot 678 pBalise.removeChild(noeud) 679 noeud = pBalise._get_firstChild() 680 pListe.append( [lg1, len(pTextes)] ) 681 pBlocs.append(pTextes[lg1:]) 682 return pTextes[lg1:]
683 684 # Methode privee qui creer le noeud <information> a inserer dans le fichier xml
685 - def __creerInformations_(self, planTravail, pResultat):
686 """pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ 687 isinstance(pResultat, Donnees.resultatAppli.Resultat) 688 post: isinstance(__return__, xml.dom.minidom.Node) """ 689 pParametres = planTravail.getParam() 690 xmldoc = Document() 691 # Balise Information 692 xmltagInfo = xmldoc.createElement(B_INFORMATIONS) 693 xmltagInfo.setAttribute(B_VERS_SOURCE,planTravail.getVersionSource() ) 694 xmltagInfo.setAttribute(B_ETAT_SOURCE,planTravail.getEtatSource()) 695 xmltagInfo.setAttribute(B_VERS_CIBLE,planTravail.getVersionCible()) 696 xmltagInfo.setAttribute(B_ETAT_CIBLE,planTravail.getEtatCible()) 697 xmltagInfo.setAttribute(B_PARAM_1,'%s'%pParametres.getp1()) 698 xmltagInfo.setAttribute(B_PARAM_2,'%s'%pParametres.getp2()) 699 xmltagInfo.setAttribute(B_PARAM_3,'%s'%pParametres.getp3()) 700 xmltagInfo.setAttribute(B_PARAM_4,'%s'%pParametres.getp4()) 701 xmltagInfo.setAttribute(B_PARAM_5,'%s'%pParametres.getp5()) 702 xmltagInfo.setAttribute(B_PARAM_6,'%s'%pParametres.getp6()) 703 xmltagInfo.setAttribute(B_PARAM_7,'%s'%pParametres.getp7()) 704 # Balise Transformations 705 xmlTransfo = xmldoc.createElement(B_TRANSFORMATIONS) 706 xmlLgsource = xmldoc.createElement(B_LGSOURCE) 707 xmlLgsource.setAttribute(B_LG, str(pResultat.getLgSource())) 708 xmlTransfo.appendChild(xmlLgsource) 709 # Insertions 710 xmlInsert = xmldoc.createElement(B_INSERTIONS) 711 listeInsert = pResultat.getListeInsertions() 712 for deb,fin in listeInsert : 713 node = xmldoc.createElement(B_INS) 714 node.setAttribute(B_DEB, str(deb)) 715 node.setAttribute(B_FIN, str(fin)) 716 xmlInsert.appendChild(node) 717 xmlTransfo.appendChild(xmlInsert) 718 # Suppressions 719 xmlSupp = xmldoc.createElement(B_SUPPRESSIONS) 720 listeSupp = pResultat.getListeSuppressions() 721 for deb,fin in listeSupp : 722 node = xmldoc.createElement(B_SUP) 723 node.setAttribute(B_DEB, str(deb)) 724 node.setAttribute(B_FIN, str(fin)) 725 xmlSupp.appendChild(node) 726 xmlTransfo.appendChild(xmlSupp) 727 # Deplacements 728 xmlDepl = xmldoc.createElement(B_DEPLACEMENTS) 729 listeDepl = pResultat.getListeDeplacements() 730 for deb,fin in listeDepl : 731 node = xmldoc.createElement(B_DEP) 732 node.setAttribute(B_DEB, str(deb)) 733 node.setAttribute(B_FIN, str(fin)) 734 xmlDepl.appendChild(node) 735 xmlTransfo.appendChild(xmlDepl) 736 # Remplacement 737 xmlRemp = xmldoc.createElement(B_REMPLACEMENTS) 738 listeRemp = pResultat.getListeRemplacements() 739 for deb,fin in listeRemp : 740 node = xmldoc.createElement(B_REMP) 741 node.setAttribute(B_DEB, str(deb)) 742 node.setAttribute(B_FIN, str(fin)) 743 xmlRemp.appendChild(node) 744 xmlTransfo.appendChild(xmlRemp) 745 #blocs communs 746 xmlBlocsCom = xmldoc.createElement(B_BLOCSCOMMUNS) 747 listeBlocsCommuns = pResultat.getBlocsCommuns() 748 for deb,fin in listeBlocsCommuns : 749 node = xmldoc.createElement(B_BC) 750 node.setAttribute(B_DEB, str(deb)) 751 node.setAttribute(B_FIN, str(fin)) 752 xmlBlocsCom.appendChild(node) 753 xmlTransfo.appendChild(xmlBlocsCom) 754 # blocs déplacés 755 xmlBlocsDep = xmldoc.createElement(B_BLOCSDEPLACES) 756 listeBlocsDep = pResultat.getPairesBlocsDeplaces() 757 for (b1deb,b1fin),(b2deb,b2fin) in listeBlocsDep : 758 node = xmldoc.createElement(B_DEP) 759 node.setAttribute(B1_D, str(b1deb)) 760 node.setAttribute(B1_F, str(b1fin)) 761 node.setAttribute(B2_D, str(b2deb)) 762 node.setAttribute(B2_F, str(b2fin)) 763 xmlBlocsDep.appendChild(node) 764 xmlTransfo.appendChild(xmlBlocsDep) 765 # Insertion de la balise B_TRANSFORMATIONS dans la balise B_INFORMATIONS 766 xmltagInfo.appendChild(xmlTransfo) 767 return xmltagInfo
768 769 # Methode privee qui creer le noeud <information> a inserer dans le fichier xml
770 - def __creerInformations(self, planTravail, pResultat):
771 """ 772 Avec Ft.Xml 773 pre: isinstance(planTravail, Donnees.planTravail.PlanTravail) and \ 774 isinstance(pResultat, Donnees.resultatAppli.Resultat) 775 post: isinstance(__return__, xml.dom.minidom.Node) """ 776 pParametres = planTravail.getParam() 777 #xmldoc = Ft.Xml.Document() 778 xmldoc = doc = Ft.Xml.Domlette.implementation.createDocument(Ft.Xml.EMPTY_NAMESPACE, None, None) 779 # Balise Information 780 xmltagInfo = xmldoc.createElementNS(xmldoc.namespaceURI, B_INFORMATIONS) 781 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_VERS_SOURCE,planTravail.getVersionSource() ) 782 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_ETAT_SOURCE,planTravail.getEtatSource()) 783 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_VERS_CIBLE,planTravail.getVersionCible()) 784 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_ETAT_CIBLE,planTravail.getEtatCible()) 785 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_1,'%s'%pParametres.getp1()) 786 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_2,'%s'%pParametres.getp2()) 787 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_3,'%s'%pParametres.getp3()) 788 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_4,'%s'%pParametres.getp4()) 789 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_5,'%s'%pParametres.getp5()) 790 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_6,'%s'%pParametres.getp6()) 791 xmltagInfo.setAttributeNS(xmltagInfo.namespaceURI, B_PARAM_7,'%s'%pParametres.getp7()) 792 # Balise Transformations 793 xmlTransfo = xmldoc.createElementNS(xmldoc.namespaceURI, B_TRANSFORMATIONS) 794 xmlLgsource = xmldoc.createElementNS(xmldoc.namespaceURI, B_LGSOURCE) 795 xmlLgsource.setAttributeNS(xmlLgsource.namespaceURI, B_LG, str(pResultat.getLgSource())) 796 xmlTransfo.appendChild(xmlLgsource) 797 # Insertions 798 xmlInsert = xmldoc.createElementNS(xmldoc.namespaceURI, B_INSERTIONS) 799 listeInsert = pResultat.getListeInsertions() 800 for deb,fin in listeInsert : 801 node = xmldoc.createElementNS(xmldoc.namespaceURI, B_INS) 802 node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) 803 node.setAttributeNS(xmldoc.namespaceURI, B_FIN, str(fin)) 804 xmlInsert.appendChild(node) 805 xmlTransfo.appendChild(xmlInsert) 806 # Suppressions 807 xmlSupp = xmldoc.createElementNS(xmldoc.namespaceURI, B_SUPPRESSIONS) 808 listeSupp = pResultat.getListeSuppressions() 809 for deb,fin in listeSupp : 810 node = xmldoc.createElementNS(xmldoc.namespaceURI, B_SUP) 811 node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) 812 node.setAttributeNS(node.namespaceURI, B_FIN, str(fin)) 813 xmlSupp.appendChild(node) 814 xmlTransfo.appendChild(xmlSupp) 815 # Deplacements 816 xmlDepl = xmldoc.createElementNS(xmldoc.namespaceURI, B_DEPLACEMENTS) 817 listeDepl = pResultat.getListeDeplacements() 818 for deb,fin in listeDepl : 819 node = xmldoc.createElementNS(xmldoc.namespaceURI, B_DEP) 820 node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) 821 node.setAttributeNS(node.namespaceURI, B_FIN, str(fin)) 822 xmlDepl.appendChild(node) 823 xmlTransfo.appendChild(xmlDepl) 824 # Remplacement 825 xmlRemp = xmldoc.createElementNS(xmldoc.namespaceURI, B_REMPLACEMENTS) 826 listeRemp = pResultat.getListeRemplacements() 827 for deb,fin in listeRemp : 828 node = xmldoc.createElementNS(xmldoc.namespaceURI, B_REMP) 829 node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) 830 node.setAttributeNS(node.namespaceURI, B_FIN, str(fin)) 831 xmlRemp.appendChild(node) 832 xmlTransfo.appendChild(xmlRemp) 833 #blocs communs 834 xmlBlocsCom = xmldoc.createElementNS(xmldoc.namespaceURI, B_BLOCSCOMMUNS) 835 listeBlocsCommuns = pResultat.getBlocsCommuns() 836 for deb,fin in listeBlocsCommuns : 837 node = xmldoc.createElementNS(xmldoc.namespaceURI, B_BC) 838 node.setAttributeNS(node.namespaceURI, B_DEB, str(deb)) 839 node.setAttributeNS(node.namespaceURI, B_FIN, str(fin)) 840 xmlBlocsCom.appendChild(node) 841 xmlTransfo.appendChild(xmlBlocsCom) 842 # blocs déplacés 843 xmlBlocsDep = xmldoc.createElementNS(xmldoc.namespaceURI, B_BLOCSDEPLACES) 844 listeBlocsDep = pResultat.getPairesBlocsDeplaces() 845 for (b1deb,b1fin),(b2deb,b2fin) in listeBlocsDep : 846 node = xmldoc.createElementNS(xmldoc.namespaceURI, B_DEP) 847 node.setAttributeNS(node.namespaceURI, B1_D, str(b1deb)) 848 node.setAttributeNS(node.namespaceURI, B1_F, str(b1fin)) 849 node.setAttributeNS(node.namespaceURI, B2_D, str(b2deb)) 850 node.setAttributeNS(node.namespaceURI, B2_F, str(b2fin)) 851 xmlBlocsDep.appendChild(node) 852 xmlTransfo.appendChild(xmlBlocsDep) 853 # Insertion de la balise B_TRANSFORMATIONS dans la balise B_INFORMATIONS 854 xmltagInfo.appendChild(xmlTransfo) 855 return xmltagInfo
856 857
858 - def __creerCommentaire(self, pCommentaire):
859 """ Methode privee qui creer le noeud <commentaire> a inserer dans le fichier xml 860 pre: isinstance(pCommentaire,Donnees.commentaire.Commentaire) 861 post: isinstance(__return__, xml.dom.minidom.Node) """ 862 comment = pCommentaire.getCommentaire() 863 xmldoc = Document() 864 xmltagComment = xmldoc.createElement(B_COMMENTAIRE) 865 xmltagComment.appendChild(xmldoc.createTextNode(comment)) 866 return xmltagComment
867
868 - def __getInformations(self,nodeInfos, pNom, pTitre):
869 """ On recupere dans fic les informations du fichier Informations.xml dont 870 le chemin est passe en parametre 871 pre: isinstance(nodeInfos, xml.dom.minidom.Node) and \ 872 isinstance(pNom,str) and isinstance(pTitre,str) """ 873 logging.debug('begin __getInformations') 874 pVersionSource = nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_VERS_SOURCE) 875 pEtatSource = nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_ETAT_SOURCE) 876 pVersionCible = nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_VERS_CIBLE) 877 pEtatCible = nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_ETAT_CIBLE) 878 pParametres = Parametres(atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_1)), atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_2)), 879 atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_3)),atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_4)), 880 atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_5)),atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_6)), 881 atoi(nodeInfos.getAttributeNS(nodeInfos.namespaceURI,B_PARAM_7))) 882 # on concatène les 2 fichiers 883 pathSource=self.dgm.getEtatPath(pNom,pTitre,pVersionSource,pEtatSource) 884 pathCible=self.dgm.getEtatPath(pNom,pTitre,pVersionCible,pEtatCible) 885 fs = open(pathSource) 886 fc = open(pathCible) 887 textesConca = fs.read() + fc.read() 888 fs.close() 889 fc.close() 890 ## creer un objet de type 'Transformations' 891 #i = nodeInfos.getElementsByTagName(B_LGSOURCE) 892 #lgsource=0 893 #if i != []: 894 # lgsource = int(i[0].getAttribute(B_LG)) 895 lgsource = int(nodeInfos.xpath(u'string('+B_TRANSFORMATIONS+'/'+B_LGSOURCE+'/@' +B_LG+')')) 896 #print lgsource 897 listInsert = [] 898 #i = nodeInfos.getElementsByTagName(B_INSERTIONS) 899 #if i != [] : 900 # li = i[0].getElementsByTagName(B_INS) 901 # for m in li : 902 # listInsert.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 903 #nodeInsertions = nodeInfos.xpath(u'string(//'+B_INSERTIONS+')') 904 n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_INSERTIONS+')')[0] 905 #print n.localName 906 if n.localName == B_INSERTIONS: 907 #for child in Ft.Xml.XPath.Evaluate(u'('+B_INS+')',n): 908 for child in n.xpath(u'('+B_INS+')'): 909 listInsert.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) 910 # print (int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN))) 911 #sys.stdout.flush() 912 listSupp = [] 913 #s = nodeInfos.getElementsByTagName(B_SUPPRESSIONS) 914 #if s != [] : 915 # ls = s[0].getElementsByTagName(B_SUP) 916 # for m in ls : 917 # listSupp.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 918 n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_SUPPRESSIONS+')')[0] 919 if n.localName == B_SUPPRESSIONS: 920 #for child in Ft.Xml.XPath.Evaluate(u'(/'+B_SUP+')',n): 921 for child in n.xpath(u'('+B_SUP+')'): 922 listSupp.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) 923 924 listDepl = [] 925 #d = nodeInfos.getElementsByTagName(B_DEPLACEMENTS) 926 #if d != []: 927 # ld = d[0].getElementsByTagName(B_DEP) 928 # for m in ld : 929 # listDepl.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 930 n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_DEPLACEMENTS+')')[0] 931 if n.localName == B_DEPLACEMENTS: 932 #for child in Ft.Xml.XPath.Evaluate(u'(//'+B_DEP+')',contextNode=n): 933 #for child in n.xpath(u'(/'+B_DEP+')'): 934 for child in n.xpath(u'('+B_DEP+')'): 935 #print child 936 try: 937 listDepl.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) 938 except Exception,e: 939 print e 940 print child 941 print child.localName 942 print child.attributes 943 sys.stdout.flush() 944 sys.exit() 945 946 listRemp = [] 947 #r = nodeInfos.getElementsByTagName(B_REMPLACEMENTS) 948 #if r != []: 949 # lr = r[0].getElementsByTagName(B_REMP) 950 # for m in lr : 951 # listRemp.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 952 n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_REMPLACEMENTS+')')[0] 953 if n.localName == B_REMPLACEMENTS: 954 #for child in Ft.Xml.XPath.Evaluate(u'(/'+B_REMP+')',n): 955 for child in n.xpath(u'('+B_REMP+')'): 956 listRemp.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) 957 958 listBlocCom = [] 959 #r = nodeInfos.getElementsByTagName(B_BLOCSCOMMUNS) 960 #if r != []: 961 # lr = r[0].getElementsByTagName(B_BC) 962 # for m in lr : 963 # listBlocCom.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 964 n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_BLOCSCOMMUNS+')')[0] 965 if n.localName == B_BLOCSCOMMUNS: 966 #for child in Ft.Xml.XPath.Evaluate(u'(/'+B_BC+')',n): 967 for child in n.xpath(u'('+B_BC+')'): 968 listBlocCom.append((int(child.getAttributeNS(child.namespaceURI,B_DEB)),int(child.getAttributeNS(child.namespaceURI,B_FIN)))) 969 970 listBlocDep = [] 971 #r = nodeInfos.getElementsByTagName(B_BLOCSDEPLACES) 972 #if r != []: 973 # lr = r[0].getElementsByTagName(B_DEP) 974 # for m in lr : 975 # listBlocDep.append(((int(m.getAttribute(B1_D)),int(m.getAttribute(B1_F))), 976 # (int(m.getAttribute(B2_D)),int(m.getAttribute(B2_F))))) 977 n = nodeInfos.xpath(u'('+B_TRANSFORMATIONS+'/'+B_BLOCSDEPLACES+')')[0] 978 if n.localName == B_BLOCSDEPLACES: 979 #for child in Ft.Xml.XPath.Evaluate(u'(/'+B_DEP+')',n): 980 for child in n.xpath(u'('+B_DEP+')'): 981 listBlocDep.append(((int(child.getAttributeNS(child.namespaceURI,B1_D)),int(child.getAttributeNS(child.namespaceURI,B1_F))), 982 (int(child.getAttributeNS(child.namespaceURI,B2_D)),int(child.getAttributeNS(child.namespaceURI,B2_F))))) 983 984 #transfo = Transformations(listInsert, listSupp, listDepl, listRemp, textSource, textCible) 985 res = Resultat(listInsert, listSupp, listDepl, listRemp,lgsource,textesConca,listBlocCom,listBlocDep) 986 ## creer un objet de type 'Commentaire' si la balise B_COMMENTAIRE existe 987 #c = nodeInfos.getElementsByTagName(B_COMMENTAIRE) 988 #if c != []: 989 # comment = Commentaire((c[0]._get_firstChild()).toxml()) 990 #else : 991 # comment = Commentaire('') 992 c = Ft.Xml.XPath.Evaluate(u'(//'+B_COMMENTAIRE+')', contextNode=nodeInfos) 993 if len(c) != 0: 994 comment = Commentaire(c[0].nodeValue) 995 else: 996 comment = Commentaire('') 997 logging.debug('fin __getInformations') 998 ## creer et retourner un objet du type 'Informations' 999 return Informations(pVersionSource, pEtatSource, pVersionCible, 1000 pEtatCible,pParametres, res, comment)
1001
1002 - def __getInformations_(self,nodeInfos, pNom, pTitre):
1003 """ 1004 Avec des Numeric.array plutot que des listes 1005 On recupere dans fic les informations du fichier Informations.xml dont 1006 le chemin est passe en parametre 1007 pre: isinstance(nodeInfos, xml.dom.minidom.Node) and \ 1008 isinstance(pNom,str) and isinstance(pTitre,str) """ 1009 logging.debug('begin __getInformations') 1010 pVersionSource = nodeInfos.getAttribute(B_VERS_SOURCE) 1011 pEtatSource = nodeInfos.getAttribute(B_ETAT_SOURCE) 1012 pVersionCible = nodeInfos.getAttribute(B_VERS_CIBLE) 1013 pEtatCible = nodeInfos.getAttribute(B_ETAT_CIBLE) 1014 pParametres = Parametres(atoi(nodeInfos.getAttribute(B_PARAM_1)), atoi(nodeInfos.getAttribute(B_PARAM_2)), 1015 atoi(nodeInfos.getAttribute(B_PARAM_3)),atoi(nodeInfos.getAttribute(B_PARAM_4)), 1016 atoi(nodeInfos.getAttribute(B_PARAM_5)),atoi(nodeInfos.getAttribute(B_PARAM_6)), 1017 atoi(nodeInfos.getAttribute(B_PARAM_7))) 1018 # on concatène les 2 fichiers 1019 pathSource=self.dgm.getEtatPath(pNom,pTitre,pVersionSource,pEtatSource) 1020 pathCible=self.dgm.getEtatPath(pNom,pTitre,pVersionCible,pEtatCible) 1021 fs = open(pathSource) 1022 fc = open(pathCible) 1023 textesConca = fs.read() + fc.read() 1024 fs.close() 1025 fc.close() 1026 ## creer un objet de type 'Transformations' 1027 i = nodeInfos.getElementsByTagName(B_LGSOURCE) 1028 lgsource=0 1029 if i != []: 1030 lgsource = int(i[0].getAttribute(B_LG)) 1031 1032 #listInsert = [] 1033 i = nodeInfos.getElementsByTagName(B_INSERTIONS) 1034 if i != [] : 1035 li = i[0].getElementsByTagName(B_INS) 1036 nb = len(li) ; i = 0 1037 tabIns = Numeric.zeros((nb,2), Numeric.Int16) 1038 for m in li : 1039 #listInsert.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 1040 tabIns[i][0] = int(m.getAttribute(B_DEB)) 1041 tabIns[i][1] = int(m.getAttribute(B_FIN)) 1042 i += 1 1043 1044 #listSupp = [] 1045 s = nodeInfos.getElementsByTagName(B_SUPPRESSIONS) 1046 if s != [] : 1047 ls = s[0].getElementsByTagName(B_SUP) 1048 nb = len(ls) ; i = 0 1049 tabSup = Numeric.zeros((nb,2), Numeric.Int16) 1050 for m in ls : 1051 #listSupp.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 1052 tabSup[i][0] = int(m.getAttribute(B_DEB)) 1053 tabSup[i][1] = int(m.getAttribute(B_FIN)) 1054 i += 1 1055 1056 #listDepl = [] 1057 d = nodeInfos.getElementsByTagName(B_DEPLACEMENTS) 1058 if d != []: 1059 ld = d[0].getElementsByTagName(B_DEP) 1060 nb = len(ld) ; i = 0 1061 tabDep = Numeric.zeros((nb,2), Numeric.Int16) 1062 for m in ld : 1063 #listDepl.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 1064 tabDep[i][0] = int(m.getAttribute(B_DEB)) 1065 tabDep[i][1] = int(m.getAttribute(B_FIN)) 1066 i += 1 1067 1068 #listRemp = [] 1069 r = nodeInfos.getElementsByTagName(B_REMPLACEMENTS) 1070 if r != []: 1071 lr = r[0].getElementsByTagName(B_REMP) 1072 nb = len(lr) ; i = 0 1073 tabRemp = Numeric.zeros((nb,2), Numeric.Int16) 1074 for m in lr : 1075 #listRemp.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 1076 tabRemp[i][0] = int(m.getAttribute(B_DEB)) 1077 tabRemp[i][1] = int(m.getAttribute(B_FIN)) 1078 i += 1 1079 1080 #listBlocCom = [] 1081 r = nodeInfos.getElementsByTagName(B_BLOCSCOMMUNS) 1082 if r != []: 1083 lr = r[0].getElementsByTagName(B_BC) 1084 nb = len(lr) ; i = 0 1085 tabBC = Numeric.zeros((nb,2), Numeric.Int16) 1086 for m in lr : 1087 #listBlocCom.append((int(m.getAttribute(B_DEB)),int(m.getAttribute(B_FIN)))) 1088 tabBC[i][0] = int(m.getAttribute(B_DEB)) 1089 tabBC[i][1] = int(m.getAttribute(B_FIN)) 1090 i += 1 1091 1092 #listBlocDep = [] 1093 r = nodeInfos.getElementsByTagName(B_BLOCSDEPLACES) 1094 if r != []: 1095 lr = r[0].getElementsByTagName(B_DEP) 1096 nb = len(lr) ; i = 0 1097 tabBD = Numeric.zeros((nb,2,2), Numeric.Int16) 1098 for m in lr : 1099 #listBlocDep.append(((int(m.getAttribute(B1_D)),int(m.getAttribute(B1_F))), 1100 # (int(m.getAttribute(B2_D)),int(m.getAttribute(B2_F))))) 1101 tabBD[i][0][0] = int(m.getAttribute(B1_D)) 1102 tabBD[i][0][1] = int(m.getAttribute(B1_F)) 1103 tabBD[i][1][0] = int(m.getAttribute(B2_D)) 1104 tabBD[i][1][1] = int(m.getAttribute(B2_F)) 1105 i += 1 1106 1107 #transfo = Transformations(listInsert, listSupp, listDepl, listRemp, textSource, textCible) 1108 #res = Resultat(listInsert, listSupp, listDepl, listRemp,lgsource,textesConca,listBlocCom,listBlocDep) 1109 res = Resultat(tabIns, tabSup, tabDep, tabRemp,lgsource,textesConca, tabBC, tabBD) 1110 #listInsert = None; listSupp = None; listDepl = None; listRemp = None;listBlocCom = None;listBlocDep = None 1111 ## creer un objet de type 'Commentaire' si la balise B_COMMENTAIRE existe 1112 c = nodeInfos.getElementsByTagName(B_COMMENTAIRE) 1113 if c != []: 1114 comment = Commentaire((c[0]._get_firstChild()).toxml()) 1115 else : 1116 comment = Commentaire('') 1117 logging.debug('fin __getInformations') 1118 ## creer et retourner un objet du type 'Informations' 1119 return Informations(pVersionSource, pEtatSource, pVersionCible, 1120 pEtatCible,pParametres, res, comment)
1121
1122 - def enregistrerTransformationsStream(self, planTravail, pResultat):
1123 """ enregistre un fichier xml par streaming 1124 1125 évite de construire un DOM complet ce qui consomme trop de memoire pour 1126 de gros fichiers source: taille des 2 fichiers supérieure à 3.5 Mo 1127 écrase le fichier existant éventuel 1128 toutes les chaines écrites doivent être en unicode (utf-8 par défaut) 1129 """ 1130 def u(x): 1131 return unicode(x)
1132 nomDG = self.dgm.getDG(planTravail.getAuteur(), planTravail.getOeuvre()) 1133 fim = Controleurs.FInfoManager.FInfoManager(Controleurs.DGManager.Auteur(planTravail.getAuteur()), 1134 Controleurs.DGManager.Oeuvre(planTravail.getOeuvre())) 1135 pParametres = planTravail.getParam() 1136 output_file = file(fim.getFileInfo(), 'w') 1137 writer = MarkupWriter(output_file, indent=u"yes") 1138 writer.startDocument() 1139 writer.startElement(B_INFORMATIONS, attributes={ 1140 B_VERS_SOURCE : u(planTravail.getVersionSource()), 1141 B_ETAT_SOURCE : u(planTravail.getEtatSource()), 1142 B_VERS_CIBLE : u(planTravail.getVersionCible()), 1143 B_ETAT_CIBLE : u(planTravail.getEtatCible()), 1144 B_PARAM_1 : u('%s'%pParametres.getp1()), 1145 B_PARAM_2 : u('%s'%pParametres.getp2()), 1146 B_PARAM_3 : u('%s'%pParametres.getp3()), 1147 B_PARAM_4 : u('%s'%pParametres.getp4()), 1148 B_PARAM_5 : u('%s'%pParametres.getp5()), 1149 B_PARAM_6 : u('%s'%pParametres.getp6()), 1150 B_PARAM_7 : u('%s'%pParametres.getp7())}) 1151 # Balise Transformations 1152 writer.startElement(B_TRANSFORMATIONS) 1153 writer.simpleElement(B_LGSOURCE, attributes={B_LG : u(pResultat.getLgSource())}) 1154 # Insertions 1155 writer.startElement(B_INSERTIONS) 1156 listeInsert = pResultat.getListeInsertions() 1157 for deb,fin in listeInsert : 1158 writer.simpleElement(B_INS, attributes={B_DEB : u(deb), B_FIN : u(fin)}) 1159 writer.endElement(B_INSERTIONS) 1160 # Suppressions 1161 writer.startElement(B_SUPPRESSIONS) 1162 listeSupp = pResultat.getListeSuppressions() 1163 for deb,fin in listeSupp : 1164 writer.simpleElement(B_SUP, attributes={B_DEB : u(deb), B_FIN : u(fin)}) 1165 writer.endElement(B_SUPPRESSIONS) 1166 # Deplacements 1167 writer.startElement(B_DEPLACEMENTS) 1168 listeDepl = pResultat.getListeDeplacements() 1169 for deb,fin in listeDepl : 1170 writer.simpleElement(B_DEP, attributes={B_DEB : u(deb), B_FIN : u(fin)}) 1171 writer.endElement(B_DEPLACEMENTS) 1172 # Remplacement 1173 writer.startElement(B_REMPLACEMENTS) 1174 listeRemp = pResultat.getListeRemplacements() 1175 for deb,fin in listeRemp : 1176 writer.simpleElement(B_REMP, attributes={B_DEB : u(deb), B_FIN : u(fin)}) 1177 writer.endElement(B_REMPLACEMENTS) 1178 #blocs communs 1179 writer.startElement(B_BLOCSCOMMUNS) 1180 listeBlocsCommuns = pResultat.getBlocsCommuns() 1181 for deb,fin in listeBlocsCommuns : 1182 writer.simpleElement(B_BC, attributes={B_DEB : u(deb), B_FIN : u(fin)}) 1183 writer.endElement(B_BLOCSCOMMUNS) 1184 # blocs déplacés 1185 writer.startElement(B_BLOCSDEPLACES) 1186 listeBlocsDep = pResultat.getPairesBlocsDeplaces() 1187 for (b1deb,b1fin),(b2deb,b2fin) in listeBlocsDep : 1188 writer.simpleElement(B_DEP, attributes={B1_D : u(b1deb), B1_F : u(b1fin), 1189 B2_D : u(b2deb), B2_F : u(b2fin)}) 1190 writer.endElement(B_BLOCSDEPLACES) 1191 writer.endElement(B_TRANSFORMATIONS) 1192 writer.endElement(B_INFORMATIONS) 1193 writer.endDocument()
1194
1195 -class InformationsContentHandler(ContentHandler):
1196 - def __init__(self,name):
1197 self.look_for = name 1198 self.is_name, self.is_mobile = None, None 1199 self.buffer = ''
1200 - def startElement(self,name,attrs):
1201 if name == 'phone' and attrs.get('type') == 'mobile': 1202 self.is_mobile = 1 1203 elif name == 'name': self.is_name = 1
1204 - def endElement(self,name):
1205 if self.is_name: 1206 self.current_name = self.buffer.strip() 1207 self.buffer = '' 1208 self.is_name = None 1209 elif self.is_mobile and self.current_name == self.look_for: 1210 self.mobile = self.buffer 1211 raise SAXException('Found mobile phone') # stop parsing
1212 - def characters(self,chars):
1213 if self.is_name or self.is_mobile: self.buffer += chars
1214 - def find_mobile_phone(name):
1215 handler = PhoneContentHandler(name) 1216 parser = make_parser() 1217 parser.setContentHandler(handler) 1218 try: 1219 parser.parse(open('addressbook.xml')) 1220 except SAXException: 1221 return handler.mobile 1222 return None
1223 1224 if __name__ == '__main__': 1225 import sys 1226 name = ' '.join(sys.argv[1:]) 1227 phone = find_mobile_phone(name) 1228 if phone: 1229 print 'Mobile phone is',phone 1230 else: 1231 print 'No mobile phone found for',name
1232