From 746ff52c8a2a2ad33872951e14784940b4be411b Mon Sep 17 00:00:00 2001 From: Faerbit Date: Fri, 1 Mar 2024 23:26:55 +0100 Subject: [PATCH] Force save on exit --- src/fime/data.py | 5 ++--- src/fime/main.py | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/fime/data.py b/src/fime/data.py index 4101ab0..e51d29d 100644 --- a/src/fime/data.py +++ b/src/fime/data.py @@ -61,16 +61,15 @@ class Data(MutableMapping): def _executor(self): while self._trunning: self._tevent.wait(save_delay) - self._save() + self.save() - def _save(self): + def save(self): for key in self._hot_keys: logger.info(f"saving dict {key}") to_write = self._cache[key] # apparently thread-safe with open(self.data_path.format(key), "w+") as f: f.write(json.dumps(to_write)) self._hot_keys = set() - self._saving = False def __delitem__(self, key): return NotImplemented diff --git a/src/fime/main.py b/src/fime/main.py index 8318f5e..e97a5d2 100755 --- a/src/fime/main.py +++ b/src/fime/main.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import os import signal import sys from functools import partial @@ -31,9 +32,9 @@ class App: self.app = QtWidgets.QApplication(sys.argv) self.app.setQuitOnLastWindowClosed(False) - data = Data() - self.tasks = Tasks(data) - lcd = LogCommentsData(data) + self.data = Data() + self.tasks = Tasks(self.data) + lcd = LogCommentsData(self.data) self.log = Log(lcd) self._active_task = self.log.last_log() or "Nothing" @@ -153,7 +154,7 @@ class App: menu_items.append(("Settings", partial(self.open_new_dialog, self.settings))) menu_items.append(("About", partial(self.open_new_dialog, self.about))) - menu_items.append(("Close", self.app.quit)) + menu_items.append(("Close", self.quit_handler)) if self.config.flip_menu: menu_items.reverse() @@ -169,8 +170,10 @@ class App: action.setIcon(get_icon("go-next")) action.triggered.connect(item[1]) - def sigterm_handler(self, signo, _frame): - logger.debug(f'handling signal "{signal.strsignal(signo)}"') + def quit_handler(self, signo=None, _frame=None): + if signo: + logger.debug(f'handling signal "{signal.strsignal(signo)}"') + self.data.save() self.app.quit() def run(self): @@ -178,8 +181,8 @@ class App: # interrupt event loop regularly for signal handling timer.timeout.connect(lambda: None) timer.start(500) - signal.signal(signal.SIGTERM, self.sigterm_handler) - signal.signal(signal.SIGINT, self.sigterm_handler) + signal.signal(signal.SIGTERM, self.quit_handler) + signal.signal(signal.SIGINT, self.quit_handler) if PYSIDE_6: self.app.exec() else: