Test and fix exclusive output

This commit is contained in:
Joscha
2021-04-29 15:26:10 +02:00
parent 2e85d26b6b
commit d96a361325
4 changed files with 55 additions and 19 deletions

View File

@ -1,7 +1,30 @@
from typing import Optional
import functools
import contextvars
import asyncio
import getpass
from typing import Any, Callable, Optional, TypeVar
T = TypeVar("T")
def prompt_yes_no(query: str, default: Optional[bool]) -> bool:
# TODO When switching to 3.9, use asyncio.to_thread instead of this
async def to_thread(func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
# https://github.com/python/cpython/blob/8d47f92d46a92a5931b8f3dcb4a484df672fc4de/Lib/asyncio/threads.py
loop = asyncio.get_event_loop()
ctx = contextvars.copy_context()
func_call = functools.partial(ctx.run, func, *args, **kwargs)
return await loop.run_in_executor(None, func_call)
async def ainput(prompt: Optional[str] = None) -> str:
return await to_thread(lambda: input(prompt))
async def agetpass(prompt: Optional[str] = None) -> str:
return await to_thread(lambda: getpass.getpass(prompt))
async def prompt_yes_no(query: str, default: Optional[bool]) -> bool:
"""
Asks the user a yes/no question and returns their choice.
"""
@ -14,7 +37,7 @@ def prompt_yes_no(query: str, default: Optional[bool]) -> bool:
query += " [y/n] "
while True:
response = input(query).strip().lower()
response = (await ainput(query)).strip().lower()
if response == "y":
return True
elif response == "n":