-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#78: Adjust module structure and BeheadingInterpreter docstring
- Loading branch information
Showing
4 changed files
with
46 additions
and
38 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ers/command/test_beheading_interpreter.py → ...rs/wrappers/test_beheading_interpreter.py
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,5 @@ | ||
from tickit.adapters.interpreters.wrappers.beheading_interpreter import ( | ||
BeheadingInterpreter, | ||
) | ||
|
||
__all__ = ["BeheadingInterpreter"] |
40 changes: 40 additions & 0 deletions
40
tickit/adapters/interpreters/wrappers/beheading_interpreter.py
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,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) |