Changeset 7861 for main/waeup.kofa/trunk/src/waeup
- Timestamp:
- 13 Mar 2012, 01:26:21 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/university/export.py
r7811 r7861 22 22 from cStringIO import StringIO 23 23 from waeup.kofa.interfaces import ICSVExporter 24 from waeup.kofa.utils.batching import ExporterBase 24 25 25 class FacultyExporter(grok.GlobalUtility ):26 class FacultyExporter(grok.GlobalUtility, ExporterBase): 26 27 """Exporter for faculties. 27 28 """ … … 31 32 #: Fieldnames considered by this exporter 32 33 fields = ('code', 'title', 'title_prefix') 33 34 def mangle_value(self, value, name, context=None):35 """Hook for mangling values in derived classes36 """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 unicode41 value = value.encode('utf-8')42 elif value is None:43 # None is not really representable in CSV files44 value = ''45 return value46 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 a51 :class:`csv.DictWriter` instance and outfile is the real file52 which is written to. The latter is important when writing to53 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))) # header63 return writer, outfile64 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] = value73 writer.writerow(row)74 return75 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 return86 34 87 35 def export(self, faculties, filepath=None):
Note: See TracChangeset for help on using the changeset viewer.