pda2suiviactivite.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. '''
  2. Script d'export des données de configuration des applications 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 # @UnusedImport
  10. from core import logconf
  11. from core.pde import PdaDb, PDA_FILES_DEST
  12. logger = logging.getLogger("pda2suiviactivite")
  13. logconf.start("pda2suiviactivite", logging.DEBUG)
  14. # # POUR TESTER, décommenter les lignes suivantes
  15. ##-----------------------------------------------
  16. # PDA_FILES_DEST = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\PDA\Fichiers_PDA")
  17. # PdaDb._path = Path(r"\\h2o\local\4-transversal\BDD\mdb_test\PDA\db_PDA.mdb")
  18. # logger.handlers = [h for h in logger.handlers if (type(h) == logging.StreamHandler)]
  19. # logger.warning("Mode TEST")
  20. ##-----------------------------------------------
  21. def main():
  22. logger.info("Initialization")
  23. # Connect to db_PDA.mdb
  24. pda_db = PdaDb(autocommit=True)
  25. class PdaXmlFile():
  26. def __init__(self):
  27. self.source_table = ""
  28. self.fields_mapping = {}
  29. self.root_node = ""
  30. self.item_node = ""
  31. self.file_name = ""
  32. self.order_by = []
  33. self.nsmap = {'xsd': "http://www.w3.org/2001/XMLSchema",
  34. 'xsi': "http://www.w3.org/2001/XMLSchema-instance"}
  35. def create(self):
  36. maker = builder.ElementMaker(nsmap=self.nsmap)
  37. nodes = []
  38. sql = "SELECT {mapping} FROM {tbl} ORDER BY {order}".format(
  39. mapping=",".join(["[{}].[{}] as {}".format(self.source_table, key, val) for key, val in self.fields_mapping.items()]),
  40. tbl=self.source_table,
  41. order=",".join(self.order_by)
  42. )
  43. data = [record._asdict() for record in pda_db.read_all(sql)]
  44. for record in data:
  45. node = maker.__call__(self.item_node, *[maker.__call__(field, str(value)) for field, value in record.items()])
  46. nodes.append(node)
  47. root = maker.__call__(self.root_node, *nodes)
  48. with open(PDA_FILES_DEST / self.file_name, "wb") as f:
  49. f.write(etree.tostring(root, xml_declaration=True, encoding='utf-8', pretty_print=True))
  50. logger.info("> Créé: {}".format(self.file_name))
  51. xml = PdaXmlFile()
  52. xml.source_table = "pdaEngin"
  53. xml.fields_mapping = {"strEnginId": "Id", "strEnginLibelleLong": "Nom"}
  54. xml.root_node = "ArrayOfAttelage"
  55. xml.item_node = "Attelage"
  56. xml.file_name = "attelages.xml"
  57. xml.order_by = ["strEnginLibelleLong"]
  58. xml.create()
  59. xml = PdaXmlFile()
  60. xml.source_table = "pdaorigine"
  61. xml.fields_mapping = {"lngorigine": "Id", "strorigine": "Nom"}
  62. xml.root_node = "ArrayOfDepart"
  63. xml.item_node = "Depart"
  64. xml.file_name = "depart.xml"
  65. xml.order_by = ["strorigine"]
  66. xml.create()
  67. xml = PdaXmlFile()
  68. xml.source_table = "pdaHeures"
  69. xml.fields_mapping = {"annee": "annee", "mois": "mois", "heures": "heures"}
  70. xml.root_node = "dataroot"
  71. xml.item_node = "pdaHeures"
  72. xml.file_name = "heures.xml"
  73. xml.order_by = ["annee", "mois"]
  74. xml.create()
  75. xml = PdaXmlFile()
  76. xml.source_table = "pdaEquip"
  77. xml.fields_mapping = {"strEquipesId": "Id", "strEquipesLibelle": "Nom"}
  78. xml.root_node = "ArrayOfEquipe"
  79. xml.item_node = "Equipe"
  80. xml.file_name = "equipes.xml"
  81. xml.order_by = ["strEquipesLibelle"]
  82. xml.create()
  83. xml = PdaXmlFile()
  84. xml.source_table = "pdalocalisation"
  85. xml.fields_mapping = {"lngTiersId": "Id", "strTiersMnemo": "Nom"}
  86. xml.root_node = "ArrayOfLocalisation"
  87. xml.item_node = "Localisation"
  88. xml.file_name = "localisation.xml"
  89. xml.order_by = ["strTiersMnemo"]
  90. xml.create()
  91. xml = PdaXmlFile()
  92. xml.source_table = "pdaNatInterv"
  93. xml.fields_mapping = {"strCategorieInterventioinId": "Id", "strCategorieInterventioinLibelle": "Nom"}
  94. xml.root_node = "ArrayOfNatureRealisation"
  95. xml.item_node = "NatureRealisation"
  96. xml.file_name = "naturesinterventions.xml"
  97. xml.order_by = ["strCategorieInterventioinLibelle"]
  98. xml.create()
  99. if __name__ == "__main__":
  100. main()
  101. logger.info("-- Fin --")