init commit
This commit is contained in:
51
app/bots/editor/messages.py
Normal file
51
app/bots/editor/messages.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from __future__ import annotations
|
||||
import shlex
|
||||
from typing import Dict
|
||||
|
||||
|
||||
class MessageParsers:
|
||||
@staticmethod
|
||||
def parse_template_invocation(s: str) -> tuple[str, Dict[str, str]]:
|
||||
"""
|
||||
Пример: "#promo title='Hi' url=https://x.y"
|
||||
-> ("promo", {"title":"Hi", "url":"https://x.y"})
|
||||
"""
|
||||
s = (s or "").strip()
|
||||
if not s.startswith("#"):
|
||||
raise ValueError("not a template invocation")
|
||||
parts = shlex.split(s)
|
||||
name = parts[0][1:]
|
||||
args: Dict[str, str] = {}
|
||||
for tok in parts[1:]:
|
||||
if "=" in tok:
|
||||
k, v = tok.split("=", 1)
|
||||
args[k] = v
|
||||
return name, args
|
||||
|
||||
@staticmethod
|
||||
def parse_key_value_lines(text: str) -> Dict[str, str]:
|
||||
"""
|
||||
Поддерживает:
|
||||
- построчно:
|
||||
key=value
|
||||
key2="quoted value"
|
||||
- одной строкой:
|
||||
key=value key2="quoted value"
|
||||
"""
|
||||
text = (text or "").strip()
|
||||
if not text:
|
||||
return {}
|
||||
if "\n" in text:
|
||||
out: Dict[str, str] = {}
|
||||
for line in text.splitlines():
|
||||
if "=" in line:
|
||||
k, v = line.split("=", 1)
|
||||
out[k.strip()] = v.strip().strip('"')
|
||||
return out
|
||||
|
||||
out: Dict[str, str] = {}
|
||||
for tok in shlex.split(text):
|
||||
if "=" in tok:
|
||||
k, v = tok.split("=", 1)
|
||||
out[k] = v
|
||||
return out
|
||||
Reference in New Issue
Block a user