]> danix's work - publisher.git/commitdiff
feat: HugoWorker QProcess with log streaming
authorDanilo M. <redacted>
Sun, 3 May 2026 08:10:30 +0000 (10:10 +0200)
committerDanilo M. <redacted>
Sun, 3 May 2026 08:10:30 +0000 (10:10 +0200)
workers/hugo_worker.py [new file with mode: 0644]

diff --git a/workers/hugo_worker.py b/workers/hugo_worker.py
new file mode 100644 (file)
index 0000000..23748a7
--- /dev/null
@@ -0,0 +1,49 @@
+from __future__ import annotations
+from PyQt6.QtCore import QObject, QProcess, pyqtSignal
+from pathlib import Path
+
+class HugoWorker(QObject):
+    log_line = pyqtSignal(str)
+    started = pyqtSignal()
+    stopped = pyqtSignal()
+    error = pyqtSignal(str)
+
+    def __init__(self, repo_path: Path, parent=None):
+        super().__init__(parent)
+        self._repo_path = repo_path
+        self._process = QProcess(self)
+        self._process.readyReadStandardOutput.connect(self._on_stdout)
+        self._process.readyReadStandardError.connect(self._on_stderr)
+        self._process.finished.connect(self._on_finished)
+
+    @property
+    def is_running(self) -> bool:
+        return self._process.state() == QProcess.ProcessState.Running
+
+    def start(self):
+        if self.is_running:
+            return
+        self._process.setWorkingDirectory(str(self._repo_path))
+        self._process.start("hugo", ["server", "-D"])
+        self.started.emit()
+
+    def stop(self):
+        if self.is_running:
+            self._process.terminate()
+            self._process.waitForFinished(3000)
+            self.stopped.emit()
+
+    def _on_stdout(self):
+        data = self._process.readAllStandardOutput().data().decode("utf-8", errors="replace")
+        for line in data.splitlines():
+            self.log_line.emit(line)
+
+    def _on_stderr(self):
+        data = self._process.readAllStandardError().data().decode("utf-8", errors="replace")
+        for line in data.splitlines():
+            self.log_line.emit(line)
+
+    def _on_finished(self, exit_code: int, _):
+        if exit_code != 0:
+            self.error.emit(f"hugo server exited with code {exit_code}")
+        self.stopped.emit()