avrae-ls 0.3.1__py3-none-any.whl → 0.4.1__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/alias_preview.py CHANGED
@@ -2,7 +2,7 @@ from __future__ import annotations
2
2
 
3
3
  import re
4
4
  import shlex
5
- from dataclasses import dataclass
5
+ from dataclasses import asdict, dataclass, field
6
6
  from typing import Any, Optional, Tuple
7
7
 
8
8
  from .parser import DRACONIC_RE
@@ -19,6 +19,45 @@ class RenderedAlias:
19
19
  last_value: Any | None = None
20
20
 
21
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
+
22
61
  def _strip_alias_header(text: str) -> str:
23
62
  lines = text.splitlines()
24
63
  if lines and lines[0].lstrip().startswith("!alias"):
@@ -82,6 +121,58 @@ def validate_embed_payload(payload: str) -> Tuple[bool, str | None]:
82
121
  return _validate_embed_flags(text)
83
122
 
84
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
+
85
176
  def _validate_embed_flags(text: str) -> Tuple[bool, str | None]:
86
177
  """Validate embed flags according to Avrae's help text."""
87
178
  if not text:
@@ -164,17 +255,92 @@ def _validate_timeout_arg(value: str | None) -> Tuple[bool, str | None, int]:
164
255
  return True, None, consumed
165
256
 
166
257
 
167
- def simulate_command(command: str) -> tuple[str | None, str | None, str | None]:
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:
168
289
  """Very small shim to preview common commands."""
169
- text = command.strip()
290
+ text = _strip_alias_header(command).strip()
170
291
  if not text:
171
- return None, None, None
172
- head, *rest = text.split(maxsplit=1)
173
- payload = rest[0] if rest else ""
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)
174
296
  lowered = head.lower()
175
297
  if lowered == "echo":
176
- return payload, "echo", None
298
+ return SimulatedCommand(payload, "echo", None, None)
177
299
  if lowered == "embed":
178
300
  valid, error = validate_embed_payload(payload)
179
- return payload, "embed", error
180
- return None, head, None
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"}