Changeset 7859 for main/waeup.kofa/trunk/src/waeup/kofa
- Timestamp:
- 13 Mar 2012, 01:17:20 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/utils/batching.py
r7819 r7859 28 28 import tempfile 29 29 import time 30 from cStringIO import StringIO 30 31 from zope.component import createObject 31 32 from zope.interface import Interface 32 33 from zope.schema import getFields 33 34 from waeup.kofa.interfaces import ( 34 IBatchProcessor, FatalCSVError, DuplicationError, IObjectConverter) 35 IBatchProcessor, FatalCSVError, DuplicationError, IObjectConverter, 36 ICSVExporter) 35 37 36 38 class BatchProcessor(grok.GlobalUtility): … … 272 274 num_warns = 0 273 275 site = grok.getSite() 274 276 275 277 for raw_row in reader: 276 278 num += 1 … … 343 345 return (num, num_warns, 344 346 os.path.abspath(finished_path), failed_path) 347 348 class ExporterBase(object): 349 """A base for exporters. 350 """ 351 grok.implements(ICSVExporter) 352 353 #: Fieldnames considered by this exporter 354 fields = ('code', 'title', 'title_prefix') 355 356 def mangle_value(self, value, name, context=None): 357 """Hook for mangling values in derived classes 358 """ 359 if isinstance(value, bool): 360 value = value and '1' or '0' 361 elif isinstance(value, unicode): 362 # CSV writers like byte streams better than unicode 363 value = value.encode('utf-8') 364 elif value is None: 365 # None is not really representable in CSV files 366 value = '' 367 return value 368 369 def get_csv_writer(self, filepath=None): 370 """Get a CSV dict writer instance open for writing. 371 372 Returns a tuple (<writer>, <outfile>) where ``<writer>`` is a 373 :class:`csv.DictWriter` instance and outfile is the real file 374 which is written to. The latter is important when writing to 375 StringIO and can normally be ignored otherwise. 376 377 The returned file will already be filled with the header row. 378 379 Please note that if you give a filepath, the returned outfile 380 is open for writing only and you might have to close it before 381 reopening it for reading. 382 """ 383 if filepath is None: 384 outfile = StringIO() 385 else: 386 outfile = open(filepath, 'wb') 387 writer = csv.DictWriter(outfile, self.fields) 388 writer.writerow(dict(zip(self.fields, self.fields))) # header 389 return writer, outfile 390 391 def write_item(self, obj, writer): 392 """Write a row extracted from `obj` into CSV file using `writer`. 393 """ 394 row = {} 395 for name in self.fields: 396 value = getattr(obj, name, None) 397 value = self.mangle_value(value, name, obj) 398 row[name] = value 399 writer.writerow(row) 400 return 401 402 def close_outfile(self, filepath, outfile): 403 """Close outfile. 404 405 If filepath is None, the contents of outfile is returned. 406 """ 407 outfile.seek(0) 408 if filepath is None: 409 return outfile.read() 410 outfile.close() 411 return 412 413 def export(self, iterable, filepath=None): 414 """Export `iterable` as CSV file. 415 416 If `filepath` is ``None``, a raw string with CSV data should 417 be returned. 418 """ 419 raise NotImplementedError 420 421 def export_all(self, site, filepath=None): 422 """Export all appropriate objects in `site` into `filepath` as 423 CSV data. 424 425 If `filepath` is ``None``, a raw string with CSV data should 426 be returned. 427 """ 428 raise NotImplementedError
Note: See TracChangeset for help on using the changeset viewer.