aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/vk/utils/tracking/db/record.py
blob: 4748a37c329982e495a78ea6fe7c6d0e029b01f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# 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, timezone

from vk.user import LastSeen, User, UserField

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'

    @staticmethod
    def from_last_seen(ls):
        return Timestamp(ls.get_time())

    def to_last_seen(self):
        ls = LastSeen()
        ls.set_time(self._dt)
        return ls

class Record(MutableMapping):
    FIELDS = (
        UserField.UID,
        UserField.FIRST_NAME,
        UserField.LAST_NAME,
        UserField.SCREEN_NAME,
        UserField.ONLINE,
        UserField.LAST_SEEN,
    )

    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 UserField.LAST_SEEN:
            return Timestamp.from_last_seen(self._fields[field])
        return self._fields[field]

    def __setitem__(self, field, value):
        if field is UserField.LAST_SEEN:
            if isinstance(value, str):
                value = Timestamp.from_string(value).to_last_seen()
            elif isinstance(value, Timestamp):
                value = value.to_last_seen()
            elif isinstance(value, LastSeen):
                pass
            else:
                raise TypeError()
        self._fields[field] = User.parse(field, value)

    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):
        instance = Record()
        for field in Record.FIELDS:
            instance[field] = user[field]
        return instance

    def to_user(self):
        user = User()
        for field in self:
            user[field] = self[field]
        return user