forked from balisujohn/localwriter
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdiagnostics.py
More file actions
204 lines (175 loc) · 6.08 KB
/
Copy pathdiagnostics.py
File metadata and controls
204 lines (175 loc) · 6.08 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
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
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2026 KeithCu
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Bounded per-workbook log of ``=PY()`` stdout / errors for the LibrePy sidebar.
Recording from formula evaluation must stay UNO-light: only plain Python data.
Cell addresses are matched later when the sidebar refreshes.
"""
from __future__ import annotations
import threading
import time
from collections import deque
from dataclasses import dataclass, field
from typing import Any, Literal
DiagnosticFilter = Literal["all", "errors", "output"]
_MAX_ENTRIES_PER_WORKBOOK = 200
_CODE_SNIPPET_MAX = 240
@dataclass(frozen=True)
class DiagnosticEntry:
"""One recorded ``=PY()`` evaluation outcome."""
workbook_key: str
code: str
status: str # "ok" | "error"
message: str = ""
stdout: str = ""
traceback: str = ""
timestamp: float = field(default_factory=time.time)
sheet: str = ""
address: str = ""
@property
def has_output(self) -> bool:
return bool((self.stdout or "").strip())
@property
def is_error(self) -> bool:
return self.status != "ok"
def matches_filter(self, filt: DiagnosticFilter) -> bool:
if filt == "errors":
return self.is_error
if filt == "output":
return self.has_output or self.is_error
return True
def summary_line(self) -> str:
"""One-line label for list controls."""
where = self.address or "(unknown cell)"
if self.is_error:
msg = (self.message or "error").strip().splitlines()[0][:80]
return f"{where}: ERROR — {msg}"
if self.has_output:
out = self.stdout.strip().splitlines()[0][:80]
return f"{where}: {out}"
return f"{where}: ok"
class PythonDiagnosticsStore:
"""Thread-safe ring buffer of diagnostics keyed by workbook."""
def __init__(self, *, max_entries: int = _MAX_ENTRIES_PER_WORKBOOK) -> None:
self._max = max(1, int(max_entries))
self._lock = threading.Lock()
self._by_workbook: dict[str, deque[DiagnosticEntry]] = {}
self._listeners: list[Any] = []
def record(
self,
*,
workbook_key: str,
code: str,
status: str,
message: str = "",
stdout: str = "",
traceback: str = "",
sheet: str = "",
address: str = "",
) -> DiagnosticEntry:
key = (workbook_key or "unknown").strip() or "unknown"
snippet = (code or "")[:_CODE_SNIPPET_MAX]
entry = DiagnosticEntry(
workbook_key=key,
code=snippet,
status="ok" if status == "ok" else "error",
message=str(message or ""),
stdout=str(stdout or ""),
traceback=str(traceback or ""),
sheet=str(sheet or ""),
address=str(address or ""),
)
with self._lock:
bucket = self._by_workbook.get(key)
if bucket is None:
bucket = deque(maxlen=self._max)
self._by_workbook[key] = bucket
bucket.append(entry)
listeners = list(self._listeners)
for cb in listeners:
try:
cb(entry)
except Exception:
pass
return entry
def list_entries(
self,
workbook_key: str,
*,
filt: DiagnosticFilter = "all",
newest_first: bool = True,
) -> list[DiagnosticEntry]:
key = (workbook_key or "unknown").strip() or "unknown"
with self._lock:
bucket = list(self._by_workbook.get(key, ()))
if newest_first:
bucket.reverse()
return [e for e in bucket if e.matches_filter(filt)]
def latest_for_code(self, workbook_key: str, code: str) -> DiagnosticEntry | None:
"""Return the newest entry whose code prefix matches *code*."""
needle = (code or "")[:_CODE_SNIPPET_MAX]
if not needle:
return None
for entry in self.list_entries(workbook_key, filt="all", newest_first=True):
if entry.code == needle or entry.code.startswith(needle) or needle.startswith(entry.code):
return entry
return None
def clear(self, workbook_key: str | None = None) -> None:
with self._lock:
if workbook_key is None:
self._by_workbook.clear()
else:
self._by_workbook.pop((workbook_key or "").strip() or "unknown", None)
def add_listener(self, callback: Any) -> None:
with self._lock:
if callback not in self._listeners:
self._listeners.append(callback)
def remove_listener(self, callback: Any) -> None:
with self._lock:
try:
self._listeners.remove(callback)
except ValueError:
pass
_STORE = PythonDiagnosticsStore()
def get_diagnostics_store() -> PythonDiagnosticsStore:
return _STORE
def record_python_eval(
*,
workbook_key: str,
code: str,
status: str,
message: str = "",
stdout: str = "",
traceback: str = "",
sheet: str = "",
address: str = "",
) -> DiagnosticEntry:
"""Record one ``=PY()`` outcome (safe to call from formula evaluation)."""
return _STORE.record(
workbook_key=workbook_key,
code=code,
status=status,
message=message,
stdout=stdout,
traceback=traceback,
sheet=sheet,
address=address,
)
def diagnostics_detail_text(entry: DiagnosticEntry) -> str:
"""Multi-line detail for the diagnostics text area."""
lines = [
f"Cell: {entry.address or '(unknown)'}",
f"Status: {entry.status}",
f"Time: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(entry.timestamp))}",
"",
"Code:",
entry.code or "(empty)",
]
if entry.message:
lines.extend(["", "Message:", entry.message.strip()])
if entry.stdout:
lines.extend(["", "stdout:", entry.stdout.strip()])
if entry.traceback:
lines.extend(["", "Traceback:", entry.traceback.strip()])
return "\n".join(lines)