diff options
Diffstat (limited to 'vk/user.py')
-rw-r--r-- | vk/user.py | 102 |
1 files changed, 80 insertions, 22 deletions
@@ -4,6 +4,7 @@ from datetime import datetime from enum import Enum +from numbers import Real, Integral class Field(Enum): UID = 'uid' @@ -20,51 +21,108 @@ class User: def __init__(self, impl): self._impl = impl + def __str__(self): + return str(self._impl) + + def __eq__(self, other): + return self.get_uid() == other.get_uid() + + def __hash__(self): + return hash(self.get_uid()) + def __iter__(self): return iter(self._impl) + def __contains__(self, field): + if field is Field.LAST_SEEN: + return self._has_last_seen() + return self._normalize_field(field) in self._impl + def __getitem__(self, field): - if isinstance(field, Field): - field = field.value - return self._impl[field] + if field is Field.LAST_SEEN: + return self._get_last_seen() + return self._impl[self._normalize_field(field)] - def __contains__(self, field): + def __setitem__(self, field, value): + if field is Field.LAST_SEEN: + self._set_last_seen(value) + else: + self._impl[self._normalize_field(field)] = value + + @staticmethod + def _normalize_field(field): if isinstance(field, Field): - field = field.value - return field in self._impl + return field.value + return field def get_uid(self): - return self._impl[Field.UID.value] + return self[Field.UID] def get_first_name(self): - return self._impl[Field.FIRST_NAME.value] + return self[Field.FIRST_NAME] - def get_last_name(self): - return self._impl[Field.LAST_NAME.value] + def set_first_name(self, name): + self[Field.FIRST_NAME] = name def has_last_name(self): - return Field.LAST_NAME.value in self._impl and self.get_last_name() + return Field.LAST_NAME in self and self.get_last_name() + + def get_last_name(self): + return self[Field.LAST_NAME] + + def set_last_name(self, name): + self[Field.LAST_NAME] = name def has_screen_name(self): - return Field.SCREEN_NAME.value in self._impl + return Field.SCREEN_NAME in self def get_screen_name(self): if self.has_screen_name(): - return self._impl[Field.SCREEN_NAME.value] + return self[Field.SCREEN_NAME] else: return 'id' + str(self.get_uid()) + def set_screen_name(self, name): + self[Field.SCREEN_NAME] = name + + def has_online(self): + return Field.ONLINE in self + def is_online(self): - return self._impl[Field.ONLINE.value] + return bool(self[Field.ONLINE]) - def get_last_seen(self): - return datetime.fromtimestamp(self._impl[Field.LAST_SEEN.value]['time']) + def set_online(self, value=True): + self[Field.ONLINE] = value - def __str__(self): - return repr(self._impl) + @staticmethod + def _last_seen_from_timestamp(t): + return datetime.fromtimestamp(t) - def __hash__(self): - return hash(self.get_uid()) + @staticmethod + def _last_seen_to_timestamp(t): + if isinstance(t, datetime): + return t.timestamp() + elif isinstance(t, Real) or isinstance(t, Integral): + return t + else: + raise TypeError('"last seen" time must be either a `datetime` or a POSIX timestamp') - def __eq__(self, other): - return self.get_uid() == other.get_uid() + def _has_last_seen(self): + return Field.LAST_SEEN.value in self._impl and 'time' in self._impl[Field.LAST_SEEN.value] + + def has_last_seen(self): + return self._has_last_seen() + + def _get_last_seen(self): + return self._last_seen_from_timestamp(self._impl[Field.LAST_SEEN.value]['time']) + + def get_last_seen(self): + return self._get_last_seen() + + def _set_last_seen(self, t): + if Field.LAST_SEEN.value not in self._impl: + self._impl[Field.LAST_SEEN.value] = {} + self._impl[Field.LAST_SEEN.value]['time'] = self._last_seen_to_timestamp(t) + + def set_last_seen(self, t): + self._set_last_seen(t) |