zstdlib 0.10.5__tar.gz → 0.10.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {zstdlib-0.10.5/zstdlib.egg-info → zstdlib-0.10.6}/PKG-INFO +1 -1
- zstdlib-0.10.6/tests/test_prompt.py +103 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/__init__.py +4 -1
- zstdlib-0.10.6/zstdlib/prompt.py +93 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6/zstdlib.egg-info}/PKG-INFO +1 -1
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib.egg-info/SOURCES.txt +2 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/LICENSE +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/README.md +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/pyproject.toml +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/setup.cfg +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/__init__.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/log/__init__.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/log/base.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/log/test_cute.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/log/test_trace.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_ansi.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_enum.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_exc_capture.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_frozen.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_hybridmethod.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_io.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_lazy.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_not_set.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/tests/test_singleton.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/ansi.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/enum.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/exc_capture.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/frozen.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/hybridmethod.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/io.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/lazy.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/log/__init__.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/log/cute.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/log/trace.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/macos.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/not_set.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/py.typed +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/signal_cm.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib/singleton.py +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib.egg-info/dependency_links.txt +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib.egg-info/requires.txt +0 -0
- {zstdlib-0.10.5 → zstdlib-0.10.6}/zstdlib.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring,unused-variable
|
|
2
|
+
from unittest.mock import MagicMock, patch
|
|
3
|
+
import unittest
|
|
4
|
+
|
|
5
|
+
from zstdlib.prompt import prompt_until, prompt_for, yn_prompt
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
mock_input = lambda *x: patch("builtins.input", side_effect=x)
|
|
9
|
+
mock_print = lambda: patch("builtins.print")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def want_have(mock_p: MagicMock, msg: str, n: int) -> tuple[list[str], list[str]]:
|
|
13
|
+
if any(len(i.args) != 1 or i.kwargs for i in mock_p.call_args_list):
|
|
14
|
+
return False
|
|
15
|
+
return [i.args[0] for i in mock_p.call_args_list], [msg] * n
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class TestPromptUntil(unittest.TestCase):
|
|
19
|
+
msg = "print_me"
|
|
20
|
+
|
|
21
|
+
def test_str(self) -> None:
|
|
22
|
+
with mock_print() as stdout, mock_input("test", "test", "A") as stdout:
|
|
23
|
+
self.assertEqual(prompt_until(self.msg, until="a"), "a")
|
|
24
|
+
self.assertEqual(*want_have(stdout, self.msg, 3))
|
|
25
|
+
|
|
26
|
+
def test_set(self) -> None:
|
|
27
|
+
with mock_print() as stdout, mock_input("test", "ab", "A") as stdout:
|
|
28
|
+
self.assertEqual(prompt_until(self.msg, until=("b", "a")), "a")
|
|
29
|
+
self.assertEqual(*want_have(stdout, self.msg, 3))
|
|
30
|
+
|
|
31
|
+
def test_case(self) -> None:
|
|
32
|
+
with mock_print() as stdout, mock_input("test", "test", "a", "A") as stdout:
|
|
33
|
+
self.assertEqual(prompt_until(self.msg, until="A", ignore_case=False), "A")
|
|
34
|
+
self.assertEqual(*want_have(stdout, self.msg, 4))
|
|
35
|
+
|
|
36
|
+
def test_eof(self) -> None:
|
|
37
|
+
with mock_print() as stdout, mock_input("test", EOFError) as stdout:
|
|
38
|
+
with self.assertRaises(EOFError):
|
|
39
|
+
prompt_until(self.msg, until="A")
|
|
40
|
+
self.assertEqual(*want_have(stdout, self.msg, 2))
|
|
41
|
+
with mock_print() as stdout, mock_input("test", EOFError) as stdout:
|
|
42
|
+
self.assertIs(prompt_until(self.msg, until="A", eof=True), None)
|
|
43
|
+
self.assertEqual(*want_have(stdout, self.msg, 2))
|
|
44
|
+
|
|
45
|
+
def test_ctrlc(self) -> None:
|
|
46
|
+
with mock_print() as stdout, mock_input("test", KeyboardInterrupt) as stdout:
|
|
47
|
+
with self.assertRaises(KeyboardInterrupt):
|
|
48
|
+
prompt_until(self.msg, until="A")
|
|
49
|
+
self.assertEqual(*want_have(stdout, self.msg, 2))
|
|
50
|
+
with mock_print() as stdout, mock_input("test", KeyboardInterrupt) as stdout:
|
|
51
|
+
self.assertIs(prompt_until(self.msg, until="A", ctrl_c=True), None)
|
|
52
|
+
self.assertEqual(*want_have(stdout, self.msg, 2))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class TestPromptFor(unittest.TestCase):
|
|
56
|
+
out = "Type continue "
|
|
57
|
+
|
|
58
|
+
def test_basic(self) -> None:
|
|
59
|
+
with mock_print() as stdout, mock_input("test", "test", "cOntinue") as stdout:
|
|
60
|
+
self.assertTrue(prompt_for())
|
|
61
|
+
self.assertEqual(*want_have(stdout, self.out, 3))
|
|
62
|
+
|
|
63
|
+
def test_eof(self) -> None:
|
|
64
|
+
with mock_print() as stdout, mock_input("test", EOFError) as stdout:
|
|
65
|
+
with self.assertRaises(EOFError):
|
|
66
|
+
prompt_for()
|
|
67
|
+
self.assertEqual(*want_have(stdout, self.out, 2))
|
|
68
|
+
with mock_print() as stdout, mock_input("test", EOFError) as stdout:
|
|
69
|
+
self.assertFalse(prompt_for(eof=True))
|
|
70
|
+
self.assertEqual(*want_have(stdout, self.out, 2))
|
|
71
|
+
|
|
72
|
+
class TestYNPrompt(unittest.TestCase):
|
|
73
|
+
out = "Yes or no? [yn] "
|
|
74
|
+
|
|
75
|
+
def test_basic(self) -> None:
|
|
76
|
+
with mock_print() as stdout, mock_input("test", "yes", "y") as stdout:
|
|
77
|
+
self.assertTrue(yn_prompt())
|
|
78
|
+
self.assertEqual(*want_have(stdout, self.out, 3))
|
|
79
|
+
with mock_print() as stdout, mock_input("test", "no", "n") as stdout:
|
|
80
|
+
self.assertIs(yn_prompt(), False)
|
|
81
|
+
self.assertEqual(*want_have(stdout, self.out, 3))
|
|
82
|
+
|
|
83
|
+
def test_yes_no(self) -> None:
|
|
84
|
+
out = "Yes or no? [yes/no] "
|
|
85
|
+
with mock_print() as stdout, mock_input("test", "y", "yes") as stdout:
|
|
86
|
+
self.assertTrue(yn_prompt(yes_no=True))
|
|
87
|
+
self.assertEqual(*want_have(stdout, out, 3))
|
|
88
|
+
with mock_print() as stdout, mock_input("test", "n", "no") as stdout:
|
|
89
|
+
self.assertFalse(yn_prompt(yes_no=True))
|
|
90
|
+
self.assertEqual(*want_have(stdout, out, 3))
|
|
91
|
+
|
|
92
|
+
def test_eof(self) -> None:
|
|
93
|
+
with mock_print() as stdout, mock_input("test", EOFError) as stdout:
|
|
94
|
+
with self.assertRaises(EOFError):
|
|
95
|
+
yn_prompt()
|
|
96
|
+
self.assertEqual(*want_have(stdout, self.out, 2))
|
|
97
|
+
with mock_print() as stdout, mock_input("test", EOFError) as stdout:
|
|
98
|
+
self.assertFalse(yn_prompt(eof=True))
|
|
99
|
+
self.assertEqual(*want_have(stdout, self.out, 2))
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
if __name__ == "__main__":
|
|
103
|
+
unittest.main()
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
__version__ = "0.10.
|
|
1
|
+
__version__ = "0.10.6"
|
|
2
2
|
|
|
3
3
|
from .exc_capture import ExcCapture, ExcDelay
|
|
4
|
+
from .prompt import prompt_for, yn_prompt
|
|
4
5
|
from .hybridmethod import hybridmethod
|
|
5
6
|
from .not_set import NotSetType, NotSet
|
|
6
7
|
from .frozen import Freezable, frozen
|
|
@@ -24,5 +25,7 @@ __all__ = (
|
|
|
24
25
|
"lazy",
|
|
25
26
|
"log",
|
|
26
27
|
"macos",
|
|
28
|
+
"prompt_for",
|
|
27
29
|
"signal_cm",
|
|
30
|
+
"yn_prompt",
|
|
28
31
|
)
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from typing import Literal, overload
|
|
2
|
+
from collections.abc import Iterable
|
|
3
|
+
from shlex import quote
|
|
4
|
+
|
|
5
|
+
__all__ = ("prompt_for", "prompt_until", "yn_prompt")
|
|
6
|
+
|
|
7
|
+
@overload
|
|
8
|
+
def prompt_until(
|
|
9
|
+
msg: str,
|
|
10
|
+
until: str | Iterable[str],
|
|
11
|
+
*,
|
|
12
|
+
ignore_case: bool = True,
|
|
13
|
+
eof: Literal[False] = False,
|
|
14
|
+
ctrl_c: Literal[False] = False,
|
|
15
|
+
) -> str: ...
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@overload
|
|
19
|
+
def prompt_until(
|
|
20
|
+
msg: str,
|
|
21
|
+
until: str | Iterable[str],
|
|
22
|
+
*,
|
|
23
|
+
ignore_case: bool = True,
|
|
24
|
+
eof: bool = False,
|
|
25
|
+
ctrl_c: bool = False,
|
|
26
|
+
) -> str | None: ...
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def prompt_until(
|
|
30
|
+
msg: str,
|
|
31
|
+
until: str | Iterable[str],
|
|
32
|
+
*,
|
|
33
|
+
ignore_case: bool = True,
|
|
34
|
+
eof: bool = False,
|
|
35
|
+
ctrl_c: bool = False,
|
|
36
|
+
) -> str | None:
|
|
37
|
+
"""
|
|
38
|
+
Prompt the user with msg until they respone with a string in until, ignore case as needed
|
|
39
|
+
Whitespace is stripped
|
|
40
|
+
If ctrl_c and KeyboardInterrupt is caught, return None
|
|
41
|
+
If eof EOFError is caught, return None
|
|
42
|
+
Return the case-adjusted string the user entered
|
|
43
|
+
"""
|
|
44
|
+
cased = lambda x: (x.casefold() if ignore_case else x)
|
|
45
|
+
until = frozenset(map(cased, (until,) if isinstance(until, str) else until))
|
|
46
|
+
try:
|
|
47
|
+
while (ret := cased(input(msg).strip())) not in until:
|
|
48
|
+
pass
|
|
49
|
+
return ret
|
|
50
|
+
except (EOFError, KeyboardInterrupt) as e:
|
|
51
|
+
if eof if isinstance(e, EOFError) else ctrl_c:
|
|
52
|
+
return None
|
|
53
|
+
raise
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def prompt_for(
|
|
57
|
+
continue_str: str = "continue",
|
|
58
|
+
*,
|
|
59
|
+
ignore_case: bool = True,
|
|
60
|
+
eof: bool = False,
|
|
61
|
+
ctrl_c: bool = False,
|
|
62
|
+
) -> bool:
|
|
63
|
+
"""
|
|
64
|
+
Prompt the user to contine by entering continue_str, whitespace is stripped
|
|
65
|
+
If eof/ctrl_c, interpret EOFError/KeyboardInterrupt as commands to return False
|
|
66
|
+
"""
|
|
67
|
+
ret = prompt_until(
|
|
68
|
+
f"Type {quote(continue_str)} ",
|
|
69
|
+
ignore_case=ignore_case,
|
|
70
|
+
until={continue_str},
|
|
71
|
+
eof=eof,
|
|
72
|
+
ctrl_c=ctrl_c,
|
|
73
|
+
)
|
|
74
|
+
return isinstance(ret, str)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def yn_prompt(
|
|
78
|
+
msg: str = "Yes or no?",
|
|
79
|
+
*,
|
|
80
|
+
eof: bool = False,
|
|
81
|
+
ctrl_c: bool = False,
|
|
82
|
+
yes_no: bool = False,
|
|
83
|
+
) -> bool:
|
|
84
|
+
"""
|
|
85
|
+
Prompt the user to select from [yn], or [yes/no] if [yes/no], whitespace is stripped
|
|
86
|
+
If eof/ctrl_c, interpret EOFError/KeyboardInterrupt as commands to return False
|
|
87
|
+
"""
|
|
88
|
+
return prompt_until(
|
|
89
|
+
f"{msg} [{"yes/no" if yes_no else "yn"}] ",
|
|
90
|
+
until=("yes", "no") if yes_no else ("y", "n"),
|
|
91
|
+
eof=eof,
|
|
92
|
+
ctrl_c=ctrl_c,
|
|
93
|
+
) in ("y", "yes")
|
|
@@ -10,6 +10,7 @@ tests/test_hybridmethod.py
|
|
|
10
10
|
tests/test_io.py
|
|
11
11
|
tests/test_lazy.py
|
|
12
12
|
tests/test_not_set.py
|
|
13
|
+
tests/test_prompt.py
|
|
13
14
|
tests/test_singleton.py
|
|
14
15
|
tests/log/__init__.py
|
|
15
16
|
tests/log/base.py
|
|
@@ -25,6 +26,7 @@ zstdlib/io.py
|
|
|
25
26
|
zstdlib/lazy.py
|
|
26
27
|
zstdlib/macos.py
|
|
27
28
|
zstdlib/not_set.py
|
|
29
|
+
zstdlib/prompt.py
|
|
28
30
|
zstdlib/py.typed
|
|
29
31
|
zstdlib/signal_cm.py
|
|
30
32
|
zstdlib/singleton.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|