From ed5097b4779e844183aa3a2d0c2d10cf40ccc0e5 Mon Sep 17 00:00:00 2001 From: Matthew Pritchard Date: Tue, 9 Aug 2022 10:41:38 +0100 Subject: [PATCH] #78: Adjust module structure and BeheadingInterpreter docstring --- .../test_beheading_interpreter.py | 2 +- .../command/command_interpreter.py | 37 ----------------- .../interpreters/wrappers/__init__.py | 5 +++ .../wrappers/beheading_interpreter.py | 40 +++++++++++++++++++ 4 files changed, 46 insertions(+), 38 deletions(-) rename tests/adapters/interpreters/{command => wrappers}/test_beheading_interpreter.py (90%) create mode 100644 tickit/adapters/interpreters/wrappers/__init__.py create mode 100644 tickit/adapters/interpreters/wrappers/beheading_interpreter.py diff --git a/tests/adapters/interpreters/command/test_beheading_interpreter.py b/tests/adapters/interpreters/wrappers/test_beheading_interpreter.py similarity index 90% rename from tests/adapters/interpreters/command/test_beheading_interpreter.py rename to tests/adapters/interpreters/wrappers/test_beheading_interpreter.py index 02b9b2837..b9ea3611a 100644 --- a/tests/adapters/interpreters/command/test_beheading_interpreter.py +++ b/tests/adapters/interpreters/wrappers/test_beheading_interpreter.py @@ -1,7 +1,7 @@ import pytest from mock import ANY, AsyncMock -from tickit.adapters.interpreters.command import BeheadingInterpreter +from tickit.adapters.interpreters.wrappers import BeheadingInterpreter @pytest.mark.asyncio diff --git a/tickit/adapters/interpreters/command/command_interpreter.py b/tickit/adapters/interpreters/command/command_interpreter.py index 425324b13..935d9d749 100644 --- a/tickit/adapters/interpreters/command/command_interpreter.py +++ b/tickit/adapters/interpreters/command/command_interpreter.py @@ -100,40 +100,3 @@ async def handle( return resp, command.interrupt resp = CommandInterpreter.unknown_command() return resp, False - - -class BeheadingInterpreter(Interpreter[AnyStr]): - """A decorator for an interpreter which strips a header from a message. - - An interpreter decorator that takes a message, strips off a header of a fixed size - and passes the stripped message on to the decorated interpreter - """ - - def __init__(self, interpreter: Interpreter[AnyStr], header_size: int) -> None: - """A decorator for an interpreter which strips a header from a message. - - Args: - interpreter (Interpreter): The interpreter the message is passed on to - after the header is stripped. - header_size (int): The number of characters in the header. - """ - super().__init__() - self.interpreter: Interpreter[AnyStr] = interpreter - self.header_size: int = header_size - - async def handle( - self, adapter: Adapter, message: AnyStr - ) -> Tuple[AsyncIterable[AnyStr], bool]: - """Removes a header from the start of a message, and passes it on to an interpreter. - - Args: - adapter (Adapter): The adapter in which the function should be executed - message: (AnyStr): The handled message, of which the header is removed. - - Returns: - Tuple[AsyncIterable[Union[str, bytes]], bool]: - A tuple of the asynchronous iterable of reply messages and a flag - indicating whether an interrupt should be raised by the adapter. - """ - message = message[self.header_size :] - return await self.interpreter.handle(adapter, message) diff --git a/tickit/adapters/interpreters/wrappers/__init__.py b/tickit/adapters/interpreters/wrappers/__init__.py new file mode 100644 index 000000000..f86ee1ec3 --- /dev/null +++ b/tickit/adapters/interpreters/wrappers/__init__.py @@ -0,0 +1,5 @@ +from tickit.adapters.interpreters.wrappers.beheading_interpreter import ( + BeheadingInterpreter, +) + +__all__ = ["BeheadingInterpreter"] diff --git a/tickit/adapters/interpreters/wrappers/beheading_interpreter.py b/tickit/adapters/interpreters/wrappers/beheading_interpreter.py new file mode 100644 index 000000000..e5cd7e0ce --- /dev/null +++ b/tickit/adapters/interpreters/wrappers/beheading_interpreter.py @@ -0,0 +1,40 @@ +from typing import AnyStr, AsyncIterable, Tuple + +from tickit.core.adapter import Adapter, Interpreter + + +class BeheadingInterpreter(Interpreter[AnyStr]): + """A wrapper for an interpreter which strips a header from a message. + + An interpreter wrapper that takes a message, strips off a header of a fixed size + and passes the stripped message on to the wrapped interpreter + """ + + def __init__(self, interpreter: Interpreter[AnyStr], header_size: int) -> None: + """A wrapper for an interpreter which strips a header from a message. + + Args: + interpreter (Interpreter): The interpreter the message is passed on to + after the header is stripped. + header_size (int): The number of characters in the header. + """ + super().__init__() + self.interpreter: Interpreter[AnyStr] = interpreter + self.header_size: int = header_size + + async def handle( + self, adapter: Adapter, message: AnyStr + ) -> Tuple[AsyncIterable[AnyStr], bool]: + """Removes a header from the start of a message, and passes it on to an interpreter. + + Args: + adapter (Adapter): The adapter in which the function should be executed + message: (AnyStr): The handled message, of which the header is removed. + + Returns: + Tuple[AsyncIterable[Union[str, bytes]], bool]: + A tuple of the asynchronous iterable of reply messages and a flag + indicating whether an interrupt should be raised by the adapter. + """ + message = message[self.header_size :] + return await self.interpreter.handle(adapter, message)