Skip to content

Commit

Permalink
- Serialize datetime.datetime objects to dict instead of isoformat to
Browse files Browse the repository at this point in the history
preserve Python 3.6 compatibility
  • Loading branch information
bdutro committed Oct 5, 2019
1 parent a86d78f commit 41cb08a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
27 changes: 26 additions & 1 deletion labelplus/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,5 +347,30 @@ def recurse(dict_in, dict_out, pos_in, pos_out):

return dict_out


def cmp(a, b):
return (a > b) - (a < b)
return (a > b) - (a < b)


def serialize_datetime(dt):
utc_time = dt.time()
return {'Y': dt.year,
'M': dt.month,
'D': dt.day,
'H': utc_time.hour,
'm': utc_time.minute,
's': utc_time.second,
'u': utc_time.microsecond,
'f': utc_time.fold}


def deserialize_datetime(d):
return datetime.datetime(d['Y'],
d['M'],
d['D'],
hour=d['H'],
minute=d['m'],
second=d['s'],
microsecond=d['u'],
tzinfo=None,
fold=d['f'])
5 changes: 3 additions & 2 deletions labelplus/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
from labelplus.common import LABEL_UPDATE_TYPE_FULL
from labelplus.common import LabelPlusError
from labelplus.common import cmp
from labelplus.common import serialize_datetime, deserialize_datetime


from labelplus.common.literals import (
Expand Down Expand Up @@ -550,7 +551,7 @@ def get_labels_data(self, timestamp=None):
def get_label_updates_dict(self, since=None):

if since:
t = datetime.datetime.fromisoformat(since)
t = deserialize_datetime(since)
else:
t = labelplus.common.DATETIME_010101

Expand All @@ -560,7 +561,7 @@ def get_label_updates_dict(self, since=None):
if t <= last_changed:
return {
"type": LABEL_UPDATE_TYPE_FULL,
"timestamp": datetime.datetime.now().isoformat(),
"timestamp": serialize_datetime(datetime.datetime.now()),
"data": self._get_labels_data()
}
else:
Expand Down
5 changes: 3 additions & 2 deletions labelplus/gtkui/gtkui.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from deluge.plugins.pluginbase import Gtk3PluginBase

from labelplus.common import LabelPlusError
from labelplus.common import serialize_datetime, deserialize_datetime
from labelplus.gtkui.common.label_store import LabelStore
from labelplus.gtkui.extensions.add_torrent_ext import AddTorrentExt
from labelplus.gtkui.extensions.preferences_ext import PreferencesExt
Expand Down Expand Up @@ -370,7 +371,7 @@ def process_result(result):
labelplus.common.clean_calls(self._calls)

if self.initialized:
iso_time = self.last_updated.isoformat()
iso_time = serialize_datetime(self.last_updated)
deferred = client.labelplus.get_label_updates_dict(iso_time)
labelplus.common.deferred_timeout(deferred, REQUEST_TIMEOUT, on_timeout,
process_result, process_result)
Expand All @@ -386,7 +387,7 @@ def _update_store(self, result):
log.debug("Update: Type: %s, Timestamp: %s", update['type'],
update['timestamp'])

self.last_updated = datetime.datetime.fromisoformat(update['timestamp'])
self.last_updated = deserialize_datetime(update['timestamp'])
self.store.update(update['data'])

for func in list(self._update_funcs):
Expand Down

0 comments on commit 41cb08a

Please sign in to comment.