forked from balisujohn/localwriter
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathquant.py
More file actions
146 lines (115 loc) · 4.54 KB
/
Copy pathquant.py
File metadata and controls
146 lines (115 loc) · 4.54 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
# WriterAgent - AI Writing Assistant for LibreOffice
# Copyright (c) 2026 KeithCu (modifications and relicensing)
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Quant helper templates and host RPC (LO host).
Compute is lazy-loaded from ``plugin.scripting.venv.quant`` via ``__getattr__``.
"""
from __future__ import annotations
import logging
from typing import Any
from plugin.calc.analysis_runner import calc_tool_context
from plugin.scripting._lazy_venv import make_getattr
from plugin.calc.calc_addin_data import _resolve_python_data
from plugin.doc.doc_type import is_calc, is_writer
from plugin.scripting.client import run_quant as client_run_quant
from plugin.scripting.helper_domain import (
header_prefix,
)
from plugin.framework.errors import ToolExecutionError
log = logging.getLogger(__name__)
from plugin.scripting.calc_functions_common import QUANT_HELPER_NAMES as HELPER_NAMES
QUANT_HEADER_PREFIX = header_prefix("quant")
_DEFAULT_PARAMS: dict[str, dict[str, Any]] = {
"fetch_historical_data": {"tickers": "data", "start_date": "2023-01-01", "end_date": "2024-01-01", "interval": "1d"},
"technical_analysis": {"indicators": ["macd", "rsi", "bbands"]},
"portfolio_tearsheet": {},
"efficient_frontier": {},
}
_HELPER_DESCRIPTIONS: dict[str, str] = {
"fetch_historical_data": "Fetch historical prices via yfinance",
"technical_analysis": "Calculate MACD, RSI, and Bollinger Bands",
"portfolio_tearsheet": "Generate portfolio performance metrics via quantstats",
"efficient_frontier": "Optimize portfolio weights via PyPortfolioOpt",
}
_QUANT_VENV_EXPORTS = frozenset(
{
"efficient_frontier",
"fetch_historical_data",
"portfolio_tearsheet",
"run_quant",
"technical_analysis",
}
)
__getattr__ = make_getattr("quant", _QUANT_VENV_EXPORTS)
# --- Templates ---
from plugin.scripting.helper_domain import DomainFacadeConfig, make_template_api
_API = make_template_api(
DomainFacadeConfig(
tag="quant",
helper_names=HELPER_NAMES,
default_params=_DEFAULT_PARAMS,
descriptions=_HELPER_DESCRIPTIONS,
import_module="writeragent.scripting.quant",
run_name="run_quant",
style="run_import",
data_expr="data",
)
)
parse_quant_script_header = _API.parse_header
get_quant_script_templates = _API.get_templates
def get_quant_template(helper: str) -> str | None:
if helper not in HELPER_NAMES:
return None
return _API.template_body(helper, dict(_DEFAULT_PARAMS.get(helper, {})))
# --- Runner ---
def supports_quant_manual(doc: Any) -> bool:
"""True when Run Python Script should expose Quant Helpers for *doc*."""
if doc is None:
return False
try:
return is_writer(doc) or is_calc(doc)
except Exception:
return False
def run_trusted_quant(
uno_ctx: Any,
doc: Any,
*,
helper: str,
params: dict[str, Any] | None = None,
data_range: str | None = None,
data: Any = None,
headers: bool = True,
task_hint: str | None = None,
) -> dict[str, Any]:
"""Fetch Calc data and run a trusted quant helper in the user venv."""
name = str(helper or "").strip()
if not name:
raise ToolExecutionError("helper is required", code="QUANT_ERROR")
if name not in HELPER_NAMES:
raise ToolExecutionError(f"Unknown helper {name!r}", code="QUANT_ERROR")
if not is_calc(doc) and not is_writer(doc):
raise ToolExecutionError("Quant helpers require a Writer or Calc document.", code="QUANT_ERROR")
dr = str(data_range).strip() if data_range else None
py_data = None
if dr or data is not None:
tool_ctx = calc_tool_context(uno_ctx, doc)
py_data, err = _resolve_python_data(tool_ctx, data_range=dr, data=data)
if err:
raise ToolExecutionError(err, code="QUANT_ERROR")
# Some helpers like fetch_historical_data do not need py_data
if name != "fetch_historical_data" and py_data is None:
raise ToolExecutionError("Provide data_range or data for this quant helper", code="QUANT_ERROR")
spec_params = params or {}
context: dict[str, Any] = {}
if is_calc(doc):
try:
from plugin.calc.bridge import CalcBridge
context["sheet_name"] = CalcBridge(doc).get_active_sheet().getName()
except Exception:
pass
if task_hint:
context["task_hint"] = str(task_hint)
if dr:
context["range_a1"] = dr
return client_run_quant(uno_ctx, {"helper": name, "params": spec_params}, py_data, context=context or None)