1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/royalnet_console/pda.py

53 lines
1.4 KiB
Python
Raw Permalink Normal View History

2021-01-10 17:37:18 +00:00
"""
2021-04-17 15:10:28 +00:00
This module contains the :class:`.ConsolePDAImplementation`.
2021-01-10 17:37:18 +00:00
"""
from __future__ import annotations
import datetime
2021-01-10 17:37:18 +00:00
import logging
2021-04-13 23:58:43 +00:00
import math
import click
import royalnet.engineer as engi
import royalnet.royaltyping as t
2021-01-10 17:37:18 +00:00
2021-03-31 03:01:40 +00:00
from . import bullets
2021-01-10 17:37:18 +00:00
log = logging.getLogger(__name__)
2021-04-13 17:50:28 +00:00
class ConsolePDAImplementation(engi.ConversationListImplementation):
2021-04-17 15:10:28 +00:00
"""
A :class:`~.engi.ConversationListImplementation` which **blockingly** runs a small interactive chat in the command
line, and uses ``stdin`` e ``stdout`` to receive and send messages respectively.
"""
2021-04-13 17:50:28 +00:00
@property
def namespace(self):
return "console"
2021-01-10 17:37:18 +00:00
2021-04-13 23:58:43 +00:00
async def run(self, cycles: int = math.inf) -> t.NoReturn:
"""
2021-03-31 03:01:40 +00:00
Run the main loop of the :class:`.ConsolePDA` for ``cycles`` cycles, or unlimited cycles if the parameter is
:data:`True`.
"""
2021-04-13 23:58:43 +00:00
while cycles > 0:
message = click.prompt("", type=str, prompt_suffix=">>> ", show_default=False)
2021-03-31 03:01:40 +00:00
log.debug(f"Received a new input: {message!r}")
log.debug(f"Creating ConsoleMessageReceived from: {message!r}")
projectile = bullets.ConsoleMessageReceived(_text=message, _timestamp=datetime.datetime.now())
2021-01-10 17:37:18 +00:00
2021-03-31 03:01:40 +00:00
log.debug(f"Putting projectile: {projectile!r}")
2021-04-17 01:21:30 +00:00
await self.put(key="TERMINAL", projectile=projectile)
2021-01-10 17:37:18 +00:00
2021-03-31 03:01:40 +00:00
if isinstance(cycles, int):
cycles -= 1
2021-01-10 17:37:18 +00:00
__all__ = (
2021-04-13 17:50:28 +00:00
"ConsolePDAImplementation",
2021-01-10 17:37:18 +00:00
)