diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-18 23:56:48 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2016-06-18 23:56:48 +0300 |
commit | 48b19f2c9fec8a7e2f26ddd058c794d4f5a33894 (patch) | |
tree | 23f397fdad7a38ea2f7d3890593fddedd0572867 /vk/tracking/db/record.py | |
parent | README update (diff) | |
download | vk-scripts-48b19f2c9fec8a7e2f26ddd058c794d4f5a33894.tar.gz vk-scripts-48b19f2c9fec8a7e2f26ddd058c794d4f5a33894.zip |
vk.utils.tracking -> vk.tracking
Diffstat (limited to 'vk/tracking/db/record.py')
-rw-r--r-- | vk/tracking/db/record.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/vk/tracking/db/record.py b/vk/tracking/db/record.py new file mode 100644 index 0000000..93be97c --- /dev/null +++ b/vk/tracking/db/record.py @@ -0,0 +1,98 @@ +# 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 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 |