Recursive watch folders for reloading, --debug

Watching folders will only be done if --debug is provided, to avoid
filesystem scan slowdowns and over watch limit errors.
This commit is contained in:
miruka 2019-03-27 16:21:31 -04:00
parent 9d838ff57a
commit 4872c3bf39
2 changed files with 26 additions and 7 deletions

View File

@ -11,6 +11,12 @@ from .engine import Engine
def run() -> None: def run() -> None:
try:
sys.argv.index("--debug")
debug = True
except ValueError:
debug = False
app = QGuiApplication(sys.argv) app = QGuiApplication(sys.argv)
engine = Engine(app) engine = Engine(app, debug=debug)
engine.show_window() engine.show_window()

View File

@ -4,7 +4,7 @@
import logging import logging
import sys import sys
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Generator, Optional
from PyQt5.QtCore import QFileSystemWatcher, QObject, QTimer from PyQt5.QtCore import QFileSystemWatcher, QObject, QTimer
from PyQt5.QtGui import QGuiApplication from PyQt5.QtGui import QGuiApplication
@ -17,8 +17,10 @@ from .backend import DummyBackend
class Engine(QQmlApplicationEngine): class Engine(QQmlApplicationEngine):
def __init__(self, app: QGuiApplication, parent: Optional[QObject] = None def __init__(self,
) -> None: app: QGuiApplication,
debug: bool = False,
parent: Optional[QObject] = None) -> None:
super().__init__(parent) super().__init__(parent)
self.app = app self.app = app
self.backend = DummyBackend() self.backend = DummyBackend()
@ -37,9 +39,13 @@ class Engine(QQmlApplicationEngine):
self._sigint_timer.start(100) self._sigint_timer.start(100)
# Setup UI live-reloading when a file is edited # Setup UI live-reloading when a file is edited
self.file_watcher = QFileSystemWatcher() if debug:
self.file_watcher.directoryChanged.connect(lambda _: self.reload_qml()) self._watcher = QFileSystemWatcher()
self.file_watcher.addPath(str(self.app_dir / "components")) self._watcher.directoryChanged.connect(lambda _: self.reload_qml())
self._watcher.addPath(str(self.app_dir))
for _dir in list(self._recursive_dirs_in(self.app_dir)):
self._watcher.addPath(str(_dir))
# Load QML page and show window # Load QML page and show window
self.load(str(self.app_dir / "components" / "Window.qml")) self.load(str(self.app_dir / "components" / "Window.qml"))
@ -50,6 +56,13 @@ class Engine(QQmlApplicationEngine):
sys.exit(self.app.exec()) sys.exit(self.app.exec())
def _recursive_dirs_in(self, path: Path) -> Generator[Path, None, None]:
for item in path.iterdir():
if item.is_dir() and item.name != "__pycache__":
yield item
yield from self._recursive_dirs_in(item)
def reload_qml(self) -> None: def reload_qml(self) -> None:
self.clearComponentCache() self.clearComponentCache()
loader = self.rootObjects()[0].findChild(QObject, "UILoader") loader = self.rootObjects()[0].findChild(QObject, "UILoader")