''' 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 from core import logconf from core.pde import PdaDb logger = logging.getLogger("gf2factures") logconf.start("gf2factures", logging.INFO) logger.info("Initialization") # Connect to db_PDA.mdb pda_db = PdaDb(autocommit=True) target_dir = Path(r"\\h2o\LOCAL\4-transversal\BDD\mdb\PDA\Fichiers_PDA") # Mapping: (nom de la table, {correspondance champ : alias}, nom des noeuds, nom du fichier cible) mapping = [ ("pdaEngin", {"strEnginId": "Id", "strEnginLibelleLong": "Nom"}, "Attelage", "attelages.xml"), ("pdaorigine", {"lngorigine": "Id", "strorigine": "Nom"}, "Depart", "depart.xml"), ("pdaEquip", {"strEquipesId": "Id", "strEquipesLibelle": "Nom"}, "Equipe", "equipes.xml"), ("pdalocalisation", {"lngTiersId": "Id", "strTiersMnemo": "Nom"}, "Localisation", "localisation.xml"), ("pdaNatInterv", {"strCategorieInterventioinId": "Id", "strCategorieInterventioinLibelle": "Nom"}, "NatureRealisation", "naturesinterventions.xml"), ("pdaHeures", {"annee": "annee", "mois": "mois", "heures": "heures"}, "pdaHeures", "heures.xml"), ] for tablename, fieldmapping, eltname, filename in mapping: maker = builder.ElementMaker(nsmap={'xsd': "http://www.w3.org/2001/XMLSchema", 'xsi': "http://www.w3.org/2001/XMLSchema-instance"}) nodes = [] sql = "SELECT {} FROM {}".format( ",".join([("{} AS {}".format(name, alias) if name != alias else name) for name, alias in fieldmapping.items()]), tablename ) data = pda_db.read_all(sql) for record in data: node = maker.__call__(eltname, *[maker.__call__(field, str(value)) for field, value in record.items()]) nodes.append(node) root = maker.__call__("ArrayOf" + eltname, *nodes) with open(target_dir / filename, "wb") as f: f.write(etree.tostring(root, xml_declaration=True, encoding='utf-8', pretty_print=True)) logger.info("> Exporte: {}".format(filename)) logger.info("Export termine")