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 Normal View History

2021-01-10 17:37:18 +00:00
"""
2021-04-13 17:50:28 +00:00
.. todo:: Document this.
2021-01-10 17:37:18 +00:00
"""
from __future__ import annotations
import royalnet.royaltyping as t
import logging
import asyncio
import royalnet.engineer as engi
import click
2021-03-31 03:01:40 +00:00
import datetime
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):
def _partialcommand_pattern(self, partial) -> str:
if partial.syntax:
return r"^{name}\s+{syntax}$"
else:
return r"^{name}$"
2021-01-10 17:37:18 +00:00
2021-04-13 17:50:28 +00:00
@property
def namespace(self):
return "console"
2021-01-10 17:37:18 +00:00
2021-03-31 03:01:40 +00:00
async def run(self, cycles: t.Union[bool, int] = True) -> 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-03-31 03:01:40 +00:00
while cycles:
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-13 17:50:28 +00:00
await self.put_projectile(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
)