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

Source Code for Module medite.Controleurs.FInfoManager

  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  import os,os.path 
 20  import xml.dom.minidom 
 21  import Utile.constantesDonnees, Utile.codageXml 
 22  from Utile.constantesDonnees import * 
 23  from Utile.codageXml import encodingNode 
 24  import DGManager 
 25  import Donnees.arbre 
 26   
27 -class FInfoManager(object) :
28 - def __init__(self,auteur,oeuvre):
29 """ pre: isinstance(auteur,DGManager.Auteur) and isinstance(oeuvre,DGManager.Oeuvre)""" 30 self.auteur = auteur 31 self.oeuvre = oeuvre 32 self.dgm = DGManager.DGManager() 33 self.nomDG = self.dgm.getDG(auteur.getNom(),oeuvre.getTitre()) 34 self.fileInfo = os.path.join(self.nomDG,Utile.constantesDonnees.DG_FILE_INFO)
35 - def getFileInfo(self):
36 return self.fileInfo
37 - def creerFichierInformations(self):
38 """ Crée le fichier Informations.xml """ 39 # creation du document XML 40 xmldoc = self.__creerDocument(self.auteur,self.oeuvre) 41 # Ecriture sur le fichier 42 Utile.codageXml.ecrireDocXml(self.fileInfo,xmldoc)
43 - def creerVersion(self, nomVersion):
44 """ Crée une balise version dans le fichier 45 pre: isinstance(nomVersion,str) """ 46 xmlVersion = self.__creerVersion(nomVersion) 47 fic = Utile.codageXml.lireDocXml(self.fileInfo) 48 arbre = fic.getElementsByTagName(B_ARBRE)[0] # renvoie un unique element arbre 49 arbre.appendChild(xmlVersion) 50 Utile.codageXml.ecrireDocXml(self.fileInfo,fic)
51 - def supprimerVersion(self, nomVersion):
52 """ Crée une balise version dans le fichier 53 pre: isinstance(nomVersion,str) """ 54 fic = Utile.codageXml.lireDocXml(self.fileInfo) 55 larbre = fic.getElementsByTagName(B_ARBRE) 56 lVersions = larbre[0].getElementsByTagName(B_VERSION) 57 for version in lVersions: 58 if version.getAttribute(B_ID)==nomVersion: 59 # suppression du noeud 60 larbre[0].removeChild(version) 61 Utile.codageXml.ecrireDocXml(self.fileInfo,fic) 62 return
63 - def creerEtat(self,nomVersion, nomEtat, etatPere=""):
64 """ Crée une balise état dans le fichier 65 pre: isinstance(nomVersion,str) and isinstance(nomEtat,str) and isinstance(etatPere,str)""" 66 xmlEtat = self.__creerEtat(nomEtat) 67 fic = Utile.codageXml.lireDocXml(self.fileInfo) 68 for version in fic.getElementsByTagName(B_VERSION): 69 if version.getAttribute(B_ID) == nomVersion: 70 if etatPere == "": 71 version.appendChild(xmlEtat) 72 Utile.codageXml.ecrireDocXml(self.fileInfo,fic) 73 return 74 else: 75 for etat in version.getElementsByTagName(B_ETAT): 76 if etat.getAttribute(B_ID) == etatPere: 77 etat.appendChild(xmlEtat) 78 Utile.codageXml.ecrireDocXml(self.fileInfo,fic) 79 return
80
81 - def supprimerEtat(self, nomVersion, nomEtat):
82 """ Supprime un état 83 pre: isinstance(nomVersion,str) and isinstance(nomEtat,str) """ 84 fic = Utile.codageXml.lireDocXml(self.fileInfo) 85 lVersions = fic.getElementsByTagName(B_VERSION) 86 for version in lVersions: 87 if version.getAttribute(B_ID) == nomVersion: 88 for etat in version.getElementsByTagName(B_ETAT): 89 if etat.getAttribute(B_ID) == nomEtat: 90 # pas forcément version car un état peut avoir une autre version comme parent 91 parent = etat.parentNode 92 parent.removeChild(etat) 93 Utile.codageXml.ecrireDocXml(self.fileInfo,fic) 94 return
95
96 - def getListeEtatsFils(self,nomVersion, nomEtat):
97 """ Retourne le nom des ID des etats fils d'un état qui correspondent aux noms De fichier 98 pre: isinstance(nomVersion,str) and isinstance(nomEtat,str) 99 post isinstance(__return__,list) """ 100 lef = [] 101 fic = Utile.codageXml.lireDocXml(self.fileInfo) 102 larbre = fic.getElementsByTagName(B_ARBRE) 103 for version in larbre[0].getElementsByTagName(B_VERSION): 104 if version.getAttribute(B_ID) == nomVersion: 105 for etat in version.getElementsByTagName(B_ETAT): 106 if etat.getAttribute(B_ID) == nomEtat: 107 lEtatsFils = etat.getElementsByTagName(B_ETAT) 108 for etatFils in lEtatsFils: 109 lef.append(etatFils.getAttribute(B_ID)) 110 return lef 111 return []
112
113 - def getArbre(self):
114 """ Renvoie l'arbre en xml d'un DG 115 En fait renvoie que l'arbre des versions car c'est seulement ce dont on a besoin 116 cela permet de ne pas construire tout l'arbre xlm lorsque le fichier est très gros (plusieurs Mo) 117 post: isinstance(__return__,Donnees.arbre.Arbre)""" 118 lFils = [] 119 fic = Utile.codageXml.lireDocXmlVersionTree(self.fileInfo) 120 larbre = fic.getElementsByTagName(B_ARBRE) 121 for version in larbre[0].getElementsByTagName(B_VERSION): 122 lFils.append(self.__getFils(version)) 123 return Donnees.arbre.Arbre(lFils)
124
125 - def __getFils(self,pere):
126 """ Lit les fils d'un noeud version ou état et retourne ce noeud en tant que Version ou Etat 127 post: isinstance(__return__,Donnees.arbre.Version) or isinstance(__return__,Donnees.arbre.Etat) """ 128 lFils = [] 129 name = pere.getAttribute(B_ID) 130 for fils in pere.childNodes: 131 lFils.append(self.__getFils(fils)) 132 if pere.tagName == B_VERSION: 133 return Donnees.arbre.Version(name, lFils) 134 else: 135 return Donnees.arbre.Etat(name, lFils)
136
137 - def __creerDocument(self, pAuteur, pOeuvre):
138 """ Methode qui cree le document xml 139 pre: isinstance(pAuteur,DGManager.Auteur) and isinstance(pOeuvre,DGManager.Oeuvre)""" 140 xmldoc = xml.dom.minidom.Document() 141 # Balise root 142 xmltagRoot = xmldoc.createElement(B_ROOT) 143 # Balise Auteur 144 xmltagAuteur = self.__baliseAuteur(pAuteur) 145 xmltagRoot.appendChild(xmltagAuteur) 146 # Balise Oeuvre 147 xmltagOeuvre = self.__baliseOeuvre(pOeuvre) 148 xmltagRoot.appendChild(xmltagOeuvre) 149 # Balise Arbre 150 xmltagArbre = self.__baliseArbre() 151 xmltagRoot.appendChild(xmltagArbre) 152 xmldoc.appendChild(xmltagRoot) 153 return xmldoc
154
155 - def __baliseAuteur(self, pAuteur):
156 """ Methode privee qui construit la balise Auteur 157 pre: isinstance(pAuteur,DGManager.Auteur) """ 158 xmldoc = xml.dom.minidom.Document() 159 # Balise Auteur 160 xmltagAuteur = xmldoc.createElement(B_AUTEUR) 161 # nom 162 xmlNom = xmldoc.createElement(B_NOM) 163 nomA = str(pAuteur.getNom()) 164 nom = xmldoc.createTextNode(encodingNode(nomA)) 165 xmlNom.appendChild(nom) 166 # prenom 167 xmlPrenom = xmldoc.createElement(B_PRENOM) 168 prenomA = str(pAuteur.getPrenom()) 169 prenom = xmldoc.createTextNode(encodingNode(prenomA)) 170 xmlPrenom.appendChild(prenom) 171 # naissance 172 xmlNaissance = xmldoc.createElement(B_NAISSANCE) 173 naisA = str(pAuteur.getNaissance()) 174 naissance = xmldoc.createTextNode(encodingNode(naisA)) 175 xmlNaissance.appendChild(naissance) 176 # deces 177 xmlDeces = xmldoc.createElement(B_DECES) 178 decA = str(pAuteur.getDeces()) 179 deces = xmldoc.createTextNode(encodingNode(decA)) 180 xmlDeces.appendChild(deces) 181 182 xmltagAuteur.appendChild(xmlNom) 183 xmltagAuteur.appendChild(xmlPrenom) 184 xmltagAuteur.appendChild(xmlNaissance) 185 xmltagAuteur.appendChild(xmlDeces) 186 return xmltagAuteur
187
188 - def __baliseOeuvre(self, pOeuvre):
189 """ Methode privee qui construit la balise Oeuvre 190 pre: isinstance(pOeuvre,DGManager.Oeuvre) """ 191 xmldoc = xml.dom.minidom.Document() 192 # Balise Oeuvre 193 xmltagOeuvre = xmldoc.createElement(B_OEUVRE) 194 # titre 195 xmlTitre = xmldoc.createElement(B_TITRE) 196 titreO = pOeuvre.getTitre() 197 titre = xmldoc.createTextNode(encodingNode(str(titreO))) 198 xmlTitre.appendChild(titre) 199 # edition 200 xmlEdition = xmldoc.createElement(B_EDITION) 201 editO = pOeuvre.getEdition() 202 edition = xmldoc.createTextNode(encodingNode(str(editO))) 203 xmlEdition.appendChild(edition) 204 # publication 205 xmlPublication = xmldoc.createElement(B_PUBLICATION) 206 pubO = pOeuvre.getPublication() 207 publication = xmldoc.createTextNode(encodingNode(str(pubO))) 208 xmlPublication.appendChild(publication) 209 210 xmltagOeuvre.appendChild(xmlTitre) 211 xmltagOeuvre.appendChild(xmlEdition) 212 xmltagOeuvre.appendChild(xmlPublication) 213 return xmltagOeuvre
214
215 - def __baliseArbre(self):
216 xmldoc = xml.dom.minidom.Document() 217 xmltagArbre = xmldoc.createElement(B_ARBRE) 218 return xmltagArbre
219
220 - def __creerVersion(self,pName):
221 xmldoc = xml.dom.minidom.Document() 222 xmltagVersion = xmldoc.createElement(B_VERSION) 223 xmltagVersion.setAttribute(B_ID,pName) 224 return xmltagVersion
225
226 - def __creerEtat(self,pName):
227 xmldoc = xml.dom.minidom.Document() 228 xmltagEtat = xmldoc.createElement(B_ETAT) 229 xmltagEtat.setAttribute(B_ID,pName) 230 return xmltagEtat
231