''' Script d'export des données de configuration des applcations SuiviDesActivtes et SuiviActivitePC Les données d'origine sont issues de la base \\h2o\LOCAL\4-transversal\BDD\mdb\PDA\db_PDA.mdb Les fichiers résultats sont les fichiers *.xml du répertoire \\h2o\LOCAL\4-transversal\BDD\mdb\PDA\Fichiers_PDA\ ''' import logging from lxml import builder # @UnresolvedImport from lxml import etree # @UnresolvedImport from path import Path # @UnusedImport from core import logconf from core.pde import PdaDb, PDA_FILES_DEST logger = logging.getLogger("pda2suiviactivite") logconf.start("pda2suiviactivite", logging.DEBUG) logger.info("Initialization") # # POUR TESTER, décommenter les lignes suivantes ##----------------------------------------------- # PDA_FILES_DEST = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\PDA\Fichiers_PDA") # PdaDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\PDA\db_PDA.mdb") # logger.handlers = [h for h in logger.handlers if (type(h) == logging.StreamHandler)] # logger.warning("Mode TEST") ##----------------------------------------------- # Connect to db_PDA.mdb pda_db = PdaDb(autocommit=True) # Explication du mapping ci dessous: # ( # nom de la table, # {champ: noeud), # nom des nodes contenant chaque enregistrement, # nom du fichier cible # Order by # ) class PdaXmlFile(): def __init__(self): self.source_table = "" self.fields_mapping = {} self.root_node = "" self.item_node = "" self.file_name = "" self.order_by = [] self.nsmap = {'xsd': "http://www.w3.org/2001/XMLSchema", 'xsi': "http://www.w3.org/2001/XMLSchema-instance"} def create(self): maker = builder.ElementMaker(nsmap=self.nsmap) nodes = [] sql = "SELECT {mapping} FROM {tbl} ORDER BY {order}".format( mapping=",".join(["[{}].[{}] as {}".format(self.source_table, key, val) for key, val in self.fields_mapping.items()]), tbl=self.source_table, order=",".join(self.order_by) ) data = [record._asdict() for record in pda_db.read_all(sql)] for record in data: node = maker.__call__(self.item_node, *[maker.__call__(field, str(value)) for field, value in record.items()]) nodes.append(node) root = maker.__call__(self.root_node, *nodes) with open(PDA_FILES_DEST / self.file_name, "wb") as f: f.write(etree.tostring(root, xml_declaration=True, encoding='utf-8', pretty_print=True)) logger.info("> Créé: {}".format(self.file_name)) xml = PdaXmlFile() xml.source_table = "pdaEngin" xml.fields_mapping = {"strEnginId": "Id", "strEnginLibelleLong": "Nom"} xml.root_node = "ArrayOfAttelage" xml.item_node = "Attelage" xml.file_name = "attelages.xml" xml.order_by = ["strEnginLibelleLong"] xml.create() xml = PdaXmlFile() xml.source_table = "pdaorigine" xml.fields_mapping = {"lngorigine": "Id", "strorigine": "Nom"} xml.root_node = "ArrayOfDepart" xml.item_node = "Depart" xml.file_name = "depart.xml" xml.order_by = ["strorigine"] xml.create() xml = PdaXmlFile() xml.source_table = "pdaHeures" xml.fields_mapping = {"annee": "annee", "mois": "mois", "heures": "heures"} xml.root_node = "dataroot" xml.item_node = "pdaHeures" xml.file_name = "heures.xml" xml.order_by = ["annee", "mois"] xml.create() xml = PdaXmlFile() xml.source_table = "pdaEquip" xml.fields_mapping = {"strEquipesId": "Id", "strEquipesLibelle": "Nom"} xml.root_node = "ArrayOfEquipe" xml.item_node = "Equipe" xml.file_name = "equipes.xml" xml.order_by = ["strEquipesLibelle"] xml.create() xml = PdaXmlFile() xml.source_table = "pdalocalisation" xml.fields_mapping = {"lngTiersId": "Id", "strTiersMnemo": "Nom"} xml.root_node = "ArrayOfLocalisation" xml.item_node = "Localisation" xml.file_name = "localisation.xml" xml.order_by = ["strTiersMnemo"] xml.create() xml = PdaXmlFile() xml.source_table = "pdaNatInterv" xml.fields_mapping = {"strCategorieInterventioinId": "Id", "strCategorieInterventioinLibelle": "Nom"} xml.root_node = "ArrayOfNatureRealisation" xml.item_node = "NatureRealisation" xml.file_name = "naturesinterventions.xml" xml.order_by = ["strCategorieInterventioinLibelle"] xml.create() logger.info("Export termine")