Fix various edge cases.

This commit is contained in:
Faerbit 2020-02-25 20:20:37 +01:00
parent 3e4ef88638
commit 8eba8ac60e

14
data.py
View File

@ -49,7 +49,7 @@ class Data(MutableMapping):
if not os.path.exists(data_dir_path):
os.mkdir(data_dir_path)
self._cache = {}
self._hot_keys = []
self._hot_keys = set()
self._trunning = False
self._tevent = Event()
self._thread = None
@ -71,7 +71,7 @@ class Data(MutableMapping):
def __setitem__(self, key, value):
self._cache[key] = value
self._hot_keys.append(key)
self._hot_keys.add(key)
self._schedule_save()
def _schedule_save(self):
@ -92,7 +92,7 @@ class Data(MutableMapping):
to_write = self._cache[key] # apparently thread-safe
with open(data_path.format(key), "w+") as f:
f.write(json.dumps(to_write))
self._hot_keys = []
self._hot_keys = set()
self._saving = False
def __delitem__(self, key):
@ -143,6 +143,8 @@ class Log:
month = self._data[pdate.strftime("%Y-%m")]
del month[pdate.strftime("%d")][-1]
self._data[pdate.strftime("%Y-%m")] = month
if len(self._data[pdate.strftime("%Y-%m")][pdate.strftime("%d")]) == 0:
return None
last = base64.b64decode(
self._data[pdate.strftime("%Y-%m")][pdate.strftime("%d")][-1].split()[1].encode("utf-8")).decode("utf-8")
return last
@ -160,6 +162,8 @@ class Report:
def report(self):
tmp = []
if self._date.strftime("%Y-%m") in self._data \
and self._date.strftime("%d") in self._data[self._date.strftime("%Y-%m")]:
for e in self._data[self._date.strftime("%Y-%m")][self._date.strftime("%d")]:
tstr, b64str = e.split()
task = base64.b64decode(b64str.encode("utf-8")).decode("utf-8")
@ -193,6 +197,8 @@ class Report:
report = report[:-3] # cut off sum display and end time
else:
report = report[:-2] # cut off sum display
if not report:
return
save_list = []
for tstr, ttime, _ in report:
b64str = base64.b64encode(tstr.encode("utf-8")).decode("utf-8")
@ -206,6 +212,8 @@ class Report:
self._data[self._date.strftime("%Y-%m")] = month
def prev_next_avail(self):
if self._date.strftime("%Y-%m") not in self._data:
return False, False
prev = (self._date - timedelta(days=1)).strftime("%d") in self._data[self._date.strftime("%Y-%m")]
_next = (self._date + timedelta(days=1)).strftime("%d") in self._data[self._date.strftime("%Y-%m")]
return prev, _next