tmux-amq/src/tamq/routing.py

30 lines
847 B
Python
Raw Normal View History

from __future__ import annotations
import re
from dataclasses import dataclass
ADDRESS = re.compile(r"^To:([a-z0-9][a-z0-9._-]*):[ \t]*(.+)$")
COMMAND = re.compile(r"^Cmd:[ \t]*(\S(?:.*\S)?)?[ \t]*$")
@dataclass(frozen=True)
class RoutedMessage:
target_repo: str
body: str
def parse_address_line(line: str) -> RoutedMessage | None:
"""Parse one complete direct-address line; ordinary input returns None."""
match = ADDRESS.match(line.rstrip("\r\n"))
if not match:
return None
return RoutedMessage(target_repo=match.group(1), body=match.group(2))
def parse_command_line(line: str) -> str | None:
"""Return an operator command body; worker output must never call this."""
match = COMMAND.match(line.rstrip("\r\n"))
if not match or not match.group(1):
return None
return match.group(1)