Browse Source

FIX correction creation heures.xml

olivier.massot 7 years ago
parent
commit
5a59cd1ab9
1 changed files with 87 additions and 67 deletions
  1. 87 67
      pda2suiviactivite.py

+ 87 - 67
pda2suiviactivite.py

@@ -47,73 +47,93 @@ pda_db = PdaDb(autocommit=True)
     # 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 = []
-
-
-    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)]
-
-    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(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))
+class PdaXmlFile():
+    def __init__(self):
+        self.source_table = ""
+        self.fields_mapping = {}
+        self.root_node = ""
+        self.item_node = ""
+        self.file_name = ""
+        self.order_by = []
+        self.nsmap = {'xsd': "http://www.w3.org/2001/XMLSchema",
+                      'xsi': "http://www.w3.org/2001/XMLSchema-instance"}
+
+    def create(self):
+        maker = builder.ElementMaker(nsmap=self.nsmap)
+
+        nodes = []
+
+        sql = "SELECT {mapping} FROM {tbl} ORDER BY {order}".format(
+                                                                mapping=",".join(["[{}].[{}] as {}".format(self.source_table, key, val) for key, val in self.fields_mapping.items()]),
+                                                                tbl=self.source_table,
+                                                                order=",".join(self.order_by)
+                                                                )
+        data = [record._asdict() for record in pda_db.read_all(sql)]
+
+        for record in data:
+            node = maker.__call__(self.item_node, *[maker.__call__(field, str(value)) for field, value in record.items()])
+            nodes.append(node)
+
+        root = maker.__call__(self.root_node, *nodes)
+
+        with open(PDA_FILES_DEST / self.file_name, "wb") as f:
+            f.write(etree.tostring(root, xml_declaration=True, encoding='utf-8', pretty_print=True))
+        logger.info("> Créé: {}".format(self.file_name))
+
+
+xml = PdaXmlFile()
+xml.source_table = "pdaEngin"
+xml.fields_mapping = {"strEnginId": "Id", "strEnginLibelleLong": "Nom"}
+xml.root_node = "ArrayOfAttelage"
+xml.item_node = "Attelage"
+xml.file_name = "attelages.xml"
+xml.order_by = ["strEnginLibelleLong"]
+xml.create()
+
+xml = PdaXmlFile()
+xml.source_table = "pdaorigine"
+xml.fields_mapping = {"lngorigine": "Id", "strorigine": "Nom"}
+xml.root_node = "ArrayOfDepart"
+xml.item_node = "Depart"
+xml.file_name = "depart.xml"
+xml.order_by = ["strorigine"]
+xml.create()
+
+xml = PdaXmlFile()
+xml.source_table = "pdaHeures"
+xml.fields_mapping = {"annee": "annee", "mois": "mois", "heures": "heures"}
+xml.root_node = "dataroot"
+xml.item_node = "pdaHeures"
+xml.file_name = "heures.xml"
+xml.order_by = ["annee", "mois"]
+xml.create()
+
+xml = PdaXmlFile()
+xml.source_table = "pdaEquip"
+xml.fields_mapping = {"strEquipesId": "Id", "strEquipesLibelle": "Nom"}
+xml.root_node = "ArrayOfEquipe"
+xml.item_node = "Equipe"
+xml.file_name = "equipes.xml"
+xml.order_by = ["strEquipesLibelle"]
+xml.create()
+
+xml = PdaXmlFile()
+xml.source_table = "pdalocalisation"
+xml.fields_mapping = {"lngTiersId": "Id", "strTiersMnemo": "Nom"}
+xml.root_node = "ArrayOfLocalisation"
+xml.item_node = "Localisation"
+xml.file_name = "localisation.xml"
+xml.order_by = ["strTiersMnemo"]
+xml.create()
+
+xml = PdaXmlFile()
+xml.source_table = "pdaNatInterv"
+xml.fields_mapping = {"strCategorieInterventioinId": "Id", "strCategorieInterventioinLibelle": "Nom"}
+xml.root_node = "ArrayOfNatureRealisation"
+xml.item_node = "NatureRealisation"
+xml.file_name = "naturesinterventions.xml"
+xml.order_by = ["strCategorieInterventioinLibelle"]
+xml.create()
 
 logger.info("Export termine")