From 1c0e2c13d4f0acbe42e43d886df4f1067506efed Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 28 Jan 2017 02:40:27 +0300 Subject: vk: move file i/o to a separate module --- vk/tracking/db/backend/csv.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'vk/tracking/db/backend') diff --git a/vk/tracking/db/backend/csv.py b/vk/tracking/db/backend/csv.py index e607774..5b2cc7d 100644 --- a/vk/tracking/db/backend/csv.py +++ b/vk/tracking/db/backend/csv.py @@ -4,23 +4,20 @@ # Distributed under the MIT License. from collections.abc import Iterable -import csv +from ..io import InputReaderCSV, OutputWriterCSV from ..record import Record from ..timestamp import Timestamp class Writer: def __init__(self, fd): - self._fd = fd - self._writer = csv.writer(fd, lineterminator='\n') + self._writer = OutputWriterCSV(fd) def on_initial_status(self, user): self._write_record(user) - self._fd.flush() def on_status_update(self, user): self._write_record(user) - self._fd.flush() def on_connection_error(self, e): pass @@ -28,10 +25,7 @@ class Writer: def _write_record(self, user): if not self: return - self._write_row(self._record_to_row(Record.from_user(user))) - - def _write_row(self, row): - self._writer.writerow(row) + self._writer.write_row(self._record_to_row(Record.from_user(user))) @staticmethod def _record_to_row(record): @@ -39,7 +33,7 @@ class Writer: class Reader(Iterable): def __init__(self, fd): - self._reader = csv.reader(fd) + self._reader = InputReaderCSV(fd) def __iter__(self): return map(Reader._record_from_row, self._reader) -- cgit v1.2.3