diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-18 02:22:36 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-18 02:22:36 +0300 |
commit | f39f18c94060642859900bdb04cb4df2164bc8b4 (patch) | |
tree | eb29d7577e95c32a0894c134214b86db44c3b074 /vk/utils/tracking/db/backend/csv.py | |
parent | refactoring (diff) | |
download | vk-scripts-f39f18c94060642859900bdb04cb4df2164bc8b4.tar.gz vk-scripts-f39f18c94060642859900bdb04cb4df2164bc8b4.zip |
put format-specific db writers/readers together
Diffstat (limited to '')
-rw-r--r-- | vk/utils/tracking/db/backend/csv.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/vk/utils/tracking/db/backend/csv.py b/vk/utils/tracking/db/backend/csv.py new file mode 100644 index 0000000..10a504f --- /dev/null +++ b/vk/utils/tracking/db/backend/csv.py @@ -0,0 +1,63 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +from collections.abc import Iterable +import csv + +from ..record import Record +from ..timestamp import Timestamp + +class Writer: + def __init__(self, fd): + self._fd = fd + self._writer = csv.writer(fd, lineterminator='\n') + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + 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 + + 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) + + @staticmethod + def _record_to_row(record): + return [str(record.get_timestamp())] + [str(record[field]) for field in record] + +class Reader(Iterable): + def __init__(self, fd): + self._reader = csv.reader(fd) + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def __iter__(self): + return map(Reader._record_from_row, self._reader) + + @staticmethod + def _record_from_row(row): + record = Record(Timestamp.from_string(row[0])) + for i in range(len(Record.FIELDS)): + record[Record.FIELDS[i]] = row[i + 1] + return record |