''' 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("pda2suiviactivite") logconf.start("pda2suiviactivite", 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", "strEnginLibelleLong"), "Attelage", "attelages.xml"), ("pdaorigine", ("lngorigine", "strorigine"), "Depart", "depart.xml"), ("pdaEquip", ("strEquipesId", "strEquipesLibelle"), "Equipe", "equipes.xml"), ("pdalocalisation", ("lngTiersId", "strTiersMnemo"), "Localisation", "localisation.xml"), ("pdaNatInterv", ("strCategorieInterventioinId", "strCategorieInterventioinLibelle"), "NatureRealisation", "naturesinterventions.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 = [] fieldid, fieldnom = fieldmapping sql = "SELECT {id} as Id, {nom} as Nom FROM {tbl} ORDER BY {nom}".format( id=fieldid, nom=fieldnom, tbl=tablename ) data = [record._asdict() for record in 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) if not filename[-4:] == ".xml": filename += ".xml" 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")