avrae-ls 0.4.1__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {avrae_ls-0.4.1.dist-info → avrae_ls-0.5.0.dist-info}/METADATA +31 -11
- avrae_ls-0.5.0.dist-info/RECORD +6 -0
- {avrae_ls-0.4.1.dist-info → avrae_ls-0.5.0.dist-info}/WHEEL +1 -2
- avrae_ls/__init__.py +0 -3
- avrae_ls/__main__.py +0 -108
- avrae_ls/alias_preview.py +0 -346
- avrae_ls/api.py +0 -2014
- avrae_ls/argparser.py +0 -430
- avrae_ls/argument_parsing.py +0 -67
- avrae_ls/code_actions.py +0 -282
- avrae_ls/codes.py +0 -3
- avrae_ls/completions.py +0 -1391
- avrae_ls/config.py +0 -496
- avrae_ls/context.py +0 -229
- avrae_ls/cvars.py +0 -115
- avrae_ls/diagnostics.py +0 -751
- avrae_ls/dice.py +0 -33
- avrae_ls/parser.py +0 -44
- avrae_ls/runtime.py +0 -661
- avrae_ls/server.py +0 -399
- avrae_ls/signature_help.py +0 -252
- avrae_ls/symbols.py +0 -266
- avrae_ls-0.4.1.dist-info/RECORD +0 -34
- avrae_ls-0.4.1.dist-info/top_level.txt +0 -2
- draconic/__init__.py +0 -4
- draconic/exceptions.py +0 -157
- draconic/helpers.py +0 -236
- draconic/interpreter.py +0 -1091
- draconic/string.py +0 -100
- draconic/types.py +0 -364
- draconic/utils.py +0 -78
- draconic/versions.py +0 -4
- {avrae_ls-0.4.1.dist-info → avrae_ls-0.5.0.dist-info}/entry_points.txt +0 -0
- {avrae_ls-0.4.1.dist-info → avrae_ls-0.5.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,21 +1,41 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: avrae-ls
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Language server for Avrae draconic aliases
|
|
5
5
|
Author: 1drturtle
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
8
27
|
License-File: LICENSE
|
|
9
|
-
Requires-
|
|
10
|
-
Requires-Dist: lsprotocol>=2023.0.1
|
|
11
|
-
Requires-Dist: httpx>=0.27
|
|
28
|
+
Requires-Python: >=3.11
|
|
12
29
|
Requires-Dist: d20>=1.1.2
|
|
30
|
+
Requires-Dist: httpx>=0.27
|
|
31
|
+
Requires-Dist: lsprotocol>=2023.0.1
|
|
32
|
+
Requires-Dist: pygls>=1.3.1
|
|
13
33
|
Provides-Extra: dev
|
|
14
|
-
Requires-Dist: pytest>=
|
|
15
|
-
Requires-Dist: pytest-
|
|
16
|
-
Requires-Dist: pytest
|
|
17
|
-
Requires-Dist: ruff>=0.6; extra ==
|
|
18
|
-
|
|
34
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
35
|
+
Requires-Dist: pytest-cov>=7.0.0; extra == 'dev'
|
|
36
|
+
Requires-Dist: pytest>=8.3; extra == 'dev'
|
|
37
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
38
|
+
Description-Content-Type: text/markdown
|
|
19
39
|
|
|
20
40
|
# Avrae Draconic Alias Language Server
|
|
21
41
|
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
draconic/LICENSE,sha256=Fzvu32_DafLKKn2mzxhEdlmrKZzAsigDZ87O7uoVqZI,1067
|
|
2
|
+
avrae_ls-0.5.0.dist-info/METADATA,sha256=Cl5rYWKuTLOoom3cjo4BV9ah4dMurBJ39nwJMsKBO4s,5916
|
|
3
|
+
avrae_ls-0.5.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
4
|
+
avrae_ls-0.5.0.dist-info/entry_points.txt,sha256=OtYXipMQzqmxpMoApgo0MeJYFmMbkbFN51Ibhpb8hF4,52
|
|
5
|
+
avrae_ls-0.5.0.dist-info/licenses/LICENSE,sha256=O-0zMbcEi6wXz1DiSdVgzMlQjJcNqNe5KDv08uYzqR0,1055
|
|
6
|
+
avrae_ls-0.5.0.dist-info/RECORD,,
|
avrae_ls/__init__.py
DELETED
avrae_ls/__main__.py
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import argparse
|
|
4
|
-
import asyncio
|
|
5
|
-
import logging
|
|
6
|
-
import sys
|
|
7
|
-
from pathlib import Path
|
|
8
|
-
from typing import Iterable
|
|
9
|
-
|
|
10
|
-
from lsprotocol import types
|
|
11
|
-
|
|
12
|
-
from .config import CONFIG_FILENAME, load_config
|
|
13
|
-
from .context import ContextBuilder
|
|
14
|
-
from .diagnostics import DiagnosticProvider
|
|
15
|
-
from .runtime import MockExecutor
|
|
16
|
-
from .server import create_server
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def main(argv: list[str] | None = None) -> None:
|
|
20
|
-
parser = argparse.ArgumentParser(description="Avrae draconic alias language server")
|
|
21
|
-
parser.add_argument("--tcp", action="store_true", help="Run in TCP mode instead of stdio")
|
|
22
|
-
parser.add_argument("--host", default="127.0.0.1", help="TCP host (when --tcp is set)")
|
|
23
|
-
parser.add_argument("--port", type=int, default=2087, help="TCP port (when --tcp is set)")
|
|
24
|
-
parser.add_argument("--stdio", action="store_true", help="Accept stdio flag for VS Code clients (ignored)")
|
|
25
|
-
parser.add_argument("--log-level", default="WARNING", help="Logging level (DEBUG, INFO, WARNING, ERROR)")
|
|
26
|
-
parser.add_argument("--analyze", metavar="FILE", help="Run diagnostics for a file and print them to stdout")
|
|
27
|
-
args = parser.parse_args(argv)
|
|
28
|
-
|
|
29
|
-
_configure_logging(args.log_level)
|
|
30
|
-
|
|
31
|
-
if args.analyze:
|
|
32
|
-
if args.tcp:
|
|
33
|
-
parser.error("--analyze cannot be combined with --tcp")
|
|
34
|
-
sys.exit(_run_analysis(Path(args.analyze)))
|
|
35
|
-
|
|
36
|
-
server = create_server()
|
|
37
|
-
if args.tcp:
|
|
38
|
-
server.start_tcp(args.host, args.port)
|
|
39
|
-
else:
|
|
40
|
-
server.start_io()
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
def _configure_logging(level: str) -> None:
|
|
44
|
-
numeric = getattr(logging, level.upper(), logging.WARNING)
|
|
45
|
-
if not isinstance(numeric, int):
|
|
46
|
-
numeric = logging.WARNING
|
|
47
|
-
logging.basicConfig(
|
|
48
|
-
level=numeric,
|
|
49
|
-
format="%(levelname)s %(name)s: %(message)s",
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def _run_analysis(path: Path) -> int:
|
|
54
|
-
if not path.exists():
|
|
55
|
-
print(f"File not found: {path}", file=sys.stderr)
|
|
56
|
-
return 2
|
|
57
|
-
|
|
58
|
-
workspace_root = _discover_workspace_root(path)
|
|
59
|
-
log = logging.getLogger(__name__)
|
|
60
|
-
log.info("Analyzing %s (workspace root: %s)", path, workspace_root)
|
|
61
|
-
|
|
62
|
-
config, warnings = load_config(workspace_root)
|
|
63
|
-
for warning in warnings:
|
|
64
|
-
log.warning(warning)
|
|
65
|
-
|
|
66
|
-
builder = ContextBuilder(config)
|
|
67
|
-
ctx_data = builder.build()
|
|
68
|
-
executor = MockExecutor(config.service)
|
|
69
|
-
diagnostics = DiagnosticProvider(executor, config.diagnostics)
|
|
70
|
-
|
|
71
|
-
source = path.read_text()
|
|
72
|
-
results = asyncio.run(diagnostics.analyze(source, ctx_data, builder.gvar_resolver))
|
|
73
|
-
_print_diagnostics(path, results)
|
|
74
|
-
return 1 if results else 0
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
def _discover_workspace_root(target: Path) -> Path:
|
|
78
|
-
current = target if target.is_dir() else target.parent
|
|
79
|
-
for folder in [current, *current.parents]:
|
|
80
|
-
if (folder / CONFIG_FILENAME).exists():
|
|
81
|
-
return folder
|
|
82
|
-
return current
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
def _print_diagnostics(path: Path, diagnostics: Iterable[types.Diagnostic]) -> None:
|
|
86
|
-
diags = list(diagnostics)
|
|
87
|
-
if not diags:
|
|
88
|
-
print(f"{path}: no issues found")
|
|
89
|
-
return
|
|
90
|
-
|
|
91
|
-
for diag in diags:
|
|
92
|
-
start = diag.range.start
|
|
93
|
-
severity = _severity_label(diag.severity)
|
|
94
|
-
source = diag.source or "avrae-ls"
|
|
95
|
-
print(f"{path}:{start.line + 1}:{start.character + 1}: {severity} [{source}] {diag.message}")
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def _severity_label(severity: types.DiagnosticSeverity | None) -> str:
|
|
99
|
-
if severity is None:
|
|
100
|
-
return "info"
|
|
101
|
-
try:
|
|
102
|
-
return types.DiagnosticSeverity(severity).name.lower()
|
|
103
|
-
except Exception:
|
|
104
|
-
return str(severity).lower()
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if __name__ == "__main__":
|
|
108
|
-
main()
|
avrae_ls/alias_preview.py
DELETED
|
@@ -1,346 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import re
|
|
4
|
-
import shlex
|
|
5
|
-
from dataclasses import asdict, dataclass, field
|
|
6
|
-
from typing import Any, Optional, Tuple
|
|
7
|
-
|
|
8
|
-
from .parser import DRACONIC_RE
|
|
9
|
-
from .runtime import ExecutionResult, MockExecutor
|
|
10
|
-
from .context import ContextData, GVarResolver
|
|
11
|
-
from .argument_parsing import apply_argument_parsing
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
@dataclass
|
|
15
|
-
class RenderedAlias:
|
|
16
|
-
command: str
|
|
17
|
-
stdout: str
|
|
18
|
-
error: Optional[BaseException]
|
|
19
|
-
last_value: Any | None = None
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
@dataclass
|
|
23
|
-
class EmbedFieldPreview:
|
|
24
|
-
name: str
|
|
25
|
-
value: str
|
|
26
|
-
inline: bool = False
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
@dataclass
|
|
30
|
-
class EmbedPreview:
|
|
31
|
-
title: str | None = None
|
|
32
|
-
description: str | None = None
|
|
33
|
-
footer: str | None = None
|
|
34
|
-
thumbnail: str | None = None
|
|
35
|
-
image: str | None = None
|
|
36
|
-
color: str | None = None
|
|
37
|
-
timeout: int | None = None
|
|
38
|
-
fields: list[EmbedFieldPreview] = field(default_factory=list)
|
|
39
|
-
|
|
40
|
-
def to_dict(self) -> dict[str, Any]:
|
|
41
|
-
return {
|
|
42
|
-
"title": self.title,
|
|
43
|
-
"description": self.description,
|
|
44
|
-
"footer": self.footer,
|
|
45
|
-
"thumbnail": self.thumbnail,
|
|
46
|
-
"image": self.image,
|
|
47
|
-
"color": self.color,
|
|
48
|
-
"timeout": self.timeout,
|
|
49
|
-
"fields": [asdict(f) for f in self.fields],
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
@dataclass
|
|
54
|
-
class SimulatedCommand:
|
|
55
|
-
preview: str | None
|
|
56
|
-
command_name: str | None
|
|
57
|
-
validation_error: str | None
|
|
58
|
-
embed: EmbedPreview | None = None
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
def _strip_alias_header(text: str) -> str:
|
|
62
|
-
lines = text.splitlines()
|
|
63
|
-
if lines and lines[0].lstrip().startswith("!alias"):
|
|
64
|
-
first = lines[0].lstrip()
|
|
65
|
-
parts = first.split(maxsplit=2)
|
|
66
|
-
remainder = parts[2] if len(parts) > 2 else ""
|
|
67
|
-
body = "\n".join(lines[1:])
|
|
68
|
-
if remainder:
|
|
69
|
-
return remainder + ("\n" + body if body else "")
|
|
70
|
-
return body
|
|
71
|
-
return text
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
async def render_alias_command(
|
|
75
|
-
text: str,
|
|
76
|
-
executor: MockExecutor,
|
|
77
|
-
ctx_data: ContextData,
|
|
78
|
-
resolver: GVarResolver,
|
|
79
|
-
args: list[str] | None = None,
|
|
80
|
-
) -> RenderedAlias:
|
|
81
|
-
"""Replace <drac2> blocks with their evaluated values and return final command."""
|
|
82
|
-
body = _strip_alias_header(text)
|
|
83
|
-
body = apply_argument_parsing(body, args)
|
|
84
|
-
stdout_parts: list[str] = []
|
|
85
|
-
parts: list[str] = []
|
|
86
|
-
last_value = None
|
|
87
|
-
error: BaseException | None = None
|
|
88
|
-
|
|
89
|
-
pos = 0
|
|
90
|
-
for match in DRACONIC_RE.finditer(body):
|
|
91
|
-
parts.append(body[pos: match.start()])
|
|
92
|
-
code = match.group(1)
|
|
93
|
-
result: ExecutionResult = await executor.run(code, ctx_data, resolver)
|
|
94
|
-
if result.stdout:
|
|
95
|
-
stdout_parts.append(result.stdout)
|
|
96
|
-
if result.error:
|
|
97
|
-
error = result.error
|
|
98
|
-
break
|
|
99
|
-
last_value = result.value
|
|
100
|
-
parts.append("" if result.value is None else str(result.value))
|
|
101
|
-
pos = match.end()
|
|
102
|
-
|
|
103
|
-
if error is None:
|
|
104
|
-
parts.append(body[pos:])
|
|
105
|
-
|
|
106
|
-
final_command = "".join(parts)
|
|
107
|
-
return RenderedAlias(command=final_command, stdout="".join(stdout_parts), error=error, last_value=last_value)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
def validate_embed_payload(payload: str) -> Tuple[bool, str | None]:
|
|
111
|
-
"""
|
|
112
|
-
Light validation for embed previews using Avrae-style flags.
|
|
113
|
-
|
|
114
|
-
Accepts strings such as "-title Foo -f \"T|Body\"" and validates arguments.
|
|
115
|
-
Returns (is_valid, error_message) without attempting to parse JSON objects.
|
|
116
|
-
"""
|
|
117
|
-
text = payload.strip()
|
|
118
|
-
if not text:
|
|
119
|
-
return False, "Embed payload is empty."
|
|
120
|
-
|
|
121
|
-
return _validate_embed_flags(text)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
def parse_embed_payload(payload: str) -> EmbedPreview:
|
|
125
|
-
"""Parse an embed payload into a structured preview object."""
|
|
126
|
-
tokens = shlex.split(payload.strip())
|
|
127
|
-
preview = EmbedPreview()
|
|
128
|
-
|
|
129
|
-
i = 0
|
|
130
|
-
while i < len(tokens):
|
|
131
|
-
tok = tokens[i]
|
|
132
|
-
if not tok.startswith("-"):
|
|
133
|
-
i += 1
|
|
134
|
-
continue
|
|
135
|
-
key = tok.lower()
|
|
136
|
-
next_val = tokens[i + 1] if i + 1 < len(tokens) else None
|
|
137
|
-
if key == "-title":
|
|
138
|
-
preview.title = next_val or ""
|
|
139
|
-
i += 2
|
|
140
|
-
continue
|
|
141
|
-
if key == "-desc":
|
|
142
|
-
preview.description = next_val or ""
|
|
143
|
-
i += 2
|
|
144
|
-
continue
|
|
145
|
-
if key == "-footer":
|
|
146
|
-
preview.footer = next_val or ""
|
|
147
|
-
i += 2
|
|
148
|
-
continue
|
|
149
|
-
if key == "-thumb":
|
|
150
|
-
preview.thumbnail = next_val or ""
|
|
151
|
-
i += 2
|
|
152
|
-
continue
|
|
153
|
-
if key == "-image":
|
|
154
|
-
preview.image = next_val or ""
|
|
155
|
-
i += 2
|
|
156
|
-
continue
|
|
157
|
-
if key == "-color":
|
|
158
|
-
preview.color = _normalize_color(next_val)
|
|
159
|
-
i += 2 if next_val is not None else 1
|
|
160
|
-
continue
|
|
161
|
-
if key == "-t":
|
|
162
|
-
preview.timeout = _parse_timeout(next_val)
|
|
163
|
-
i += 2
|
|
164
|
-
continue
|
|
165
|
-
if key == "-f":
|
|
166
|
-
field = _parse_field_value(next_val)
|
|
167
|
-
if field:
|
|
168
|
-
preview.fields.append(field)
|
|
169
|
-
i += 2
|
|
170
|
-
continue
|
|
171
|
-
i += 1
|
|
172
|
-
|
|
173
|
-
return preview
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
def _validate_embed_flags(text: str) -> Tuple[bool, str | None]:
|
|
177
|
-
"""Validate embed flags according to Avrae's help text."""
|
|
178
|
-
if not text:
|
|
179
|
-
return False, "Embed payload is empty."
|
|
180
|
-
|
|
181
|
-
try:
|
|
182
|
-
tokens = shlex.split(text)
|
|
183
|
-
except ValueError as exc: # pragma: no cover - defensive only
|
|
184
|
-
return False, f"Embed payload could not be parsed: {exc}"
|
|
185
|
-
|
|
186
|
-
flag_handlers = {
|
|
187
|
-
"-title": lambda val: _require_value("-title", val),
|
|
188
|
-
"-desc": lambda val: _require_value("-desc", val),
|
|
189
|
-
"-thumb": lambda val: _require_value("-thumb", val),
|
|
190
|
-
"-image": lambda val: _require_value("-image", val),
|
|
191
|
-
"-footer": lambda val: _require_value("-footer", val),
|
|
192
|
-
"-f": _validate_field_arg,
|
|
193
|
-
"-color": _validate_color_arg,
|
|
194
|
-
"-t": _validate_timeout_arg,
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
i = 0
|
|
198
|
-
while i < len(tokens):
|
|
199
|
-
tok = tokens[i]
|
|
200
|
-
key = tok.lower()
|
|
201
|
-
if not tok.startswith("-"):
|
|
202
|
-
i += 1
|
|
203
|
-
continue
|
|
204
|
-
if key not in flag_handlers:
|
|
205
|
-
return False, f"Embed payload contains unknown flag '{tok}'."
|
|
206
|
-
next_val = tokens[i + 1] if i + 1 < len(tokens) else None
|
|
207
|
-
ok, err, consumed = flag_handlers[key](next_val)
|
|
208
|
-
if not ok:
|
|
209
|
-
return False, err
|
|
210
|
-
i += consumed + 1
|
|
211
|
-
return True, None
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
def _require_value(flag: str, value: str | None) -> Tuple[bool, str | None, int]:
|
|
215
|
-
if value is None or value.startswith("-"):
|
|
216
|
-
return False, f"Embed flag '{flag}' requires a value.", 0
|
|
217
|
-
return True, None, 1
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
def _validate_field_arg(value: str | None) -> Tuple[bool, str | None, int]:
|
|
221
|
-
ok, err, consumed = _require_value("-f", value)
|
|
222
|
-
if not ok:
|
|
223
|
-
return ok, err, consumed
|
|
224
|
-
assert value is not None # for type checker
|
|
225
|
-
parts = value.split("|")
|
|
226
|
-
if len(parts) < 2 or len(parts) > 3:
|
|
227
|
-
return False, "Embed field must be in the form \"Title|Text[|inline]\".", consumed
|
|
228
|
-
if not parts[0] or not parts[1]:
|
|
229
|
-
return False, "Embed field title and text cannot be empty.", consumed
|
|
230
|
-
if len(parts) == 3 and parts[2].lower() not in ("inline", ""):
|
|
231
|
-
return False, "Embed field inline value must be 'inline' or omitted.", consumed
|
|
232
|
-
return True, None, consumed
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
def _validate_color_arg(value: str | None) -> Tuple[bool, str | None, int]:
|
|
236
|
-
if value is None or value.startswith("-"):
|
|
237
|
-
# Random color is allowed when omitted
|
|
238
|
-
return True, None, 0
|
|
239
|
-
if not re.match(r"^(?:#|0x)?[0-9a-fA-F]{6}$", value):
|
|
240
|
-
return False, "Embed color must be a 6-hex value (e.g. #ff00ff).", 1
|
|
241
|
-
return True, None, 1
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
def _validate_timeout_arg(value: str | None) -> Tuple[bool, str | None, int]:
|
|
245
|
-
ok, err, consumed = _require_value("-t", value)
|
|
246
|
-
if not ok:
|
|
247
|
-
return ok, err, consumed
|
|
248
|
-
assert value is not None # for type checker
|
|
249
|
-
try:
|
|
250
|
-
num = int(value)
|
|
251
|
-
except ValueError:
|
|
252
|
-
return False, "Embed timeout (-t) must be an integer.", consumed
|
|
253
|
-
if num < 0 or num > 600:
|
|
254
|
-
return False, "Embed timeout (-t) must be between 0 and 600 seconds.", consumed
|
|
255
|
-
return True, None, consumed
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
def _parse_timeout(value: str | None) -> int | None:
|
|
259
|
-
if value is None:
|
|
260
|
-
return None
|
|
261
|
-
try:
|
|
262
|
-
return int(value)
|
|
263
|
-
except (TypeError, ValueError):
|
|
264
|
-
return None
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
def _normalize_color(value: str | None) -> str | None:
|
|
268
|
-
if value is None:
|
|
269
|
-
return None
|
|
270
|
-
if not value:
|
|
271
|
-
return None
|
|
272
|
-
match = re.match(r"^(?:#|0x)?([0-9a-fA-F]{6})$", value)
|
|
273
|
-
if not match:
|
|
274
|
-
return value
|
|
275
|
-
return f"#{match.group(1)}"
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
def _parse_field_value(value: str | None) -> EmbedFieldPreview | None:
|
|
279
|
-
if value is None:
|
|
280
|
-
return None
|
|
281
|
-
parts = value.split("|")
|
|
282
|
-
if len(parts) < 2:
|
|
283
|
-
return None
|
|
284
|
-
inline_flag = parts[2].lower() == "inline" if len(parts) == 3 else False
|
|
285
|
-
return EmbedFieldPreview(name=parts[0], value=parts[1], inline=inline_flag)
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
def simulate_command(command: str) -> SimulatedCommand:
|
|
289
|
-
"""Very small shim to preview common commands."""
|
|
290
|
-
text = _strip_alias_header(command).strip()
|
|
291
|
-
if not text:
|
|
292
|
-
return SimulatedCommand(None, None, None, None)
|
|
293
|
-
head, payload = _extract_command_head_and_payload(text)
|
|
294
|
-
if not head:
|
|
295
|
-
return SimulatedCommand(None, None, None, None)
|
|
296
|
-
lowered = head.lower()
|
|
297
|
-
if lowered == "echo":
|
|
298
|
-
return SimulatedCommand(payload, "echo", None, None)
|
|
299
|
-
if lowered == "embed":
|
|
300
|
-
valid, error = validate_embed_payload(payload)
|
|
301
|
-
embed_preview = parse_embed_payload(payload) if valid else None
|
|
302
|
-
return SimulatedCommand(payload, "embed", error, embed_preview)
|
|
303
|
-
if head.startswith("-") and _is_embed_flag(head):
|
|
304
|
-
payload = text
|
|
305
|
-
valid, error = validate_embed_payload(payload)
|
|
306
|
-
embed_preview = parse_embed_payload(payload) if valid else None
|
|
307
|
-
return SimulatedCommand(payload, "embed", error, embed_preview)
|
|
308
|
-
return SimulatedCommand(None, head, None, None)
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
def _extract_command_head_and_payload(text: str) -> tuple[str | None, str]:
|
|
312
|
-
"""Prefer the first non-empty line; fall back to any embed line later."""
|
|
313
|
-
lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
|
|
314
|
-
if not lines:
|
|
315
|
-
return None, ""
|
|
316
|
-
head, payload = _split_head_and_payload_from_line(lines[0])
|
|
317
|
-
if _is_embed_flag(head):
|
|
318
|
-
# Treat the entire payload (including the head line) as embed flags so multiple lines are preserved.
|
|
319
|
-
return head, "\n".join(lines)
|
|
320
|
-
if head and head.lower() in ("embed", "echo"):
|
|
321
|
-
return head, _merge_payload(payload, lines[1:])
|
|
322
|
-
for idx, line in enumerate(lines[1:], start=1):
|
|
323
|
-
possible_head, possible_payload = _split_head_and_payload_from_line(line)
|
|
324
|
-
if possible_head and (possible_head.lower() == "embed" or _is_embed_flag(possible_head)):
|
|
325
|
-
return possible_head, _merge_payload(possible_payload, lines[idx + 1 :])
|
|
326
|
-
return head, _merge_payload(payload, lines[1:])
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
def _split_head_and_payload_from_line(line: str) -> tuple[str | None, str]:
|
|
330
|
-
if not line:
|
|
331
|
-
return None, ""
|
|
332
|
-
parts = line.split(maxsplit=1)
|
|
333
|
-
head = parts[0]
|
|
334
|
-
payload = parts[1] if len(parts) > 1 else ""
|
|
335
|
-
return head, payload
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
def _merge_payload(first_payload: str, trailing_lines: list[str]) -> str:
|
|
339
|
-
payload = first_payload
|
|
340
|
-
if trailing_lines:
|
|
341
|
-
payload = (payload + "\n" if payload else "") + "\n".join(trailing_lines)
|
|
342
|
-
return payload
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
def _is_embed_flag(flag: str) -> bool:
|
|
346
|
-
return flag.lower() in {"-title", "-desc", "-thumb", "-image", "-footer", "-f", "-color", "-t"}
|