Skip to content

Commit

Permalink
#78: Adjust module structure and BeheadingInterpreter docstring
Browse files Browse the repository at this point in the history
  • Loading branch information
MattPrit committed Aug 9, 2022
1 parent 9c1ea3b commit ed5097b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
37 changes: 0 additions & 37 deletions tickit/adapters/interpreters/command/command_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 5 additions & 0 deletions tickit/adapters/interpreters/wrappers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from tickit.adapters.interpreters.wrappers.beheading_interpreter import (
BeheadingInterpreter,
)

__all__ = ["BeheadingInterpreter"]
40 changes: 40 additions & 0 deletions tickit/adapters/interpreters/wrappers/beheading_interpreter.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ed5097b

Please sign in to comment.