# -*- 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 import os,os.path import xml.dom.minidom import Utile.constantesDonnees, Utile.codageXml from Utile.constantesDonnees import * from Utile.codageXml import encodingNode import DGManager import Donnees.arbre class FInfoManager(object) : def __init__(self,auteur,oeuvre): """ pre: isinstance(auteur,DGManager.Auteur) and isinstance(oeuvre,DGManager.Oeuvre)""" self.auteur = auteur self.oeuvre = oeuvre self.dgm = DGManager.DGManager() self.nomDG = self.dgm.getDG(auteur.getNom(),oeuvre.getTitre()) self.fileInfo = os.path.join(self.nomDG,Utile.constantesDonnees.DG_FILE_INFO) def getFileInfo(self): return self.fileInfo def creerFichierInformations(self): """ Crée le fichier Informations.xml """ # creation du document XML xmldoc = self.__creerDocument(self.auteur,self.oeuvre) # Ecriture sur le fichier Utile.codageXml.ecrireDocXml(self.fileInfo,xmldoc) def creerVersion(self, nomVersion): """ Crée une balise version dans le fichier pre: isinstance(nomVersion,str) """ xmlVersion = self.__creerVersion(nomVersion) fic = Utile.codageXml.lireDocXml(self.fileInfo) arbre = fic.getElementsByTagName(B_ARBRE)[0] # renvoie un unique element arbre arbre.appendChild(xmlVersion) Utile.codageXml.ecrireDocXml(self.fileInfo,fic) def supprimerVersion(self, nomVersion): """ Crée une balise version dans le fichier pre: isinstance(nomVersion,str) """ fic = Utile.codageXml.lireDocXml(self.fileInfo) larbre = fic.getElementsByTagName(B_ARBRE) lVersions = larbre[0].getElementsByTagName(B_VERSION) for version in lVersions: if version.getAttribute(B_ID)==nomVersion: # suppression du noeud larbre[0].removeChild(version) Utile.codageXml.ecrireDocXml(self.fileInfo,fic) return def creerEtat(self,nomVersion, nomEtat, etatPere=""): """ Crée une balise état dans le fichier pre: isinstance(nomVersion,str) and isinstance(nomEtat,str) and isinstance(etatPere,str)""" xmlEtat = self.__creerEtat(nomEtat) fic = Utile.codageXml.lireDocXml(self.fileInfo) for version in fic.getElementsByTagName(B_VERSION): if version.getAttribute(B_ID) == nomVersion: if etatPere == "": version.appendChild(xmlEtat) Utile.codageXml.ecrireDocXml(self.fileInfo,fic) return else: for etat in version.getElementsByTagName(B_ETAT): if etat.getAttribute(B_ID) == etatPere: etat.appendChild(xmlEtat) Utile.codageXml.ecrireDocXml(self.fileInfo,fic) return def supprimerEtat(self, nomVersion, nomEtat): """ Supprime un état pre: isinstance(nomVersion,str) and isinstance(nomEtat,str) """ fic = Utile.codageXml.lireDocXml(self.fileInfo) lVersions = fic.getElementsByTagName(B_VERSION) for version in lVersions: if version.getAttribute(B_ID) == nomVersion: for etat in version.getElementsByTagName(B_ETAT): if etat.getAttribute(B_ID) == nomEtat: # pas forcément version car un état peut avoir une autre version comme parent parent = etat.parentNode parent.removeChild(etat) Utile.codageXml.ecrireDocXml(self.fileInfo,fic) return def getListeEtatsFils(self,nomVersion, nomEtat): """ Retourne le nom des ID des etats fils d'un état qui correspondent aux noms de fichier pre: isinstance(nomVersion,str) and isinstance(nomEtat,str) post isinstance(__return__,list) """ lef = [] fic = Utile.codageXml.lireDocXml(self.fileInfo) larbre = fic.getElementsByTagName(B_ARBRE) for version in larbre[0].getElementsByTagName(B_VERSION): if version.getAttribute(B_ID) == nomVersion: for etat in version.getElementsByTagName(B_ETAT): if etat.getAttribute(B_ID) == nomEtat: lEtatsFils = etat.getElementsByTagName(B_ETAT) for etatFils in lEtatsFils: lef.append(etatFils.getAttribute(B_ID)) return lef return [] def getArbre(self): """ Renvoie l'arbre en xml d'un DG En fait renvoie que l'arbre des versions car c'est seulement ce dont on a besoin cela permet de ne pas construire tout l'arbre xlm lorsque le fichier est très gros (plusieurs Mo) post: isinstance(__return__,Donnees.arbre.Arbre)""" lFils = [] fic = Utile.codageXml.lireDocXmlVersionTree(self.fileInfo) larbre = fic.getElementsByTagName(B_ARBRE) for version in larbre[0].getElementsByTagName(B_VERSION): lFils.append(self.__getFils(version)) return Donnees.arbre.Arbre(lFils) def __getFils(self,pere): """ Lit les fils d'un noeud version ou état et retourne ce noeud en tant que Version ou Etat post: isinstance(__return__,Donnees.arbre.Version) or isinstance(__return__,Donnees.arbre.Etat) """ lFils = [] name = pere.getAttribute(B_ID) for fils in pere.childNodes: lFils.append(self.__getFils(fils)) if pere.tagName == B_VERSION: return Donnees.arbre.Version(name, lFils) else: return Donnees.arbre.Etat(name, lFils) def __creerDocument(self, pAuteur, pOeuvre): """ Methode qui cree le document xml pre: isinstance(pAuteur,DGManager.Auteur) and isinstance(pOeuvre,DGManager.Oeuvre)""" xmldoc = xml.dom.minidom.Document() # Balise root xmltagRoot = xmldoc.createElement(B_ROOT) # Balise Auteur xmltagAuteur = self.__baliseAuteur(pAuteur) xmltagRoot.appendChild(xmltagAuteur) # Balise Oeuvre xmltagOeuvre = self.__baliseOeuvre(pOeuvre) xmltagRoot.appendChild(xmltagOeuvre) # Balise Arbre xmltagArbre = self.__baliseArbre() xmltagRoot.appendChild(xmltagArbre) xmldoc.appendChild(xmltagRoot) return xmldoc def __baliseAuteur(self, pAuteur): """ Methode privee qui construit la balise Auteur pre: isinstance(pAuteur,DGManager.Auteur) """ xmldoc = xml.dom.minidom.Document() # Balise Auteur xmltagAuteur = xmldoc.createElement(B_AUTEUR) # nom xmlNom = xmldoc.createElement(B_NOM) nomA = str(pAuteur.getNom()) nom = xmldoc.createTextNode(encodingNode(nomA)) xmlNom.appendChild(nom) # prenom xmlPrenom = xmldoc.createElement(B_PRENOM) prenomA = str(pAuteur.getPrenom()) prenom = xmldoc.createTextNode(encodingNode(prenomA)) xmlPrenom.appendChild(prenom) # naissance xmlNaissance = xmldoc.createElement(B_NAISSANCE) naisA = str(pAuteur.getNaissance()) naissance = xmldoc.createTextNode(encodingNode(naisA)) xmlNaissance.appendChild(naissance) # deces xmlDeces = xmldoc.createElement(B_DECES) decA = str(pAuteur.getDeces()) deces = xmldoc.createTextNode(encodingNode(decA)) xmlDeces.appendChild(deces) xmltagAuteur.appendChild(xmlNom) xmltagAuteur.appendChild(xmlPrenom) xmltagAuteur.appendChild(xmlNaissance) xmltagAuteur.appendChild(xmlDeces) return xmltagAuteur def __baliseOeuvre(self, pOeuvre): """ Methode privee qui construit la balise Oeuvre pre: isinstance(pOeuvre,DGManager.Oeuvre) """ xmldoc = xml.dom.minidom.Document() # Balise Oeuvre xmltagOeuvre = xmldoc.createElement(B_OEUVRE) # titre xmlTitre = xmldoc.createElement(B_TITRE) titreO = pOeuvre.getTitre() titre = xmldoc.createTextNode(encodingNode(str(titreO))) xmlTitre.appendChild(titre) # edition xmlEdition = xmldoc.createElement(B_EDITION) editO = pOeuvre.getEdition() edition = xmldoc.createTextNode(encodingNode(str(editO))) xmlEdition.appendChild(edition) # publication xmlPublication = xmldoc.createElement(B_PUBLICATION) pubO = pOeuvre.getPublication() publication = xmldoc.createTextNode(encodingNode(str(pubO))) xmlPublication.appendChild(publication) xmltagOeuvre.appendChild(xmlTitre) xmltagOeuvre.appendChild(xmlEdition) xmltagOeuvre.appendChild(xmlPublication) return xmltagOeuvre def __baliseArbre(self): xmldoc = xml.dom.minidom.Document() xmltagArbre = xmldoc.createElement(B_ARBRE) return xmltagArbre def __creerVersion(self,pName): xmldoc = xml.dom.minidom.Document() xmltagVersion = xmldoc.createElement(B_VERSION) xmltagVersion.setAttribute(B_ID,pName) return xmltagVersion def __creerEtat(self,pName): xmldoc = xml.dom.minidom.Document() xmltagEtat = xmldoc.createElement(B_ETAT) xmltagEtat.setAttribute(B_ID,pName) return xmltagEtat