Coverage for casanova/casanova/writer.py: 33%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# =============================================================================
2# Casanova Writer
3# =============================================================================
4#
5# A CSV writer that is only really useful if you intend to resume its operation
6# somehow
7#
8import csv
10from casanova.resuming import Resumer, LastCellResumer
11from casanova.reader import Headers
14class Writer(object):
15 __supported_resumers__ = (LastCellResumer,)
17 def __init__(self, output_file, fieldnames):
18 self.fieldnames = fieldnames
19 self.headers = Headers(fieldnames)
21 can_resume = False
23 if isinstance(output_file, Resumer):
24 resumer = output_file
26 if not isinstance(output_file, self.__class__.__supported_resumers__):
27 raise TypeError('%s: does not support %s!' % (self.__class__.__name__, output_file.__class__.__name__))
29 can_resume = resumer.can_resume()
31 if can_resume:
32 resumer.get_insights_from_output(self)
34 output_file = resumer.open_output_file()
36 self.writer = csv.writer(output_file)
38 if not can_resume:
39 self.writeheader()
41 def writeheader(self):
42 self.writer.writerow(self.fieldnames)
44 def writerow(self, row):
45 self.writer.writerow(row)