Fix various edge cases.
This commit is contained in:
parent
3e4ef88638
commit
8eba8ac60e
24
data.py
24
data.py
@ -49,7 +49,7 @@ class Data(MutableMapping):
|
|||||||
if not os.path.exists(data_dir_path):
|
if not os.path.exists(data_dir_path):
|
||||||
os.mkdir(data_dir_path)
|
os.mkdir(data_dir_path)
|
||||||
self._cache = {}
|
self._cache = {}
|
||||||
self._hot_keys = []
|
self._hot_keys = set()
|
||||||
self._trunning = False
|
self._trunning = False
|
||||||
self._tevent = Event()
|
self._tevent = Event()
|
||||||
self._thread = None
|
self._thread = None
|
||||||
@ -71,7 +71,7 @@ class Data(MutableMapping):
|
|||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
self._cache[key] = value
|
self._cache[key] = value
|
||||||
self._hot_keys.append(key)
|
self._hot_keys.add(key)
|
||||||
self._schedule_save()
|
self._schedule_save()
|
||||||
|
|
||||||
def _schedule_save(self):
|
def _schedule_save(self):
|
||||||
@ -92,7 +92,7 @@ class Data(MutableMapping):
|
|||||||
to_write = self._cache[key] # apparently thread-safe
|
to_write = self._cache[key] # apparently thread-safe
|
||||||
with open(data_path.format(key), "w+") as f:
|
with open(data_path.format(key), "w+") as f:
|
||||||
f.write(json.dumps(to_write))
|
f.write(json.dumps(to_write))
|
||||||
self._hot_keys = []
|
self._hot_keys = set()
|
||||||
self._saving = False
|
self._saving = False
|
||||||
|
|
||||||
def __delitem__(self, key):
|
def __delitem__(self, key):
|
||||||
@ -143,6 +143,8 @@ class Log:
|
|||||||
month = self._data[pdate.strftime("%Y-%m")]
|
month = self._data[pdate.strftime("%Y-%m")]
|
||||||
del month[pdate.strftime("%d")][-1]
|
del month[pdate.strftime("%d")][-1]
|
||||||
self._data[pdate.strftime("%Y-%m")] = month
|
self._data[pdate.strftime("%Y-%m")] = month
|
||||||
|
if len(self._data[pdate.strftime("%Y-%m")][pdate.strftime("%d")]) == 0:
|
||||||
|
return None
|
||||||
last = base64.b64decode(
|
last = base64.b64decode(
|
||||||
self._data[pdate.strftime("%Y-%m")][pdate.strftime("%d")][-1].split()[1].encode("utf-8")).decode("utf-8")
|
self._data[pdate.strftime("%Y-%m")][pdate.strftime("%d")][-1].split()[1].encode("utf-8")).decode("utf-8")
|
||||||
return last
|
return last
|
||||||
@ -160,11 +162,13 @@ class Report:
|
|||||||
|
|
||||||
def report(self):
|
def report(self):
|
||||||
tmp = []
|
tmp = []
|
||||||
for e in self._data[self._date.strftime("%Y-%m")][self._date.strftime("%d")]:
|
if self._date.strftime("%Y-%m") in self._data \
|
||||||
tstr, b64str = e.split()
|
and self._date.strftime("%d") in self._data[self._date.strftime("%Y-%m")]:
|
||||||
task = base64.b64decode(b64str.encode("utf-8")).decode("utf-8")
|
for e in self._data[self._date.strftime("%Y-%m")][self._date.strftime("%d")]:
|
||||||
start_time = datetime.combine(self._date, datetime.strptime(tstr, "%H:%M").time())
|
tstr, b64str = e.split()
|
||||||
tmp.append((task, start_time))
|
task = base64.b64decode(b64str.encode("utf-8")).decode("utf-8")
|
||||||
|
start_time = datetime.combine(self._date, datetime.strptime(tstr, "%H:%M").time())
|
||||||
|
tmp.append((task, start_time))
|
||||||
if self._date == date.today():
|
if self._date == date.today():
|
||||||
tmp.append(("End", datetime.now()))
|
tmp.append(("End", datetime.now()))
|
||||||
|
|
||||||
@ -193,6 +197,8 @@ class Report:
|
|||||||
report = report[:-3] # cut off sum display and end time
|
report = report[:-3] # cut off sum display and end time
|
||||||
else:
|
else:
|
||||||
report = report[:-2] # cut off sum display
|
report = report[:-2] # cut off sum display
|
||||||
|
if not report:
|
||||||
|
return
|
||||||
save_list = []
|
save_list = []
|
||||||
for tstr, ttime, _ in report:
|
for tstr, ttime, _ in report:
|
||||||
b64str = base64.b64encode(tstr.encode("utf-8")).decode("utf-8")
|
b64str = base64.b64encode(tstr.encode("utf-8")).decode("utf-8")
|
||||||
@ -206,6 +212,8 @@ class Report:
|
|||||||
self._data[self._date.strftime("%Y-%m")] = month
|
self._data[self._date.strftime("%Y-%m")] = month
|
||||||
|
|
||||||
def prev_next_avail(self):
|
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")]
|
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")]
|
_next = (self._date + timedelta(days=1)).strftime("%d") in self._data[self._date.strftime("%Y-%m")]
|
||||||
return prev, _next
|
return prev, _next
|
||||||
|
Loading…
Reference in New Issue
Block a user