pda2suiviactivite.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. '''
  2. Script d'export des données de configuration des applcations SuiviDesActivtes et SuiviActivitePC
  3. Les données d'origine sont issues de la base \\h2o\LOCAL\4-transversal\BDD\mdb\PDA\db_PDA.mdb
  4. Les fichiers résultats sont les fichiers *.xml du répertoire \\h2o\LOCAL\4-transversal\BDD\mdb\PDA\Fichiers_PDA\
  5. '''
  6. import logging
  7. from lxml import builder # @UnresolvedImport
  8. from lxml import etree # @UnresolvedImport
  9. from path import Path
  10. from core import logconf
  11. from core.pde import PdaDb
  12. logger = logging.getLogger("pda2suiviactivite")
  13. logconf.start("pda2suiviactivite", logging.INFO)
  14. logger.info("Initialization")
  15. # Connect to db_PDA.mdb
  16. pda_db = PdaDb(autocommit=True)
  17. target_dir = Path(r"\\h2o\LOCAL\4-transversal\BDD\mdb\PDA\Fichiers_PDA")
  18. # Mapping: (nom de la table, {correspondance champ : alias}, nom des noeuds, nom du fichier cible)
  19. mapping = [
  20. ("pdaEngin", ("strEnginId", "strEnginLibelleLong"), "Attelage", "attelages.xml"),
  21. ("pdaorigine", ("lngorigine", "strorigine"), "Depart", "depart.xml"),
  22. ("pdaEquip", ("strEquipesId", "strEquipesLibelle"), "Equipe", "equipes.xml"),
  23. ("pdalocalisation", ("lngTiersId", "strTiersMnemo"), "Localisation", "localisation.xml"),
  24. ("pdaNatInterv", ("strCategorieInterventioinId", "strCategorieInterventioinLibelle"), "NatureRealisation", "naturesinterventions.xml")
  25. ]
  26. for tablename, fieldmapping, eltname, filename in mapping:
  27. maker = builder.ElementMaker(nsmap={'xsd': "http://www.w3.org/2001/XMLSchema",
  28. 'xsi': "http://www.w3.org/2001/XMLSchema-instance"})
  29. nodes = []
  30. fieldid, fieldnom = fieldmapping
  31. sql = "SELECT {id} as Id, {nom} as Nom FROM {tbl} ORDER BY {nom}".format(
  32. id=fieldid,
  33. nom=fieldnom,
  34. tbl=tablename
  35. )
  36. data = [record._asdict() for record in pda_db.read_all(sql)]
  37. for record in data:
  38. node = maker.__call__(eltname, *[maker.__call__(field, str(value)) for field, value in record.items()])
  39. nodes.append(node)
  40. root = maker.__call__("ArrayOf" + eltname, *nodes)
  41. if not filename[-4:] == ".xml":
  42. filename += ".xml"
  43. with open(target_dir / filename, "wb") as f:
  44. f.write(etree.tostring(root, xml_declaration=True, encoding='utf-8', pretty_print=True))
  45. logger.info("> Exporte: {}".format(filename))
  46. logger.info("Export termine")