From 48b19f2c9fec8a7e2f26ddd058c794d4f5a33894 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 18 Jun 2016 23:56:48 +0300 Subject: vk.utils.tracking -> vk.tracking --- vk/utils/tracking/db/__init__.py | 7 --- vk/utils/tracking/db/backend/__init__.py | 5 -- vk/utils/tracking/db/backend/csv.py | 63 -------------------- vk/utils/tracking/db/backend/log.py | 75 ------------------------ vk/utils/tracking/db/backend/null.py | 37 ------------ vk/utils/tracking/db/format.py | 35 ------------ vk/utils/tracking/db/record.py | 98 -------------------------------- vk/utils/tracking/db/timestamp.py | 34 ----------- 8 files changed, 354 deletions(-) delete mode 100644 vk/utils/tracking/db/__init__.py delete mode 100644 vk/utils/tracking/db/backend/__init__.py delete mode 100644 vk/utils/tracking/db/backend/csv.py delete mode 100644 vk/utils/tracking/db/backend/log.py delete mode 100644 vk/utils/tracking/db/backend/null.py delete mode 100644 vk/utils/tracking/db/format.py delete mode 100644 vk/utils/tracking/db/record.py delete mode 100644 vk/utils/tracking/db/timestamp.py (limited to 'vk/utils/tracking/db') diff --git a/vk/utils/tracking/db/__init__.py b/vk/utils/tracking/db/__init__.py deleted file mode 100644 index 9e6b74a..0000000 --- a/vk/utils/tracking/db/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright 2016 Egor Tensin -# This file is licensed under the terms of the MIT License. -# See LICENSE.txt for details. - -from .format import Format - -__all__ = 'format' diff --git a/vk/utils/tracking/db/backend/__init__.py b/vk/utils/tracking/db/backend/__init__.py deleted file mode 100644 index 4b3c278..0000000 --- a/vk/utils/tracking/db/backend/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -# Copyright 2016 Egor Tensin -# 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 deleted file mode 100644 index 10a504f..0000000 --- a/vk/utils/tracking/db/backend/csv.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2016 Egor Tensin -# 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 deleted file mode 100644 index 625257b..0000000 --- a/vk/utils/tracking/db/backend/log.py +++ /dev/null @@ -1,75 +0,0 @@ -# Copyright 2016 Egor Tensin -# 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 deleted file mode 100644 index 139a9f0..0000000 --- a/vk/utils/tracking/db/backend/null.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2016 Egor Tensin -# 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 diff --git a/vk/utils/tracking/db/format.py b/vk/utils/tracking/db/format.py deleted file mode 100644 index 4856094..0000000 --- a/vk/utils/tracking/db/format.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2016 Egor Tensin -# This file is licensed under the terms of the MIT License. -# See LICENSE.txt for details. - -from enum import Enum - -from .backend import * - -class Format(Enum): - CSV = 'csv' - LOG = 'log' - NULL = 'null' - - def create_writer(self, fd): - if self is Format.CSV: - return csv.Writer(fd) - elif self is Format.LOG: - return log.Writer(fd) - elif self is Format.NULL: - return null.Writer(fd) - else: - raise NotImplementedError('unsupported database format: ' + str(self)) - - def create_reader(self, fd): - if self is Format.CSV: - return csv.Reader(fd) - elif self is Format.LOG: - raise NotImplementedError() - elif self is Format.NULL: - return null.Reader(fd) - else: - raise NotImplementedError('unsupported database format: ' + str(self)) - - def __str__(self): - return self.value diff --git a/vk/utils/tracking/db/record.py b/vk/utils/tracking/db/record.py deleted file mode 100644 index 93be97c..0000000 --- a/vk/utils/tracking/db/record.py +++ /dev/null @@ -1,98 +0,0 @@ -# Copyright 2016 Egor Tensin -# This file is licensed under the terms of the MIT License. -# See LICENSE.txt for details. - -from collections import OrderedDict -from collections.abc import MutableMapping -from datetime import datetime - -from .timestamp import Timestamp -from vk.user import LastSeen, LastSeenField, User, UserField - -class Record(MutableMapping): - FIELDS = ( - UserField.UID, - UserField.FIRST_NAME, - UserField.LAST_NAME, - UserField.SCREEN_NAME, - UserField.ONLINE, - LastSeenField.TIME, - LastSeenField.PLATFORM, - ) - - def __init__(self, timestamp=None, fields=None): - if timestamp is None: - timestamp = Timestamp() - if fields is None: - fields = OrderedDict() - self._timestamp = timestamp - self._fields = fields - - def __getitem__(self, field): - if field is LastSeenField.TIME: - return Timestamp(self._fields[field]) - return self._fields[field] - - def __setitem__(self, field, value): - if field is LastSeenField.TIME: - if isinstance(value, str): - value = Timestamp.from_string(value).dt - elif isinstance(value, Timestamp): - value = value.dt - elif isinstance(value, datetime): - pass - else: - raise TypeError() - if isinstance(field, LastSeenField): - self._fields[field] = LastSeen.parse(field, value) - elif isinstance(field, UserField): - self._fields[field] = User.parse(field, value) - else: - raise TypeError() - - def __delitem__(self, field): - del self._fields[field] - - def __iter__(self): - return iter(self._fields) - - def __len__(self): - return len(self._fields) - - def get_timestamp(self): - return self._timestamp - - @staticmethod - def from_user(user): - record = Record() - for field in Record.FIELDS: - if isinstance(field, UserField): - record[field] = user[field] - elif isinstance(field, LastSeenField): - record[field] = user.get_last_seen()[field] - else: - assert False - return record - - def _update_last_seen_field(self, last_seen, field): - if field is LastSeenField.TIME: - last_seen[field] = self[field].dt - else: - last_seen[field] = self[field] - - def _update_user_field(self, user, field): - user[field] = self[field] - - def to_user(self): - user = User() - last_seen = LastSeen() - for field in self: - if isinstance(field, LastSeenField): - self._update_last_seen_field(last_seen, field) - elif isinstance(field, UserField): - self._update_user_field(user, field) - else: - assert False - if len(last_seen): - user.set_last_seen(last_seen) - return user diff --git a/vk/utils/tracking/db/timestamp.py b/vk/utils/tracking/db/timestamp.py deleted file mode 100644 index 35cf5b3..0000000 --- a/vk/utils/tracking/db/timestamp.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright 2016 Egor Tensin -# This file is licensed under the terms of the MIT License. -# See LICENSE.txt for details. - -from datetime import datetime, timezone - -class Timestamp: - @staticmethod - def _new(): - return datetime.utcnow() - - @staticmethod - def _is_timezone_aware(dt): - return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None - - @staticmethod - def _lose_timezone(dt): - if Timestamp._is_timezone_aware(dt): - return dt.astimezone(timezone.utc).replace(tzinfo=None) - return dt - - def __init__(self, dt=None): - if dt is None: - dt = self._new() - dt = dt.replace(microsecond=0) - dt = self._lose_timezone(dt) - self.dt = dt - - @staticmethod - def from_string(s): - return Timestamp(datetime.strptime(s, '%Y-%m-%dT%H:%M:%SZ')) - - def __str__(self): - return self.dt.isoformat() + 'Z' -- cgit v1.2.3