Changeset 7861


Ignore:
Timestamp:
13 Mar 2012, 01:26:21 (13 years ago)
Author:
uli
Message:

Use the new exporter base for our exporters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/university/export.py

    r7811 r7861  
    2222from cStringIO import StringIO
    2323from waeup.kofa.interfaces import ICSVExporter
     24from waeup.kofa.utils.batching import ExporterBase
    2425
    25 class FacultyExporter(grok.GlobalUtility):
     26class FacultyExporter(grok.GlobalUtility, ExporterBase):
    2627    """Exporter for faculties.
    2728    """
     
    3132    #: Fieldnames considered by this exporter
    3233    fields = ('code', 'title', 'title_prefix')
    33 
    34     def mangle_value(self, value, name, context=None):
    35         """Hook for mangling values in derived classes
    36         """
    37         if isinstance(value, bool):
    38             value = value and '1' or '0'
    39         elif isinstance(value, unicode):
    40             # CSV writers like byte streams better than unicode
    41             value = value.encode('utf-8')
    42         elif value is None:
    43             # None is not really representable in CSV files
    44             value = ''
    45         return value
    46 
    47     def get_csv_writer(self, filepath=None):
    48         """Get a CSV dict writer instance open for writing.
    49 
    50         Returns a tuple (<writer>, <outfile>) where ``<writer>`` is a
    51         :class:`csv.DictWriter` instance and outfile is the real file
    52         which is written to. The latter is important when writing to
    53         StringIO and can normally be ignored otherwise.
    54 
    55         The returned file will already be filled with the header row.
    56         """
    57         if filepath is None:
    58             outfile = StringIO()
    59         else:
    60             outfile = open(filepath, 'wb')
    61         writer = csv.DictWriter(outfile, self.fields)
    62         writer.writerow(dict(zip(self.fields, self.fields))) # header
    63         return writer, outfile
    64 
    65     def write_item(self, faculty, writer):
    66         """Write a row into using `writer`.
    67         """
    68         row = {}
    69         for name in self.fields:
    70             value = getattr(faculty, name, None)
    71             value = self.mangle_value(value, name, faculty)
    72             row[name] = value
    73         writer.writerow(row)
    74         return
    75 
    76     def close_outfile(self, filepath, outfile):
    77         """Close outfile.
    78 
    79         If filepath is None, the contents of outfile is returned.
    80         """
    81         outfile.seek(0)
    82         if filepath is None:
    83             return outfile.read()
    84         outfile.close()
    85         return
    8634
    8735    def export(self, faculties, filepath=None):
Note: See TracChangeset for help on using the changeset viewer.