From 41cb08a0aa80a4a8d3265cac51d4a7f177c9f78c Mon Sep 17 00:00:00 2001 From: Brett Dutro Date: Sat, 5 Oct 2019 14:12:16 -0500 Subject: [PATCH] - Serialize datetime.datetime objects to dict instead of isoformat to preserve Python 3.6 compatibility --- labelplus/common/__init__.py | 27 ++++++++++++++++++++++++++- labelplus/core/core.py | 5 +++-- labelplus/gtkui/gtkui.py | 5 +++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/labelplus/common/__init__.py b/labelplus/common/__init__.py index f22c768..23225df 100644 --- a/labelplus/common/__init__.py +++ b/labelplus/common/__init__.py @@ -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']) diff --git a/labelplus/core/core.py b/labelplus/core/core.py index 196fae4..0bebd1d 100644 --- a/labelplus/core/core.py +++ b/labelplus/core/core.py @@ -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 ( @@ -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 @@ -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: diff --git a/labelplus/gtkui/gtkui.py b/labelplus/gtkui/gtkui.py index 2bddedb..5c46fb0 100644 --- a/labelplus/gtkui/gtkui.py +++ b/labelplus/gtkui/gtkui.py @@ -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 @@ -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) @@ -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):