1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00
royalnet/royalnet_console/pda.py

61 lines
1.6 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
2021-04-13 23:58:43 +00:00
import math
2021-01-10 17:37:18 +00:00
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-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-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
2021-04-14 23:06:55 +00:00
async def _handle_conversation_exc(
self,
dispenser: engi.Dispenser,
conv: engi.ConversationProtocol,
exception: Exception,
) -> None:
self.log.error(f"ERROR: {exception}")
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
)