pda2suiviactivite.py 4.4 KB

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