API is cleaner this way, easier to understand the purpose of methods (#2).

This commit is contained in:
Romain Dorgueil
2016-12-28 11:40:03 +01:00
parent 67e25b92e1
commit cebb8df173
3 changed files with 17 additions and 19 deletions

View File

@ -46,17 +46,17 @@ class FileHandler:
class Reader(FileHandler): class Reader(FileHandler):
def __call__(self, ctx): def __call__(self, ctx):
yield from self.handle(ctx) yield from self.read(ctx)
def handle(self, ctx): def read(self, ctx):
raise NotImplementedError('Abstract.') raise NotImplementedError('Abstract.')
class Writer(FileHandler): class Writer(FileHandler):
def __call__(self, ctx, row): def __call__(self, ctx, row):
return self.handle(ctx, row) return self.write(ctx, row)
def handle(self, ctx, row): def write(self, ctx, row):
raise NotImplementedError('Abstract.') raise NotImplementedError('Abstract.')
@ -71,7 +71,7 @@ class FileReader(Reader):
mode = 'r' mode = 'r'
def handle(self, ctx): def read(self, ctx):
""" """
Write a row on the next line of file pointed by `ctx.file`. Write a row on the next line of file pointed by `ctx.file`.
Prefix is used for newlines. Prefix is used for newlines.
@ -98,7 +98,7 @@ class FileWriter(Writer):
super().initialize(ctx) super().initialize(ctx)
ctx.line = 0 ctx.line = 0
def handle(self, ctx, row): def write(self, ctx, row):
""" """
Write a row on the next line of opened file in context. Write a row on the next line of opened file in context.
@ -106,10 +106,10 @@ class FileWriter(Writer):
:param str row: :param str row:
:param str prefix: :param str prefix:
""" """
self.write(ctx.file, (self.eol if ctx.line else '') + row) self._write_line(ctx.file, (self.eol if ctx.line else '') + row)
ctx.line += 1 ctx.line += 1
def write(self, fp, line): def _write_line(self, fp, line):
return fp.write(line) return fp.write(line)
def finalize(self, ctx): def finalize(self, ctx):

View File

@ -10,7 +10,7 @@ class JsonHandler:
class JsonReader(JsonHandler, FileReader): class JsonReader(JsonHandler, FileReader):
def handle(self, ctx): def read(self, ctx):
for line in json.load(ctx.file): for line in json.load(ctx.file):
yield line yield line
@ -20,14 +20,14 @@ class JsonWriter(JsonHandler, FileWriter):
super().initialize(ctx) super().initialize(ctx)
ctx.file.write('[\n') ctx.file.write('[\n')
def handle(self, ctx, row): def write(self, ctx, row):
""" """
Write a json row on the next line of file pointed by ctx.file. Write a json row on the next line of file pointed by ctx.file.
:param ctx: :param ctx:
:param row: :param row:
""" """
return super().handle(ctx, json.dumps(row)) return super().write(ctx, json.dumps(row))
def finalize(self, ctx): def finalize(self, ctx):
ctx.file.write('\n]') ctx.file.write('\n]')

View File

@ -8,17 +8,15 @@ from bonobo.util.tokens import BEGIN, END
def test_write_json_to_file(tmpdir): def test_write_json_to_file(tmpdir):
file = tmpdir.join('output.json') file = tmpdir.join('output.json')
json_writer = JsonWriter(str(file)) writer = JsonWriter(str(file))
context = ComponentExecutionContext(json_writer, None) context = ComponentExecutionContext(writer, None)
context.initialize() context.initialize()
context.recv(BEGIN, Bag({'foo': 'bar'}), END) context.recv(BEGIN, Bag({'foo': 'bar'}), END)
context.step() context.step()
context.finalize() context.finalize()
assert file.read() == '''[ assert file.read() == '[\n{"foo": "bar"}\n]'
{"foo": "bar"}
]'''
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
getattr(context, 'file') getattr(context, 'file')
@ -29,11 +27,11 @@ def test_write_json_to_file(tmpdir):
def test_write_json_without_initializer_should_not_work(tmpdir): def test_write_json_without_initializer_should_not_work(tmpdir):
file = tmpdir.join('output.json') file = tmpdir.join('output.json')
json_writer = JsonWriter(str(file)) writer = JsonWriter(str(file))
context = ComponentExecutionContext(json_writer, None) context = ComponentExecutionContext(writer, None)
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
json_writer(context, {'foo': 'bar'}) writer(context, {'foo': 'bar'})
def test_read_json_from_file(tmpdir): def test_read_json_from_file(tmpdir):