starting to write docs, taking decisions on public api

This commit is contained in:
Romain Dorgueil
2016-12-27 13:31:38 +01:00
parent 512e2ab46d
commit 25ad284935
29 changed files with 604 additions and 96 deletions

35
bonobo/io/file.py Normal file
View File

@ -0,0 +1,35 @@
from bonobo.util.lifecycle import with_context
__all__ = ['FileWriter', ]
@with_context
class FileWriter:
# XXX TODO implement @with_context like this ? Pros and cons ?
class Meta:
contextual = True
def __init__(self, path_or_buf, eol='\n'):
self.path_or_buf = path_or_buf
self.eol = eol
def initialize(self, ctx):
""" todo add lock file ? optional maybe ? """
assert not hasattr(ctx, 'fp'), 'One at a time, baby.'
ctx.fp = open(self.path_or_buf, 'w+')
ctx.first = True
def write(self, fp, line, prefix=''):
fp.write(prefix + line)
def __call__(self, ctx, row):
if ctx.first:
prefix, ctx.first = '', False
else:
prefix = self.eol
self.write(ctx.fp, row, prefix=prefix)
def finalize(self, ctx):
ctx.fp.close()
del ctx.fp, ctx.first