generated from daniil-berg/boilerplate-py
Compare commits
No commits in common. "b6aed727e941ad587c5ae5974f5b7ee7a5a7493e" and "538b9cc91c35a9da8dd88f55ade812cc848d801c" have entirely different histories.
b6aed727e9
...
538b9cc91c
@ -55,13 +55,5 @@ class ServerException(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class UnknownTaskPoolClass(ServerException):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class NotATaskPool(ServerException):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class HelpRequested(ServerException):
|
class HelpRequested(ServerException):
|
||||||
pass
|
pass
|
||||||
|
@ -19,6 +19,7 @@ Miscellaneous helper functions.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
import re
|
||||||
from asyncio.coroutines import iscoroutinefunction
|
from asyncio.coroutines import iscoroutinefunction
|
||||||
from asyncio.queues import Queue
|
from asyncio.queues import Queue
|
||||||
from inspect import getdoc
|
from inspect import getdoc
|
||||||
@ -56,7 +57,7 @@ def tasks_str(num: int) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def get_first_doc_line(obj: object) -> str:
|
def get_first_doc_line(obj: object) -> str:
|
||||||
return getdoc(obj).strip().split("\n", 1)[0].strip()
|
return getdoc(obj).strip().split("\n", 1)[0]
|
||||||
|
|
||||||
|
|
||||||
async def return_or_exception(_function_to_execute: AnyCallableT, *args, **kwargs) -> Union[T, Exception]:
|
async def return_or_exception(_function_to_execute: AnyCallableT, *args, **kwargs) -> Union[T, Exception]:
|
||||||
|
@ -5,9 +5,9 @@ from asyncio.streams import StreamReader, StreamWriter
|
|||||||
from typing import Callable, Optional, Union, TYPE_CHECKING
|
from typing import Callable, Optional, Union, TYPE_CHECKING
|
||||||
|
|
||||||
from .constants import CMD, SESSION_PARSER_WRITER, SESSION_MSG_BYTES, CLIENT_INFO
|
from .constants import CMD, SESSION_PARSER_WRITER, SESSION_MSG_BYTES, CLIENT_INFO
|
||||||
from .exceptions import HelpRequested, NotATaskPool, UnknownTaskPoolClass
|
from .exceptions import HelpRequested
|
||||||
from .helpers import get_first_doc_line, return_or_exception, tasks_str
|
from .helpers import get_first_doc_line, return_or_exception, tasks_str
|
||||||
from .pool import BaseTaskPool, TaskPool, SimpleTaskPool
|
from .pool import TaskPool, SimpleTaskPool
|
||||||
from .session_parser import CommandParser, NUM
|
from .session_parser import CommandParser, NUM
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -67,9 +67,6 @@ class ControlSession:
|
|||||||
CMD.FUNC_NAME, short_help=get_first_doc_line(self._pool.__class__.func_name.fget)
|
CMD.FUNC_NAME, short_help=get_first_doc_line(self._pool.__class__.func_name.fget)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _add_advanced_commands(self) -> None:
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def _init_parser(self, client_terminal_width: int) -> None:
|
def _init_parser(self, client_terminal_width: int) -> None:
|
||||||
parser_kwargs = {
|
parser_kwargs = {
|
||||||
'prog': '',
|
'prog': '',
|
||||||
@ -79,13 +76,9 @@ class ControlSession:
|
|||||||
self._parser = CommandParser(**parser_kwargs)
|
self._parser = CommandParser(**parser_kwargs)
|
||||||
self._add_base_commands()
|
self._add_base_commands()
|
||||||
if isinstance(self._pool, TaskPool):
|
if isinstance(self._pool, TaskPool):
|
||||||
self._add_advanced_commands()
|
pass # TODO
|
||||||
elif isinstance(self._pool, SimpleTaskPool):
|
elif isinstance(self._pool, SimpleTaskPool):
|
||||||
self._add_simple_commands()
|
self._add_simple_commands()
|
||||||
elif isinstance(self._pool, BaseTaskPool):
|
|
||||||
raise UnknownTaskPoolClass(f"No interface defined for {self._pool.__class__.__name__}")
|
|
||||||
else:
|
|
||||||
raise NotATaskPool(f"Not a task pool instance: {self._pool}")
|
|
||||||
|
|
||||||
async def client_handshake(self) -> None:
|
async def client_handshake(self) -> None:
|
||||||
client_info = json.loads((await self._reader.read(SESSION_MSG_BYTES)).decode().strip())
|
client_info = json.loads((await self._reader.read(SESSION_MSG_BYTES)).decode().strip())
|
||||||
|
@ -94,35 +94,3 @@ class HelpersTestCase(IsolatedAsyncioTestCase):
|
|||||||
self.assertEqual("tasks", helpers.tasks_str(2))
|
self.assertEqual("tasks", helpers.tasks_str(2))
|
||||||
self.assertEqual("tasks", helpers.tasks_str(-10))
|
self.assertEqual("tasks", helpers.tasks_str(-10))
|
||||||
self.assertEqual("tasks", helpers.tasks_str(42))
|
self.assertEqual("tasks", helpers.tasks_str(42))
|
||||||
|
|
||||||
def test_get_first_doc_line(self):
|
|
||||||
expected_output = 'foo bar baz'
|
|
||||||
mock_obj = MagicMock(__doc__=f"""{expected_output}
|
|
||||||
something else
|
|
||||||
|
|
||||||
even more
|
|
||||||
""")
|
|
||||||
output = helpers.get_first_doc_line(mock_obj)
|
|
||||||
self.assertEqual(expected_output, output)
|
|
||||||
|
|
||||||
async def test_return_or_exception(self):
|
|
||||||
expected_output = '420'
|
|
||||||
mock_func = AsyncMock(return_value=expected_output)
|
|
||||||
args = (1, 3, 5)
|
|
||||||
kwargs = {'a': 1, 'b': 2, 'c': 'foo'}
|
|
||||||
output = await helpers.return_or_exception(mock_func, *args, **kwargs)
|
|
||||||
self.assertEqual(expected_output, output)
|
|
||||||
mock_func.assert_awaited_once_with(*args, **kwargs)
|
|
||||||
|
|
||||||
mock_func = MagicMock(return_value=expected_output)
|
|
||||||
output = await helpers.return_or_exception(mock_func, *args, **kwargs)
|
|
||||||
self.assertEqual(expected_output, output)
|
|
||||||
mock_func.assert_called_once_with(*args, **kwargs)
|
|
||||||
|
|
||||||
class TestException(Exception):
|
|
||||||
pass
|
|
||||||
test_exception = TestException()
|
|
||||||
mock_func = MagicMock(side_effect=test_exception)
|
|
||||||
output = await helpers.return_or_exception(mock_func, *args, **kwargs)
|
|
||||||
self.assertEqual(test_exception, output)
|
|
||||||
mock_func.assert_called_once_with(*args, **kwargs)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user