diff --git a/data.py b/data.py index 802e177..36b545d 100644 --- a/data.py +++ b/data.py @@ -39,7 +39,7 @@ class Tasks: self._save() def _save(self): - print("...saving tasks...") + print("... saving tasks ...") encoded_tasks = list(map(lambda x: base64.b64encode(x.encode("utf-8")).decode("utf-8"), self._tasks)) with open(tasks_path, "w+") as f: f.write(json.dumps(encoded_tasks)) @@ -114,6 +114,11 @@ class Log: def __init__(self): self._data = Data() + def cleanup(): + self.log("End") + + atexit.register(cleanup) + def log(self, task, ptime=None): if ptime is None: ptime = datetime.now() @@ -126,11 +131,22 @@ class Log: .append(f"{ptime.strftime('%H:%M')} {base64.b64encode(task.encode('utf-8')).decode('utf-8')}") self._data[ptime.strftime("%Y-%m")] = month - def last_log(self, pdate=date.today()): - if pdate.strftime("%Y-%m") not in self._data or pdate.strftime("%d") not in self._data[pdate.strftime("%Y-%m")]: + def last_log(self, pdate=None): + if pdate is None: + pdate = date.today() + if pdate.strftime("%Y-%m") not in self._data \ + or pdate.strftime("%d") not in self._data[pdate.strftime("%Y-%m")] \ + or len(self._data[pdate.strftime("%Y-%m")][pdate.strftime("%d")]) == 0: return None - return base64.b64decode( + last = base64.b64decode( self._data[pdate.strftime("%Y-%m")][pdate.strftime("%d")][-1].split()[1].encode("utf-8")).decode("utf-8") + if last == "End": + month = self._data[pdate.strftime("%Y-%m")] + del month[pdate.strftime("%d")][-1] + self._data[pdate.strftime("%Y-%m")] = month + last = base64.b64decode( + self._data[pdate.strftime("%Y-%m")][pdate.strftime("%d")][-1].split()[1].encode("utf-8")).decode("utf-8") + return last def report(self, pdate=None): if pdate is None: @@ -150,6 +166,8 @@ class Report: 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(): + tmp.append(("End", datetime.now())) ret = [] dsum = timedelta() @@ -157,13 +175,13 @@ class Report: task, start_time = t if i < len(tmp) - 1: end_time = tmp[i+1][1] + duration = end_time - start_time + dsum += duration + dhours, rem = divmod(duration.seconds, 3600) + dmins, _ = divmod(rem, 60) + ret.append((task, start_time.strftime("%H:%M"), f"{dhours:02d}:{dmins:02d}")) else: - end_time = datetime.now() - duration = end_time - start_time - dsum += duration - dhours, rem = divmod(duration.seconds, 3600) - dmins, _ = divmod(rem, 60) - ret.append((task, start_time.strftime("%H:%M"), f"{dhours:02d}:{dmins:02d}")) + ret.append((task, start_time.strftime("%H:%M"), "")) ret.append(("", "", "")) dhours, rem = divmod(dsum.seconds, 3600) @@ -172,7 +190,10 @@ class Report: return ret def save(self, report): - report = report[:-2] # cut of sum display + if self._date == date.today(): + report = report[:-3] # cut off sum display and end time + else: + report = report[:-2] # cut off sum display save_list = [] for tstr, ttime, _ in report: b64str = base64.b64encode(tstr.encode("utf-8")).decode("utf-8") diff --git a/main.py b/main.py index a01208d..e84d5b5 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,7 @@ class App: self.tasks = Tasks() self.log = Log() - self.active_task = self.log.last_log() or "Pause" + self.active_task = self.log.last_log() or "Nothing" icon = QtGui.QIcon.fromTheme("appointment-new") @@ -34,7 +34,7 @@ class App: self.taskEdit.accepted.connect(self.tasks_edited) self.reportDialog = Report(None) - self.taskEdit.accepted.connect(self.tasks_edited) + self.reportDialog.accepted.connect(self.report_done) @QtCore.Slot() def tasks_edited(self): @@ -43,12 +43,12 @@ class App: @QtCore.Slot() def report_done(self): - self.active_task = self.log.last_log() or "Pause" + self.active_task = self.log.last_log() or "Nothing" self.update_tray_menu() def change_task(self, task): self.active_task = task - if task == "Pause": + if task == "Nothing": return self.log.log(task) self.update_tray_menu() @@ -56,7 +56,7 @@ class App: def update_tray_menu(self): self.menu.clear() tasks = list(self.tasks.tasks) - tasks.append("Pause") + tasks.append("Nothing") for t in tasks: a = self.menu.addAction(t) diff --git a/report.py b/report.py index 15dca02..daeacec 100644 --- a/report.py +++ b/report.py @@ -65,7 +65,7 @@ class Report(QtWidgets.QDialog): self.tableWidget.setItem(row, 2, item2) item0.setFlags(item0.flags() & QtCore.Qt.ItemIsEnabled) item2.setFlags(item2.flags() & QtCore.Qt.ItemIsEnabled) - if row > len(self._report_data) - 3: + if row > len(self._report_data) - 4: item1.setFlags(item1.flags() & QtCore.Qt.ItemIsEnabled) self.tableWidget.resizeColumnsToContents() @@ -87,7 +87,7 @@ class Report(QtWidgets.QDialog): @QtCore.Slot() def del_log(self): row = self.tableWidget.currentRow() - if row > len(self._report_data) - 3: + if row > len(self._report_data) - 4: return del self._report_data[row] self.refresh_table() @@ -101,6 +101,7 @@ class Report(QtWidgets.QDialog): def previous(self): self._report.save(self._report_data) self._report.previous() + self._report_data = self._report.report() self.refresh_table() self.update_prev_next() @@ -108,5 +109,6 @@ class Report(QtWidgets.QDialog): def next(self): self._report.save(self._report_data) self._report.next() + self._report_data = self._report.report() self.refresh_table() self.update_prev_next()