From 3dcdbdf2d56fb6a1aede81e8841a24a544d3bc65 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sun, 29 Jan 2023 10:36:56 +0100 Subject: [PATCH] skip update of big text if file was not modified --- src/ui/bigtext/bigtext.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ui/bigtext/bigtext.py b/src/ui/bigtext/bigtext.py index fcb4e8a..eee90d5 100644 --- a/src/ui/bigtext/bigtext.py +++ b/src/ui/bigtext/bigtext.py @@ -1,3 +1,4 @@ +import logging import math import os import time @@ -26,20 +27,31 @@ from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler from src.i18n import _ +log = logging.getLogger("bigtext") class FileObserver(FileSystemEventHandler): def __init__(self, big_text): super(FileObserver, self).__init__() self.big_text = big_text + self._last_mtime = -1 def on_modified(self, event): # slow down the updates. This is needed, because the file is modified # constantly, which would lead to constant re-rendering, which would # block the UI thread and make the UI unresponsive. # Note: we don't miss events, because they are queued and de-duplicated - time.sleep(0.5) - self.big_text.trigger_update.emit() + if not event.is_directory: + try: + mtime = os.stat(event.src_path).st_mtime + if mtime != self._last_mtime: + self._last_mtime = mtime + time.sleep(0.5) + self.big_text.trigger_update.emit() + except FileNotFoundError: + # ignore: happens when closing the application, because tmp files are deleted, + # which triggers a modification event + pass class FileWatchdogThread(QRunnable):