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

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


Author: bh
Date: 2010-11-16 21:45:35 +0100 (Tue, 16 Nov 2010)
New Revision: 4212

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/lib/export.py
Log:
* mpulsweb/lib/export.py (XLSTable, XLSContainer): New. XLS export
classes derived from the new base classes Table and Container.
They effectively replace the old XLS export code.
(XLSExport.__init__): Use XLSContainer with XLSTable as
table_factory instead of the Book class.
(XLSExport.export): Adapt to changes.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2010-11-16 20:38:05 UTC (rev 4211)
+++ base/trunk/ChangeLog	2010-11-16 20:45:35 UTC (rev 4212)
@@ -1,5 +1,14 @@
 2010-11-16  Bernhard Herzog  <bh at intevation.de>
 
+	* mpulsweb/lib/export.py (XLSTable, XLSContainer): New. XLS export
+	classes derived from the new base classes Table and Container.
+	They effectively replace the old XLS export code.
+	(XLSExport.__init__): Use XLSContainer with XLSTable as
+	table_factory instead of the Book class.
+	(XLSExport.export): Adapt to changes.
+
+2010-11-16  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/lib/export.py (Container.__init__): New instance
 	variable table_order to keep track of the order in which the table
 	occur in the formed definition, so that the order of the sheets in

Modified: base/trunk/mpulsweb/lib/export.py
===================================================================
--- base/trunk/mpulsweb/lib/export.py	2010-11-16 20:38:05 UTC (rev 4211)
+++ base/trunk/mpulsweb/lib/export.py	2010-11-16 20:45:35 UTC (rev 4212)
@@ -269,17 +269,47 @@
         self.workbook.save(fname)
 
 
+class XLSTable(Table):
 
+    def add_to_workbook(self, workbook):
+        sheet = workbook.add_sheet(self.title)
+        for column, name in enumerate(self.columns):
+            sheet.write(0, column, name)
+
+        for row_num, row in enumerate(self.rows):
+            for column, name in enumerate(self.columns):
+                sheet.write(row_num + 1, column, self.encode(row.get(name)))
+
+    def encode(self, value):
+        if value is None:
+            return ""
+        if isinstance(value, basestring) and len(value) > 32765:
+            value = value[:32765]
+        return value
+
+
+class XLSContainer(Container):
+
+    def write_xls(self, output):
+        workbook = pyExcelerator.Workbook()
+        for name in self.table_order:
+            if name in self.tables:
+                self.tables[name].add_to_workbook(workbook)
+        workbook.save(FakeStr(output))
+
+
 class XLSExport(Export):
 
     def __init__(self, tree, selection):
-        self.book = Book(tree, selection)
+        self.tree = tree
+        self.xls = XLSContainer(g.formedTree, selection, XLSTable,
+                                one_table_per_page=True)
 
     def export(self):
-        self.book.fill()
+        self.xls.fill(self.tree)
         try:
             out = MyStringIO()
-            self.book.write_xls(FakeStr(out))
+            self.xls.write_xls(out)
             return out.getvalue()
         finally:
             out.real_close()



More information about the Mpuls-commits mailing list