From 2574651d6e94be58532d4f52b63ff5d94c1caa27 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 14 Jan 2021 16:52:54 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Use=20click.echo/click.prompt=20?= =?UTF-8?q?instead=20of=20print/input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poetry.lock | 17 ++++++++++++++--- pyproject.toml | 2 ++ royalnet_console/pda.py | 27 ++++++++++++++++++--------- royalnet_console/utils/message.py | 3 ++- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/poetry.lock b/poetry.lock index 10e4b1cd..8be734a4 100644 --- a/poetry.lock +++ b/poetry.lock @@ -56,14 +56,21 @@ category = "dev" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +[[package]] +name = "click" +version = "7.1.2" +description = "Composable command line interface toolkit" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" + [[package]] name = "colorama" version = "0.4.4" description = "Cross-platform colored terminal text." -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" -marker = "sys_platform == \"win32\"" [[package]] name = "docutils" @@ -432,7 +439,7 @@ socks = ["PySocks (>=1.5.6,<1.5.7 || >1.5.7,<2.0)"] [metadata] lock-version = "1.0" python-versions = "^3.9" -content-hash = "e5ce7e56ddb284da6206a9dab891023b5a8b057efd8b683e9b0c1dfb6d55131f" +content-hash = "b205a562d986cf6f37d26656d253e542a637727073f7d16e83ceb10029762d7e" [metadata.files] alabaster = [ @@ -459,6 +466,10 @@ chardet = [ {file = "chardet-4.0.0-py2.py3-none-any.whl", hash = "sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5"}, {file = "chardet-4.0.0.tar.gz", hash = "sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa"}, ] +click = [ + {file = "click-7.1.2-py2.py3-none-any.whl", hash = "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc"}, + {file = "click-7.1.2.tar.gz", hash = "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a"}, +] colorama = [ {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, {file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"}, diff --git a/pyproject.toml b/pyproject.toml index f5f07039..5023e410 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,8 @@ license = "AGPL-3.0-or-later" python = "^3.9" royalnet = "^6.0.0a37" psutil = "^5.8.0" +click = "^7.1.2" +colorama = "^0.4.4" [tool.poetry.dev-dependencies] pytest = "^6.1.1" diff --git a/royalnet_console/pda.py b/royalnet_console/pda.py index f3343387..615a3165 100644 --- a/royalnet_console/pda.py +++ b/royalnet_console/pda.py @@ -11,6 +11,7 @@ import royalnet.royaltyping as t import logging import asyncio import royalnet.engineer as engi +import click # Internal imports from . import magazine @@ -22,7 +23,7 @@ log = logging.getLogger(__name__) # Code class ConsolePDA: """ - .. todo:: Document the :class:`.ConsolePDA` class. + A PDA which handles :mod:`royalnet` input and output using a terminal as source. """ def __init__(self): @@ -30,10 +31,13 @@ class ConsolePDA: log.debug(f"Creating new magazine...") self.mag = magazine.ConsoleMagazine() + """ + The + """ self.dispenser: t.Optional[engi.Dispenser] = None """ - The dispenser for this PDA. + The :class:`royalnet.engineer.dispenser.Dispenser` of this PDA. """ self.conversations: t.List[engi.Conversation] = [] @@ -42,9 +46,13 @@ class ConsolePDA: :class:`~royalnet.engineer.dispenser.Dispenser`. """ - async def run(self): + async def run(self) -> t.NoReturn: + """ + Run the main loop of the :class:`.ConsolePDA`. + """ + while True: - message = input() + message = click.prompt("", type=str, prompt_suffix=">>> ", show_default=False) log.debug(f"Received a new message: {message!r}") log.debug(f"Creating ConsoleMessage from: {message!r}") @@ -73,11 +81,12 @@ class ConsolePDA: def register_partial(self, part: engi.PartialCommand, names: t.List[str]) -> engi.Command: """ - Register a new :class:`PartialCommand` in the PDA, converting it to a :class:`Command` in the process. + Register a new :class:`~royalnet.engineer.command.PartialCommand` in the PDA, converting it to a + :class:`royalnet.engineer.Command` in the process. - :param part: The :class:`PartialCommand` to register. - :param names: The :attr:`~royalnet.engineer.Command.names` to register the command as. - :return: The resulting :class:`Command`. + :param part: The :class:`~royalnet.engineer.command.PartialCommand` to register. + :param names: The :attr:`~royalnet.engineer.command.Command.names` to register the command with. + :return: The resulting :class:`~royalnet.engineer.command.Command`. """ log.debug(f"Completing partial: {part!r}") if part.syntax: @@ -110,7 +119,7 @@ class ConsolePDA: log.debug(f"Putting bullet {bullet!r} in dispenser {self.dispenser!r}...") await self.dispenser.put(bullet) - log.debug("Awaiting another loop cycle") + log.debug("Awaiting another event loop cycle...") await asyncio.sleep(0) diff --git a/royalnet_console/utils/message.py b/royalnet_console/utils/message.py index 8bd7f0ac..ac78ded1 100644 --- a/royalnet_console/utils/message.py +++ b/royalnet_console/utils/message.py @@ -10,6 +10,7 @@ import royalnet.royaltyping as t # External imports import logging import royalnet.engineer as engi +import click # Internal imports # from . import something @@ -38,7 +39,7 @@ async def console_message(*, raise engi.exc.NotSupportedError("Console does not allow sending files.") log.debug("Sending message...") - print(text) + click.echo(text) log.debug("Creating bullet...") return mag.Message(_text=text)