watch for file changes
This commit is contained in:
54
bigtext.py
54
bigtext.py
@@ -1,5 +1,7 @@
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
from typing import Optional, List
|
||||
import PyQt6.QtGui
|
||||
from PyQt6 import QtGui
|
||||
@@ -18,13 +20,60 @@ from logFileModel import LogFileModel
|
||||
import re
|
||||
|
||||
from settings import Settings
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
import threading
|
||||
|
||||
|
||||
class FileObserver(FileSystemEventHandler):
|
||||
counter = 0
|
||||
def __init__(self, big_text):
|
||||
super(FileObserver, self).__init__()
|
||||
self.big_text = big_text
|
||||
|
||||
def on_modified(self, event):
|
||||
#print("file modified ", self.counter," ", time.time())
|
||||
self.counter+=1
|
||||
time.sleep(0.5)
|
||||
self.big_text.update()
|
||||
|
||||
|
||||
class FileWatchdogThread(QRunnable):
|
||||
|
||||
observer = None
|
||||
|
||||
def __init__(self, big_text, file: str):
|
||||
super(FileWatchdogThread, self).__init__()
|
||||
self.file = file
|
||||
self.big_text = big_text
|
||||
|
||||
def run(self) -> None:
|
||||
print("observer started for file ", self.file)
|
||||
if self.observer:
|
||||
print("observer already exists")
|
||||
self.observer = Observer()
|
||||
self.observer.schedule(FileObserver(self.big_text), self.file)
|
||||
self.observer.start()
|
||||
|
||||
def destruct(self):
|
||||
self.observer.stop()
|
||||
try:
|
||||
self.observer.join()
|
||||
except:
|
||||
# probably: RuntimeError: cannot join thread before it is started
|
||||
pass
|
||||
|
||||
|
||||
class BigText(QWidget):
|
||||
|
||||
def __init__(self, model: LogFileModel):
|
||||
super(BigText, self).__init__()
|
||||
|
||||
self.model = model
|
||||
|
||||
self.watchdog = FileWatchdogThread(self, model.get_file())
|
||||
QThreadPool.globalInstance().start(self.watchdog)
|
||||
|
||||
self.grid = QGridLayout()
|
||||
self.grid.setContentsMargins(0, 0, 0, 0)
|
||||
self.grid.setHorizontalSpacing(0)
|
||||
@@ -51,7 +100,7 @@ class BigText(QWidget):
|
||||
return self.model.get_file()
|
||||
|
||||
def destruct(self):
|
||||
pass
|
||||
self.watchdog.destruct()
|
||||
|
||||
|
||||
class InnerBigText(QWidget):
|
||||
@@ -82,7 +131,7 @@ class InnerBigText(QWidget):
|
||||
|
||||
def keyPressEvent(self, e: QKeyEvent) -> None:
|
||||
|
||||
#print("%s + %s" % (e.keyCombination().keyboardModifiers(), e.key()))
|
||||
# print("%s + %s" % (e.keyCombination().keyboardModifiers(), e.key()))
|
||||
if e.modifiers() == Qt.KeyboardModifier.NoModifier:
|
||||
lines_to_scroll = math.floor(self.lines_shown()) - 1
|
||||
if e.key() == Qt.Key.Key_PageUp:
|
||||
@@ -186,6 +235,7 @@ class InnerBigText(QWidget):
|
||||
cb.setText(selected_text)
|
||||
|
||||
def paintEvent(self, event: QPaintEvent) -> None:
|
||||
#print("paintEvent")
|
||||
painter = QPainter(self)
|
||||
painter.setFont(self.model.settings.font())
|
||||
painter.setPen(QColor(0, 0, 0))
|
||||
|
||||
Reference in New Issue
Block a user