File size: 8,011 Bytes
bbc65cd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | #!/usr/bin/env python3
"""Project Dashboard Tab — WebKit2 WebView embedding the Project Dashboard.
Provides build_project_dashboard_tab() returning a Gtk.Box with:
- Toolbar: Reload, Open in Browser, Rescan, server status
- WebKit2 WebView loading http://localhost:5222
- Auto-starts uvicorn server if not running
"""
import subprocess
import os
from pathlib import Path
LOG_DIR = Path.home() / ".cache" / "project-dashboard"
LOG_FILE = LOG_DIR / "server.log"
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("WebKit2", "4.1")
from gi.repository import Gtk, GLib, WebKit2
from theme import get_palette
DASHBOARD_PORT = 5222
DASHBOARD_URL = f"http://localhost:{DASHBOARD_PORT}"
DASHBOARD_DIR = Path.home() / "Projekte" / "ProjectDashboard"
VENV_PYTHON = DASHBOARD_DIR / ".venv" / "bin" / "python3"
# Module-level refs for refresh
_webview = None
_status_label = None
_spinner = None
_server_proc = None
_retry_count = 0
def _server_healthy(port: int) -> bool:
"""Check if the dashboard server responds."""
try:
import urllib.request
with urllib.request.urlopen(
f"http://127.0.0.1:{port}/api/stats", timeout=2
) as resp:
return resp.status == 200
except Exception:
return False
def _ensure_server():
"""Start the uvicorn dashboard server if not already running."""
global _server_proc
if _server_healthy(DASHBOARD_PORT):
return True
if not DASHBOARD_DIR.exists():
return False
python = str(VENV_PYTHON) if VENV_PYTHON.exists() else "python3"
main_py = DASHBOARD_DIR / "main.py"
if not main_py.exists():
return False
try:
LOG_DIR.mkdir(parents=True, exist_ok=True)
log_fh = open(LOG_FILE, "a", encoding="utf-8")
_server_proc = subprocess.Popen(
[python, "-m", "uvicorn", "main:app",
"--host", "127.0.0.1", "--port", str(DASHBOARD_PORT)],
cwd=str(DASHBOARD_DIR),
stdout=log_fh,
stderr=log_fh,
preexec_fn=os.setpgrp,
)
return True
except Exception:
return False
def _update_status(connected: bool):
"""Update the status label and spinner."""
if _status_label is None:
return
ctx = _status_label.get_style_context()
ctx.remove_class("swarm-status-connected")
ctx.remove_class("swarm-status-disconnected")
if connected:
ctx.add_class("swarm-status-connected")
_status_label.set_text("● Verbunden")
if _spinner is not None:
_spinner.stop()
else:
ctx.add_class("swarm-status-disconnected")
_status_label.set_text("● Getrennt")
if _spinner is not None:
_spinner.start()
def build_project_dashboard_tab() -> Gtk.Box:
"""Build and return the Project Dashboard tab widget."""
global _webview, _status_label, _spinner
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
# --- Toolbar ---
toolbar = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=8)
toolbar.set_margin_start(8)
toolbar.set_margin_end(8)
toolbar.set_margin_top(6)
toolbar.set_margin_bottom(4)
toolbar.get_style_context().add_class("swarm-toolbar")
reload_btn = Gtk.Button(label="⟳ Reload")
reload_btn.set_tooltip_text("Dashboard neu laden")
reload_btn.connect("clicked", _on_reload)
reload_btn.get_style_context().add_class("swarm-btn")
toolbar.pack_start(reload_btn, False, False, 0)
browser_btn = Gtk.Button(label="↗ Browser")
browser_btn.set_tooltip_text("Im Browser oeffnen")
browser_btn.connect("clicked", _on_open_browser)
browser_btn.get_style_context().add_class("swarm-btn")
toolbar.pack_start(browser_btn, False, False, 0)
rescan_btn = Gtk.Button(label="⟳ Rescan")
rescan_btn.set_tooltip_text("Alle Projekte neu scannen")
rescan_btn.connect("clicked", _on_rescan)
rescan_btn.get_style_context().add_class("swarm-btn")
toolbar.pack_start(rescan_btn, False, False, 0)
_spinner = Gtk.Spinner()
toolbar.pack_end(_spinner, False, False, 0)
_status_label = Gtk.Label()
_status_label.set_xalign(1.0)
toolbar.pack_end(_status_label, False, False, 4)
url_label = Gtk.Label(label=DASHBOARD_URL)
url_label.get_style_context().add_class("swarm-url")
url_label.set_xalign(1.0)
toolbar.pack_end(url_label, False, False, 8)
box.pack_start(toolbar, False, False, 0)
# --- Separator ---
box.pack_start(Gtk.Separator(), False, False, 0)
# --- WebView ---
_webview = WebKit2.WebView()
settings = _webview.get_settings()
settings.set_enable_javascript(True)
settings.set_enable_developer_extras(False)
settings.set_hardware_acceleration_policy(
WebKit2.HardwareAccelerationPolicy.NEVER
)
# Start server and load
_ensure_server()
if _server_healthy(DASHBOARD_PORT):
_webview.load_uri(DASHBOARD_URL)
_update_status(True)
else:
_update_status(False)
p = get_palette()
_webview.load_html(
f'<html><body style="background:{p["bg"]};color:{p["text"]};font-family:system-ui;'
'display:flex;align-items:center;justify-content:center;height:100vh;'
'flex-direction:column;gap:12px">'
'<div style="font-size:24px;opacity:0.4">⟳</div>'
'<div>Project Dashboard startet...</div>'
f'<div style="font-size:12px;color:{p["overlay"]}">Automatischer Retry alle 2s</div>'
'</body></html>',
None
)
GLib.timeout_add_seconds(2, _retry_load)
box.pack_start(_webview, True, True, 0)
return box
def _retry_load() -> bool:
"""Retry loading the dashboard URL."""
global _retry_count
_retry_count += 1
if _server_healthy(DASHBOARD_PORT):
_retry_count = 0
if _webview:
_webview.load_uri(DASHBOARD_URL)
_update_status(True)
return False
elif _retry_count >= 15:
_retry_count = 0
if _webview:
log_hint = str(LOG_FILE)
p = get_palette()
_webview.load_html(
f'<html><body style="background:{p["bg"]};color:{p["red"]};font-family:system-ui;'
'display:flex;align-items:center;justify-content:center;height:100vh;'
'flex-direction:column;gap:12px">'
'<div style="font-size:24px">Server-Fehler</div>'
f'<div style="font-size:12px;color:{p["overlay"]}">Log: {log_hint}</div>'
f'<button onclick="location.reload()" style="background:{p["card"]};color:{p["text"]};'
f'border:1px solid {p["border"]};border-radius:6px;padding:8px 16px;cursor:pointer;'
'font-size:13px;margin-top:8px">Retry</button>'
'</body></html>',
None
)
_update_status(False)
return False
else:
_ensure_server()
return True
def _on_reload(_btn):
"""Reload the WebView."""
if _webview:
_webview.reload()
def _on_open_browser(_btn):
"""Open dashboard in default browser."""
try:
subprocess.Popen(
["xdg-open", DASHBOARD_URL],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except Exception:
pass
def _on_rescan(_btn):
"""Trigger a rescan via the API and reload."""
try:
import urllib.request
req = urllib.request.Request(
f"http://127.0.0.1:{DASHBOARD_PORT}/rescan", method="POST"
)
urllib.request.urlopen(req, timeout=10)
except Exception:
pass
if _webview:
_webview.reload()
def refresh_project_dashboard() -> bool:
"""Check server health — called by GLib timer from panel.py."""
connected = _server_healthy(DASHBOARD_PORT)
_update_status(connected)
if not connected:
_ensure_server()
return True # keep timer alive
|