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 | |
parent | refactoring (diff) | |
download | vk-scripts-f39f18c94060642859900bdb04cb4df2164bc8b4.tar.gz vk-scripts-f39f18c94060642859900bdb04cb4df2164bc8b4.zip |
put format-specific db writers/readers together
Diffstat (limited to 'vk/utils/tracking/db/backend')
-rw-r--r-- | vk/utils/tracking/db/backend/__init__.py | 5 | ||||
-rw-r--r-- | vk/utils/tracking/db/backend/csv.py | 63 | ||||
-rw-r--r-- | vk/utils/tracking/db/backend/log.py | 75 | ||||
-rw-r--r-- | vk/utils/tracking/db/backend/null.py | 37 |
4 files changed, 180 insertions, 0 deletions
diff --git a/vk/utils/tracking/db/backend/__init__.py b/vk/utils/tracking/db/backend/__init__.py new file mode 100644 index 0000000..4b3c278 --- /dev/null +++ b/vk/utils/tracking/db/backend/__init__.py @@ -0,0 +1,5 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +__all__ = 'csv', 'log', 'null' 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 diff --git a/vk/utils/tracking/db/backend/log.py b/vk/utils/tracking/db/backend/log.py new file mode 100644 index 0000000..625257b --- /dev/null +++ b/vk/utils/tracking/db/backend/log.py @@ -0,0 +1,75 @@ +# Copyright 2016 Egor Tensin <Egor.Tensin@gmail.com> +# This file is licensed under the terms of the MIT License. +# See LICENSE.txt for details. + +import logging + +class Writer: + def __init__(self, fd): + self._logger = logging.getLogger(__file__) + self._logger.setLevel(logging.INFO) + handler = logging.StreamHandler(fd) + handler.setFormatter(logging.Formatter( + fmt='[%(asctime)s] %(message)s', + datefmt='%Y-%m-%d %H:%M:%S')) + self._logger.addHandler(handler) + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def info(self, msg): + self._logger.info(msg) + + def exception(self, e): + self._logger.exception(e) + + def on_initial_status(self, user): + if user.is_online(): + self.info(self._format_user_is_online(user)) + else: + self.info(self._format_user_is_offline(user)) + self.info(self._format_user_last_seen(user)) + + def on_status_update(self, user): + if user.is_online(): + self.info(self._format_user_went_online(user)) + else: + self.info(self._format_user_went_offline(user)) + self.info(self._format_user_last_seen(user)) + + def on_connection_error(self, e): + #self.exception(e) + pass + + @staticmethod + def _format_user(user): + if user.has_last_name(): + return '{} {}'.format(user.get_first_name(), user.get_last_name()) + else: + return '{}'.format(user.get_first_name()) + + @staticmethod + def _format_user_is_online(user): + return '{} is ONLINE.'.format(Writer._format_user(user)) + + @staticmethod + def _format_user_is_offline(user): + return '{} is OFFLINE.'.format(Writer._format_user(user)) + + @staticmethod + def _format_user_last_seen(user): + return '{} was last seen at {} using {}.'.format( + Writer._format_user(user), + user.get_last_seen_time_local(), + user.get_last_seen_platform().get_description_for_sentence()) + + @staticmethod + def _format_user_went_online(user): + return '{} went ONLINE.'.format(Writer._format_user(user)) + + @staticmethod + def _format_user_went_offline(user): + return '{} went OFFLINE.'.format(Writer._format_user(user)) diff --git a/vk/utils/tracking/db/backend/null.py b/vk/utils/tracking/db/backend/null.py new file mode 100644 index 0000000..139a9f0 --- /dev/null +++ b/vk/utils/tracking/db/backend/null.py @@ -0,0 +1,37 @@ +# 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 + +class Writer: + def __init__(self, fd): + pass + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def on_initial_status(self, user): + pass + + def on_status_update(self, user): + pass + + def on_connection_error(self, e): + pass + +class Reader(Iterable): + def __init__(self, fd): + pass + + def __enter__(self): + return self + + def __exit__(self, *args): + pass + + def __iter__(self): + pass |