Index: /main/waeup.kofa/trunk/src/waeup/kofa/utils/batching.py
===================================================================
--- /main/waeup.kofa/trunk/src/waeup/kofa/utils/batching.py	(revision 7858)
+++ /main/waeup.kofa/trunk/src/waeup/kofa/utils/batching.py	(revision 7859)
@@ -28,9 +28,11 @@
 import tempfile
 import time
+from cStringIO import StringIO
 from zope.component import createObject
 from zope.interface import Interface
 from zope.schema import getFields
 from waeup.kofa.interfaces import (
-    IBatchProcessor, FatalCSVError, DuplicationError, IObjectConverter)
+    IBatchProcessor, FatalCSVError, DuplicationError, IObjectConverter,
+    ICSVExporter)
 
 class BatchProcessor(grok.GlobalUtility):
@@ -272,5 +274,5 @@
         num_warns = 0
         site = grok.getSite()
-        
+
         for raw_row in reader:
             num += 1
@@ -343,2 +345,84 @@
         return (num, num_warns,
                 os.path.abspath(finished_path), failed_path)
+
+class ExporterBase(object):
+    """A base for exporters.
+    """
+    grok.implements(ICSVExporter)
+
+    #: Fieldnames considered by this exporter
+    fields = ('code', 'title', 'title_prefix')
+
+    def mangle_value(self, value, name, context=None):
+        """Hook for mangling values in derived classes
+        """
+        if isinstance(value, bool):
+            value = value and '1' or '0'
+        elif isinstance(value, unicode):
+            # CSV writers like byte streams better than unicode
+            value = value.encode('utf-8')
+        elif value is None:
+            # None is not really representable in CSV files
+            value = ''
+        return value
+
+    def get_csv_writer(self, filepath=None):
+        """Get a CSV dict writer instance open for writing.
+
+        Returns a tuple (<writer>, <outfile>) where ``<writer>`` is a
+        :class:`csv.DictWriter` instance and outfile is the real file
+        which is written to. The latter is important when writing to
+        StringIO and can normally be ignored otherwise.
+
+        The returned file will already be filled with the header row.
+
+        Please note that if you give a filepath, the returned outfile
+        is open for writing only and you might have to close it before
+        reopening it for reading.
+        """
+        if filepath is None:
+            outfile = StringIO()
+        else:
+            outfile = open(filepath, 'wb')
+        writer = csv.DictWriter(outfile, self.fields)
+        writer.writerow(dict(zip(self.fields, self.fields))) # header
+        return writer, outfile
+
+    def write_item(self, obj, writer):
+        """Write a row extracted from `obj` into CSV file using `writer`.
+        """
+        row = {}
+        for name in self.fields:
+            value = getattr(obj, name, None)
+            value = self.mangle_value(value, name, obj)
+            row[name] = value
+        writer.writerow(row)
+        return
+
+    def close_outfile(self, filepath, outfile):
+        """Close outfile.
+
+        If filepath is None, the contents of outfile is returned.
+        """
+        outfile.seek(0)
+        if filepath is None:
+            return outfile.read()
+        outfile.close()
+        return
+
+    def export(self, iterable, filepath=None):
+        """Export `iterable` as CSV file.
+
+        If `filepath` is ``None``, a raw string with CSV data should
+        be returned.
+        """
+        raise NotImplementedError
+
+    def export_all(self, site, filepath=None):
+        """Export all appropriate objects in `site` into `filepath` as
+        CSV data.
+
+        If `filepath` is ``None``, a raw string with CSV data should
+        be returned.
+        """
+        raise NotImplementedError
