diff --git a/pandablocks/commands.py b/pandablocks/commands.py index 113e361e5..abb063df9 100644 --- a/pandablocks/commands.py +++ b/pandablocks/commands.py @@ -45,6 +45,7 @@ "GetLine", "GetMultiline", "Put", + "Append", "Arm", "Disarm", "GetBlockInfo", @@ -293,6 +294,29 @@ def execute(self) -> ExchangeGenerator[None]: assert ex.line == "OK" +@dataclass +class Append(Command[None]): + """Append the value of a table field. + + Args: + field: The field, attribute, or star command to append + value: The value, list of strings, to append + + For example:: + + Append("SEQ1.TABLE", ["1048576", "0", "1000", "1000"]) + """ + + field: str + value: List[str] + + def execute(self) -> ExchangeGenerator[None]: + # Multiline table with blank line to terminate + ex = Exchange([f"{self.field}<<"] + self.value + [""]) + yield ex + assert ex.line == "OK" + + class Arm(Command[None]): """Arm PCAP for an acquisition by sending ``*PCAP.ARM=``""" diff --git a/tests/test_pandablocks.py b/tests/test_pandablocks.py index 65b608b92..19e4ea30e 100644 --- a/tests/test_pandablocks.py +++ b/tests/test_pandablocks.py @@ -3,6 +3,7 @@ import pytest from pandablocks.commands import ( + Append, ChangeGroup, CommandException, Get, @@ -188,6 +189,21 @@ def test_connect_put_no_value(): assert get_responses(conn, b"OK\n") == [(cmd, None)] +def test_connect_append(): + conn = ControlConnection() + cmd = Append("SEQ1.TABLE", ["1048576", "0", "1000", "1000"]) + assert conn.send(cmd) == b"SEQ1.TABLE<<\n1048576\n0\n1000\n1000\n\n" + assert get_responses(conn, b"OK\n") == [(cmd, None)] + + +def test_connect_append_multi_bad_list_format(): + """Confirm that an invalid data format raises the expected exception""" + conn = ControlConnection() + cmd = Append("SEQ1.TABLE", [1, 2, 3]) + with pytest.raises(TypeError): + assert conn.send(cmd) == b"" + + def test_get_block_info(): conn = ControlConnection() cmd = GetBlockInfo()