Repository navigation
Expand file tree
/
Copy pathinit_script_editor.py
More file actions
93 lines (84 loc) · 3.5 KB
/
Copy pathinit_script_editor.py
File metadata and controls
93 lines (84 loc) · 3.5 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
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2026 KeithCu
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Open Monaco to edit the Calc workbook initialization script (INIT)."""
from __future__ import annotations
from typing import Any
from plugin.chatbot.dialogs import msgbox, msgbox_with_report
from plugin.framework.config import get_config
from plugin.framework.errors import is_disposal
from plugin.framework.i18n import _
from plugin.framework.thread_guard import main_thread_only
from plugin.scripting.document_scripts import (
document_scripts_identity,
get_calc_document_from_ctx,
get_calc_init_script,
set_calc_init_script,
)
from plugin.scripting.editor_errors import failure_message
from plugin.scripting.editor_host import (
cached_webview_probe,
launch_monaco_editor,
monaco_editor_available,
resolve_editor_python,
)
@main_thread_only
def open_init_script_editor(ctx: Any) -> None:
"""Open Monaco for the active Calc workbook's initialization script."""
title = _("Edit Initialization Script")
doc = get_calc_document_from_ctx(ctx)
if doc is None:
msgbox(ctx, title, _("Open a Calc spreadsheet first."), box_type=1)
return
exe, monaco_available = monaco_editor_available()
if not monaco_available or exe is None:
if get_config("scripting.force_internal_script_editor"):
msgbox(ctx, title, _('The Python editor window is disabled by "scripting.force_internal_script_editor" in writeragent.json.'), box_type=3)
return
if not exe:
_unused, err = resolve_editor_python()
msgbox(ctx, title, err or _("Configure a Python venv path in Settings → Python."), box_type=3)
return
probe_row = cached_webview_probe(exe)
if probe_row is None:
msgbox(ctx, title, _("The Python editor check is still running. Open this again in a moment."), box_type=1)
return
if not probe_row[0]:
msg = failure_message(_("Cannot import webview (pywebview) in the configured venv."), detail=probe_row[1])
msgbox_with_report(ctx, title, msg, reportable=True)
return
closed_message = _("The spreadsheet is no longer open. Reopen it and try again.")
try:
initial = get_calc_init_script(doc)
except Exception as exc:
if not is_disposal(exc):
raise
msgbox(ctx, title, closed_message, box_type=3)
return
def on_save(code: str, _save_as_plain: bool, _data_binding: str | None, action: str) -> dict[str, Any]:
nonlocal initial
if code != initial:
try:
err = set_calc_init_script(doc, code)
except Exception as exc:
if not is_disposal(exc):
raise
return {"type": "error", "message": closed_message}
if err is not None:
return {"type": "error", "message": err}
initial = code
return {"type": "saved", "ok": True, "status_ok_text": _("Initialization script saved.")}
load_msg: dict[str, Any] = {
"type": "load",
"mode": "init_script",
"language": "python",
"code": initial,
"title": title,
"save_as_plain": True,
"plain_text_label": _("Save initialization script"),
"resource": "init",
"doc_url": document_scripts_identity(doc),
"show_data_binding": False,
}
launch_monaco_editor(ctx, exe=exe, load_message=load_msg, on_save=on_save)