[Mpuls-commits] r4209 - in base/trunk: . mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Nov 16 21:28:06 CET 2010


Author: bh
Date: 2010-11-16 21:28:06 +0100 (Tue, 16 Nov 2010)
New Revision: 4209

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/lib/export.py
Log:
* mpulsweb/lib/export.py (Container.__init__): New instance
variable field_name_to_table_name to keep track of which fields
belong to which output table.
(Container.create_tables): Fill field_name_to_table_name
(Container.fill_table, Container.fill_from_node): Rename
fill_table to the more appropriate fill_from_node.  Use
field_name_to_table_name to map from field names to the table into
which the field should be written.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2010-11-16 20:01:14 UTC (rev 4208)
+++ base/trunk/ChangeLog	2010-11-16 20:28:06 UTC (rev 4209)
@@ -1,5 +1,16 @@
 2010-11-16  Bernhard Herzog  <bh at intevation.de>
 
+	* mpulsweb/lib/export.py (Container.__init__): New instance
+	variable field_name_to_table_name to keep track of which fields
+	belong to which output table.
+	(Container.create_tables): Fill field_name_to_table_name
+	(Container.fill_table, Container.fill_from_node): Rename
+	fill_table to the more appropriate fill_from_node.  Use
+	field_name_to_table_name to map from field names to the table into
+	which the field should be written.
+
+2010-11-16  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/lib/export.py (Table.__init__): New parameter and
 	instance variable title.  Not needed by CSV export but will be
 	needed to export to XLS

Modified: base/trunk/mpulsweb/lib/export.py
===================================================================
--- base/trunk/mpulsweb/lib/export.py	2010-11-16 20:01:14 UTC (rev 4208)
+++ base/trunk/mpulsweb/lib/export.py	2010-11-16 20:28:06 UTC (rev 4209)
@@ -4,6 +4,7 @@
 import itertools
 import zipfile
 import stat
+from collections import defaultdict
 
 import pyExcelerator
 
@@ -87,6 +88,7 @@
         self.meta_tree = meta_tree
         self.table_factory = table_factory
         self.tables = dict()
+        self.field_name_to_table_name = dict()
         self.create_tables(selection)
 
     def create_tables(self, selection):
@@ -117,31 +119,37 @@
                 extra_columns.append("master_id")
             self.tables[name] = self.table_factory(titles[name],
                                                    extra_columns + columns)
+            for column_name in columns:
+                self.field_name_to_table_name[column_name] = name
 
     def fill(self, tree):
         """Fill the tables with the contents of the ElementTree tree."""
         self.repeat_group_ids = itertools.count()
         for case_id, case in enumerate(tree.getiterator('master')):
-            self.fill_table(case, case_id)
+            self.fill_from_node(case, case_id)
 
-    def fill_table(self, node, node_id, parent_id=None):
-        table = self.tables[node.tag]
+    def fill_from_node(self, node, node_id, parent_id=None):
+        rows = defaultdict(lambda: dict(id=node_id))
 
-        row = dict(id=node_id)
         if parent_id is not None:
-            row["master_id"] = parent_id
+            rows[node.tag]["master_id"] = parent_id
 
         for child in node.getchildren():
             name = child.tag
             nc = self.meta_tree.findByName(name)
             if isinstance(nc, RepeatNode):
-                self.fill_table(child, self.repeat_group_ids.next(), node_id)
+                self.fill_from_node(child, self.repeat_group_ids.next(),
+                                    node_id)
             else:
-                row[name] = child.text
-        table.append(row)
+                table_name = self.field_name_to_table_name.get(name)
+                if table_name is not None:
+                    rows[table_name][name] = child.text
 
+        for table_name, row in rows.iteritems():
+            self.tables[table_name].append(row)
 
 
+
 def to_str(x):
     t = type(x)
     if t in (IntType, LongType):



More information about the Mpuls-commits mailing list