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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Nov 16 20:55:23 CET 2010


Author: bh
Date: 2010-11-16 20:55:22 +0100 (Tue, 16 Nov 2010)
New Revision: 4207

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/lib/export.py
Log:
* mpulsweb/lib/export.py (Container, CSVContainer): Move
non-csv-specific code from CSVContainer into the new base class
Container and derive CSVContainer from that.  The Container
constructor has one new parameter table_factory which is needed
because the base class cannot depend on a particular table class
like CSVTable.
(CSVExport.__init__): Instantiate CSVContainer with CSVTable as
table_factory.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2010-11-16 19:50:46 UTC (rev 4206)
+++ base/trunk/ChangeLog	2010-11-16 19:55:22 UTC (rev 4207)
@@ -1,5 +1,16 @@
 2010-11-16  Bernhard Herzog  <bh at intevation.de>
 
+	* mpulsweb/lib/export.py (Container, CSVContainer): Move
+	non-csv-specific code from CSVContainer into the new base class
+	Container and derive CSVContainer from that.  The Container
+	constructor has one new parameter table_factory which is needed
+	because the base class cannot depend on a particular table class
+	like CSVTable.
+	(CSVExport.__init__): Instantiate CSVContainer with CSVTable as
+	table_factory.
+
+2010-11-16  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/lib/export.py (Table.__init__): Remove handling of
 	title which was added accidentally.
 

Modified: base/trunk/mpulsweb/lib/export.py
===================================================================
--- base/trunk/mpulsweb/lib/export.py	2010-11-16 19:50:46 UTC (rev 4206)
+++ base/trunk/mpulsweb/lib/export.py	2010-11-16 19:55:22 UTC (rev 4207)
@@ -73,8 +73,66 @@
         self.rows.append(row)
 
 
+class Container(object):
 
+    """A container of multiple Table instances"""
 
+    def __init__(self, meta_tree, selection, table_factory):
+        """Initialize the container with a FormEd meta tree and the selection.
+        """
+        self.meta_tree = meta_tree
+        self.table_factory = table_factory
+        self.tables = dict()
+        self.create_tables(selection)
+
+    def create_tables(self, selection):
+        table_columns = dict()
+        repeat_groups = set()
+        for page in self.meta_tree.findAllByClass(PageNode):
+            if page.getName() not in selection:
+                continue
+
+            collector = StructureWidgetCollector()
+            page.visit(collector.visitor)
+            for nc in collector.widgets:
+                table_name = "master"
+                for p in nc.pathToRoot():
+                    if isinstance(p, RepeatNode):
+                        table_name = p.getName()
+                        repeat_groups.add(table_name)
+                        break
+                table_columns.setdefault(table_name, []).append(nc.getName())
+
+        for name, columns in table_columns.iteritems():
+            extra_columns = ["id"]
+            if name in repeat_groups:
+                extra_columns.append("master_id")
+            self.tables[name] = self.table_factory(extra_columns + columns)
+
+    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)
+
+    def fill_table(self, node, node_id, parent_id=None):
+        table = self.tables[node.tag]
+
+        row = dict(id=node_id)
+        if parent_id is not None:
+            row["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)
+            else:
+                row[name] = child.text
+        table.append(row)
+
+
+
 def to_str(x):
     t = type(x)
     if t in (IntType, LongType):
@@ -236,63 +294,10 @@
 
 
 
-class CSVContainer:
+class CSVContainer(Container):
 
     """A container of multiple CSVTable instances"""
 
-    def __init__(self, meta_tree, selection):
-        """Initialize the container with a FormEd meta tree and the selection.
-        """
-        self.meta_tree = meta_tree
-        self.tables = dict()
-        self.create_tables(selection)
-
-    def create_tables(self, selection):
-        table_columns = dict()
-        repeat_groups = set()
-        for page in self.meta_tree.findAllByClass(PageNode):
-            if page.getName() not in selection:
-                continue
-
-            collector = StructureWidgetCollector()
-            page.visit(collector.visitor)
-            for nc in collector.widgets:
-                table_name = "master"
-                for p in nc.pathToRoot():
-                    if isinstance(p, RepeatNode):
-                        table_name = p.getName()
-                        repeat_groups.add(table_name)
-                        break
-                table_columns.setdefault(table_name, []).append(nc.getName())
-
-        for name, columns in table_columns.iteritems():
-            extra_columns = ["id"]
-            if name in repeat_groups:
-                extra_columns.append("master_id")
-            self.tables[name] = CSVTable(extra_columns + columns)
-
-    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)
-
-    def fill_table(self, node, node_id, parent_id=None):
-        table = self.tables[node.tag]
-
-        row = dict(id=node_id)
-        if parent_id is not None:
-            row["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)
-            else:
-                row[name] = child.text
-        table.append(row)
-
     def write_zip_file(self, output, encoding="utf-8"):
         """Write the contents of the container to output as a zip-File"""
         def add(name, contents):
@@ -323,7 +328,7 @@
 
     def __init__(self, tree, selection):
         self.tree = tree
-        self.csv = CSVContainer(g.formedTree, selection)
+        self.csv = CSVContainer(g.formedTree, selection, CSVTable)
 
     def export(self):
         self.csv.fill(self.tree)



More information about the Mpuls-commits mailing list