-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.py
More file actions
60 lines (47 loc) · 1.96 KB
/
Copy pathconsole.py
File metadata and controls
60 lines (47 loc) · 1.96 KB
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
"""console.py — Thread-safe console output buffer for QECTOR Workbench."""
from __future__ import annotations
import threading
from collections import deque
from typing import Callable
MAX_LINES = 5000
class Console:
"""Thread-safe, bounded in-memory console buffer with subscriber callbacks.
At most ``max_lines`` entries are retained; when the buffer is full the
oldest entries are dropped first. Subscribers registered with
:meth:`subscribe` are invoked outside the lock with each newly written
chunk of text; a failing subscriber never breaks the console or the other
subscribers. All methods are safe to call from any thread.
"""
def __init__(self, max_lines: int = MAX_LINES):
self._lock = threading.Lock()
self._lines: deque[str] = deque(maxlen=max(1, int(max_lines)))
self._callbacks: list[Callable[[str], None]] = []
def subscribe(self, callback: Callable[[str], None]) -> None:
"""Register a callback invoked with each newly written text chunk."""
with self._lock:
if callback not in self._callbacks:
self._callbacks.append(callback)
def unsubscribe(self, callback: Callable[[str], None]) -> None:
"""Remove a previously registered callback (no-op if absent)."""
with self._lock:
try:
self._callbacks.remove(callback)
except ValueError:
pass
def write(self, text: str) -> None:
with self._lock:
self._lines.append(text)
callbacks = list(self._callbacks)
for cb in callbacks:
try:
cb(text)
except Exception:
pass
def log(self, text: str, level: str = "INFO") -> None:
self.write(f"[{level}] {text}\n")
def get_text(self) -> str:
with self._lock:
return "".join(self._lines)
def clear(self) -> None:
with self._lock:
self._lines.clear()