-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import NamedTuple, List, Sequence | ||
|
||
from hippolyzer.lib.base.message.message import Message | ||
from hippolyzer.lib.base.templates import ChatType | ||
|
||
|
||
class RLVCommand(NamedTuple): | ||
behaviour: str | ||
param: str | ||
options: List[str] | ||
|
||
|
||
class RLVParser: | ||
@staticmethod | ||
def is_rlv_message(msg: Message) -> bool: | ||
chat: str = msg["ChatData"]["Message"] | ||
chat_type: int = msg["ChatData"]["ChatType"] | ||
return chat and chat.startswith("@") and chat_type == ChatType.OWNER | ||
|
||
@staticmethod | ||
def parse_chat(chat: str) -> List[RLVCommand]: | ||
assert chat.startswith("@") | ||
chat = chat.lstrip("@") | ||
commands = [] | ||
for command_str in chat.split(","): | ||
if not command_str: | ||
continue | ||
# RLV-style command, `<cmd>(:<option1>;<option2>)?(=<param>)?` | ||
# Roughly (?<behaviour>[^:=]+)(:(?<option>[^=]*))?=(?<param>\w+) | ||
options, _, param = command_str.partition("=") | ||
behaviour, _, options = options.partition(":") | ||
# TODO: Not always correct, commands can specify their own parsing for the option field | ||
# maybe special-case these? | ||
options = options.split(";") if options else [] | ||
commands.append(RLVCommand(behaviour, param, options)) | ||
return commands | ||
|
||
@staticmethod | ||
def format_chat(commands: Sequence[RLVCommand]) -> str: | ||
assert commands | ||
chat = "" | ||
for command in commands: | ||
if chat: | ||
chat += "," | ||
|
||
chat += command.behaviour | ||
if command.options: | ||
chat += ":" + ";".join(command.options) | ||
if command.param: | ||
chat += "=" + command.param | ||
return "@" + chat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import unittest | ||
|
||
from hippolyzer.lib.base.message.message import Message, Block | ||
from hippolyzer.lib.base.templates import ChatType | ||
from hippolyzer.lib.client.rlv import RLVParser, RLVCommand | ||
|
||
|
||
class TestRLV(unittest.TestCase): | ||
def test_is_rlv_command(self): | ||
msg = Message( | ||
"ChatFromSimulator", | ||
Block("ChatData", Message="@foobar", ChatType=ChatType.OWNER) | ||
) | ||
self.assertTrue(RLVParser.is_rlv_message(msg)) | ||
msg["ChatData"]["ChatType"] = ChatType.NORMAL | ||
self.assertFalse(RLVParser.is_rlv_message(msg)) | ||
|
||
def test_rlv_parse_single_command(self): | ||
cmd = RLVParser.parse_chat("@foo:bar;baz=quux")[0] | ||
self.assertEqual("foo", cmd.behaviour) | ||
self.assertListEqual(["bar", "baz"], cmd.options) | ||
self.assertEqual("quux", cmd.param) | ||
|
||
def test_rlv_parse_multiple_commands(self): | ||
cmds = RLVParser.parse_chat("@foo:bar;baz=quux,bazzy") | ||
self.assertEqual("foo", cmds[0].behaviour) | ||
self.assertListEqual(["bar", "baz"], cmds[0].options) | ||
self.assertEqual("quux", cmds[0].param) | ||
self.assertEqual("bazzy", cmds[1].behaviour) | ||
|
||
def test_rlv_format_commands(self): | ||
chat = RLVParser.format_chat([ | ||
RLVCommand("foo", "quux", ["bar", "baz"]), | ||
RLVCommand("bazzy", "", []) | ||
]) | ||
self.assertEqual("@foo:bar;baz=quux,bazzy", chat) |