|
|
@@ -11,44 +11,89 @@ import logging
|
|
|
|
|
|
from lxml import builder # @UnresolvedImport
|
|
|
from lxml import etree # @UnresolvedImport
|
|
|
-from path import Path
|
|
|
+from path import Path # @UnusedImport
|
|
|
|
|
|
from core import logconf
|
|
|
-from core.pde import PdaDb
|
|
|
+from core.pde import PdaDb, PDA_FILES_DEST
|
|
|
|
|
|
|
|
|
logger = logging.getLogger("pda2suiviactivite")
|
|
|
-logconf.start("pda2suiviactivite", logging.INFO)
|
|
|
+logconf.start("pda2suiviactivite", logging.DEBUG)
|
|
|
|
|
|
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")
|
|
|
+# # POUR TESTER, décommenter les lignes suivantes
|
|
|
+##-----------------------------------------------
|
|
|
|
|
|
-# 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")
|
|
|
- ]
|
|
|
+# PDA_FILES_DEST = Path(r"L:\4-transversal\BDD\mdb_test\PDA\Fichiers_PDA")
|
|
|
+# PdaDb._path = Path(r""L:\4-transversal\BDD\mdb_test\PDA\db_PDA.mdb")
|
|
|
+
|
|
|
+##-----------------------------------------------
|
|
|
|
|
|
-for tablename, fieldmapping, eltname, filename in mapping:
|
|
|
+# Explication du mapping ci dessous:
|
|
|
+ # (
|
|
|
+ # nom de la table,
|
|
|
+ # {champ: noeud),
|
|
|
+ # nom des nodes contenant chaque enregistrement,
|
|
|
+ # nom du fichier cible
|
|
|
+ # Order by
|
|
|
+ # )
|
|
|
+
|
|
|
+mapping = [
|
|
|
+ ("pdaEngin",
|
|
|
+ {"strEnginId": "Id", "strEnginLibelleLong": "Nom"},
|
|
|
+ "Attelage",
|
|
|
+ "attelages.xml",
|
|
|
+ ["strEnginLibelleLong"]
|
|
|
+ ),
|
|
|
+ ("pdaorigine",
|
|
|
+ {"lngorigine": "Id", "strorigine": "Nom"},
|
|
|
+ "Depart",
|
|
|
+ "depart.xml",
|
|
|
+ ["strorigine"]
|
|
|
+ ),
|
|
|
+ ("pdaHeures",
|
|
|
+ {"annee": "annee", "mois": "mois", "heures": "heures"},
|
|
|
+ "pdaHeures",
|
|
|
+ "heures.xml",
|
|
|
+ ["annee", "mois"]
|
|
|
+ ),
|
|
|
+ ("pdaEquip",
|
|
|
+ {"strEquipesId": "Id", "strEquipesLibelle": "Nom"},
|
|
|
+ "Equipe",
|
|
|
+ "equipes.xml",
|
|
|
+ ["strEquipesLibelle"]
|
|
|
+ ),
|
|
|
+ ("pdalocalisation",
|
|
|
+ {"lngTiersId": "Id", "strTiersMnemo": "Nom"},
|
|
|
+ "Localisation",
|
|
|
+ "localisation.xml",
|
|
|
+ ["strTiersMnemo"]
|
|
|
+ ),
|
|
|
+ ("pdaNatInterv",
|
|
|
+ {"strCategorieInterventioinId": "Id", "strCategorieInterventioinLibelle": "Nom"},
|
|
|
+ "NatureRealisation",
|
|
|
+ "naturesinterventions.xml",
|
|
|
+ ["strCategorieInterventioinLibelle"]
|
|
|
+ )
|
|
|
+ ]
|
|
|
+
|
|
|
+for tablename, fieldmapping, eltname, filename, order 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
|
|
|
- )
|
|
|
+ sql = "SELECT {mapping} FROM {tbl} ORDER BY {order}".format(
|
|
|
+ mapping=",".join(["[{}].[{}] as {}".format(tablename, key, val) for key, val in fieldmapping.items()]),
|
|
|
+ tbl=tablename,
|
|
|
+ order=",".join(order)
|
|
|
+ )
|
|
|
+ logger.debug(sql)
|
|
|
|
|
|
data = [record._asdict() for record in pda_db.read_all(sql)]
|
|
|
|
|
|
@@ -61,7 +106,7 @@ for tablename, fieldmapping, eltname, filename in mapping:
|
|
|
if not filename[-4:] == ".xml":
|
|
|
filename += ".xml"
|
|
|
|
|
|
- with open(target_dir / filename, "wb") as f:
|
|
|
+ with open(PDA_FILES_DEST / filename, "wb") as f:
|
|
|
f.write(etree.tostring(root, xml_declaration=True, encoding='utf-8', pretty_print=True))
|
|
|
logger.info("> Exporte: {}".format(filename))
|
|
|
|