Add asyncio support

This commit is contained in:
Maxim Khomutov 2022-10-01 13:41:08 +03:00
parent 069a7d9356
commit 8cd4e59918
2 changed files with 46 additions and 27 deletions

View File

@ -10,9 +10,15 @@ import builtins
import logging import logging
import sys import sys
import traceback import traceback
import asyncio
from builtins import RecursionError from builtins import RecursionError
from typing import AnyStr from typing import AnyStr
try:
from aioconsole import ainput
except ImportError:
ainput = None
class ConsoleIO: class ConsoleIO:
@ -67,6 +73,8 @@ class Console:
self.is_run = False self.is_run = False
self.async_loop = async_loop
self.get_IO = ConsoleIO self.get_IO = ConsoleIO
def __debug(self, *x): def __debug(self, *x):
@ -126,7 +134,6 @@ class Console:
message += f"%-{max_len}s; %-{max_len_v}s; %s\n" % (k, v, doc) message += f"%-{max_len}s; %-{max_len_v}s; %s\n" % (k, v, doc)
else: else:
if doc is None: if doc is None:
doc = " No help message found" doc = " No help message found"
message += f" %{max_len}s :%s\n" % (k, doc) message += f" %{max_len}s :%s\n" % (k, doc)
@ -229,6 +236,29 @@ class Console:
builtins.print = self.__builtins_print builtins.print = self.__builtins_print
def _cli_logic(self, cmd_in):
try:
cmd = cmd_in.split(" ")[0]
if cmd == "":
pass
else:
command_object = self.__alias.get(cmd)
if command_object:
command = command_object['f']
if command_object['e']:
argv = cmd_in[len(cmd) + 1:].split(" ")
output = command(argv)
else:
output = command()
self.log(str(output))
else:
self.log(self.__not_found % cmd)
except Exception as e:
ConsoleIO.write_err("\rDuring the execution of the command, an error occurred:\n\n" +
str(traceback.format_exc()) +
"\nType Enter to continue.")
def run(self) -> None: def run(self) -> None:
""" """
def run(self) -> None: def run(self) -> None:
@ -245,22 +275,8 @@ class Console:
""" """
self.__debug(f"run while {whl}") self.__debug(f"run while {whl}")
while whl: while whl:
try:
ConsoleIO.write("\r" + self.__prompt_in + " ") ConsoleIO.write("\r" + self.__prompt_in + " ")
cmd_in = ConsoleIO.read() self._cli_logic(ConsoleIO.read())
cmd = cmd_in.split(" ")[0]
if cmd == "":
pass
else:
command_object = self.__alias.get(cmd)
if command_object:
command = command_object['f']
if command_object['e']:
argv = cmd_in[len(cmd) + 1:].split(" ")
output = command(argv)
else:
output = command()
self.log(str(output))
async def async_run(self) -> None: async def async_run(self) -> None:
""" """

View File

@ -6,8 +6,8 @@
# (c) ahegao.ovh 2022 # (c) ahegao.ovh 2022
from _typeshed import SupportsWrite from _typeshed import SupportsWrite
from builtins import function from asyncio import AbstractEventLoop
from typing import AnyStr from typing import AnyStr, NoReturn
class ConsoleIO: class ConsoleIO:
@ -29,7 +29,8 @@ class Console(object):
prompt_out: str = "]:", prompt_out: str = "]:",
not_found: str = "Command \"%s\" not found in alias.", not_found: str = "Command \"%s\" not found in alias.",
file: SupportsWrite[str] or None = Console, file: SupportsWrite[str] or None = Console,
debug: bool = False) -> None: debug: bool = False,
async_loop: AbstractEventLoop = None) -> NoReturn:
self.get_IO: ConsoleIO = ConsoleIO self.get_IO: ConsoleIO = ConsoleIO
self.is_run: bool = False self.is_run: bool = False
@ -38,11 +39,13 @@ class Console(object):
@property @property
def alias(self) -> dict: ... def alias(self) -> dict: ...
def add(self, key: str, func, argv: bool = False, man: str = "No have manual message") -> dict:... def add(self, key: str, func, argv: bool = False, man: str = "No have manual message") -> dict:...
def log(self, s: AnyStr, r='\r') -> None: ... def log(self, s: AnyStr, r='\r') -> NoReturn: ...
def __lshift__(self, s: AnyStr) -> None: def __lshift__(self, s: AnyStr) -> NoReturn:
self.write(s) self.write(s)
def write(self, s: AnyStr, r='\r') -> None: ... def write(self, s: AnyStr, r='\r') -> NoReturn: ...
def logger_hook(self) -> None: ... def logger_hook(self) -> NoReturn: ...
def builtins_hook(self) -> None: ... def builtins_hook(self) -> NoReturn: ...
def run(self) -> None: ... def run(self) -> NoReturn: ...
def run_while(self, whl) -> None:... def run_while(self, whl) -> NoReturn:...
async def async_run(self) -> NoReturn: ...
async def async_run_while(self, whl) -> NoReturn: ...