Repository navigation
Expand file tree
/
Copy patheditor_errors.py
More file actions
89 lines (70 loc) · 3.56 KB
/
Copy patheditor_errors.py
File metadata and controls
89 lines (70 loc) · 3.56 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
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2026 KeithCu (modifications and relicensing)
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Failure detail and message formatting for user-visible scripting dialogs (Monaco editor, Run Python Script, Calc cell editor)."""
from __future__ import annotations
import re
import traceback
from typing import Any
from plugin.framework.deal_shim import deal
def _deal_text_ok(value: Any) -> bool:
return isinstance(value, str)
def _deal_optional_exc_ok(exc: Any) -> bool:
return exc is None or isinstance(exc, BaseException)
@deal.pre(lambda exc: isinstance(exc, BaseException))
def exception_traceback(exc: BaseException) -> str:
"""Full traceback for *exc*.
The pre is isinstance(exc, BaseException) on both profiles because a
CrossHair domain of exc is None has no __traceback__; the callable
stays # crosshair: off so traceback.format_exception is not a
check/cover entry.
"""
# crosshair: off
return "".join(traceback.format_exception(type(exc), exc, exc.__traceback__))
@deal.pre(lambda detail=None, exc=None: (detail is None or _deal_text_ok(detail)) and _deal_optional_exc_ok(exc))
def failure_detail(*, detail: str | None = None, exc: BaseException | None = None) -> str:
"""Combine subprocess stderr, probe output, and/or an exception traceback."""
# crosshair: off
chunks: list[str] = []
detail_text = (detail or "").strip()
if detail_text:
chunks.append(detail_text)
if exc is not None:
chunks.append(exception_traceback(exc).rstrip())
return "\n\n".join(chunks)
@deal.pre(lambda summary, detail=None, exc=None: _deal_text_ok(summary) and (detail is None or _deal_text_ok(detail)) and _deal_optional_exc_ok(exc))
def failure_message(summary: str, *, detail: str | None = None, exc: BaseException | None = None) -> str:
"""Build a msgbox body: *summary* plus optional detail/traceback blocks."""
# crosshair: off
body = failure_detail(detail=detail, exc=exc)
if body:
return f"{summary}\n\n{body}"
return summary
def editor_error_markers(message: str, traceback_text: str = "", code: str = "") -> list[dict[str, Any]]:
"""Extract editor error markers from failure messages and tracebacks."""
combined = f"{message}\n{traceback_text}" if traceback_text else message
line: int | None = None
m_syntax = re.search(r"Code parsing failed on line (\d+)", combined)
if m_syntax:
line = int(m_syntax.group(1))
else:
# The last <sandbox> entry is the innermost frame of this code; init-script,
# named-library and earlier-cell frames carry their own file names.
sandbox_matches = list(re.finditer(r'File "<sandbox>", line (\d+)', combined))
if sandbox_matches:
line = int(sandbox_matches[-1].group(1))
else:
m_exec = re.search(r"Code execution failed at line '(.*)' due to:", combined, re.DOTALL)
if m_exec:
segment = m_exec.group(1)
if segment and segment != "None" and code:
idx = code.find(segment)
if idx != -1:
line = code[:idx].count("\n") + 1
if line is None or line < 1 or (code and line > code.count("\n") + 1):
return []
target_text = message if message.strip() else traceback_text
first_line = next((raw.strip() for raw in target_text.splitlines() if raw.strip()), "")
first_line = re.sub(r"\s*\(took\s+[^)]*\)$", "", first_line).strip()
return [{"line": line, "message": first_line, "severity": "error"}]