zstdlib 0.10.5__tar.gz → 0.10.7__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.
Files changed (42) hide show
  1. {zstdlib-0.10.5/zstdlib.egg-info → zstdlib-0.10.7}/PKG-INFO +1 -1
  2. zstdlib-0.10.7/tests/test_prompt.py +123 -0
  3. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/__init__.py +5 -1
  4. zstdlib-0.10.7/zstdlib/prompt.py +98 -0
  5. {zstdlib-0.10.5 → zstdlib-0.10.7/zstdlib.egg-info}/PKG-INFO +1 -1
  6. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib.egg-info/SOURCES.txt +2 -0
  7. {zstdlib-0.10.5 → zstdlib-0.10.7}/LICENSE +0 -0
  8. {zstdlib-0.10.5 → zstdlib-0.10.7}/README.md +0 -0
  9. {zstdlib-0.10.5 → zstdlib-0.10.7}/pyproject.toml +0 -0
  10. {zstdlib-0.10.5 → zstdlib-0.10.7}/setup.cfg +0 -0
  11. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/__init__.py +0 -0
  12. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/log/__init__.py +0 -0
  13. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/log/base.py +0 -0
  14. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/log/test_cute.py +0 -0
  15. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/log/test_trace.py +0 -0
  16. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_ansi.py +0 -0
  17. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_enum.py +0 -0
  18. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_exc_capture.py +0 -0
  19. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_frozen.py +0 -0
  20. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_hybridmethod.py +0 -0
  21. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_io.py +0 -0
  22. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_lazy.py +0 -0
  23. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_not_set.py +0 -0
  24. {zstdlib-0.10.5 → zstdlib-0.10.7}/tests/test_singleton.py +0 -0
  25. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/ansi.py +0 -0
  26. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/enum.py +0 -0
  27. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/exc_capture.py +0 -0
  28. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/frozen.py +0 -0
  29. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/hybridmethod.py +0 -0
  30. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/io.py +0 -0
  31. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/lazy.py +0 -0
  32. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/log/__init__.py +0 -0
  33. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/log/cute.py +0 -0
  34. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/log/trace.py +0 -0
  35. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/macos.py +0 -0
  36. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/not_set.py +0 -0
  37. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/py.typed +0 -0
  38. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/signal_cm.py +0 -0
  39. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib/singleton.py +0 -0
  40. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib.egg-info/dependency_links.txt +0 -0
  41. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib.egg-info/requires.txt +0 -0
  42. {zstdlib-0.10.5 → zstdlib-0.10.7}/zstdlib.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zstdlib
3
- Version: 0.10.5
3
+ Version: 0.10.7
4
4
  Summary: A set of useful python utilities
5
5
  License: GPLv3
6
6
  Project-URL: Homepage, https://github.com/zwimer/zstdlib
@@ -0,0 +1,123 @@
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
+ mock_input = lambda *x: patch("builtins.input", side_effect=x)
8
+ mock_print = lambda: patch("builtins.print")
9
+
10
+
11
+ def want_have(mock_p: MagicMock, msg: str, n: int) -> tuple[list[str], list[str]]:
12
+ if any(len(i.args) != 1 or i.kwargs for i in mock_p.call_args_list):
13
+ raise ValueError("Unexpected print output")
14
+ return [i.args[0] for i in mock_p.call_args_list], [msg] * n
15
+
16
+
17
+ class TestPromptUntil(unittest.TestCase):
18
+ msg = "print_me"
19
+
20
+ def test_str(self) -> None:
21
+ with mock_print() as stdout, mock_input("test", "test", "A") as stdout:
22
+ self.assertEqual(prompt_until(self.msg, until="a"), "a")
23
+ self.assertEqual(*want_have(stdout, self.msg, 3))
24
+
25
+ def test_set(self) -> None:
26
+ with mock_print() as stdout, mock_input("test", "ab", "A") as stdout:
27
+ self.assertEqual(prompt_until(self.msg, until=("b", "a")), "a")
28
+ self.assertEqual(*want_have(stdout, self.msg, 3))
29
+
30
+ def test_case(self) -> None:
31
+ with mock_print() as stdout, mock_input("test", "test", "a", "A") as stdout:
32
+ self.assertEqual(prompt_until(self.msg, until="A", ignore_case=False), "A")
33
+ self.assertEqual(*want_have(stdout, self.msg, 4))
34
+
35
+ def test_eof(self) -> None:
36
+ with mock_print() as stdout, mock_input("test", EOFError) as stdout:
37
+ with self.assertRaises(EOFError):
38
+ prompt_until(self.msg, until="A")
39
+ self.assertEqual(*want_have(stdout, self.msg, 2))
40
+ with mock_print() as stdout, mock_input("test", EOFError) as stdout:
41
+ self.assertIs(prompt_until(self.msg, until="A", eof=True), None)
42
+ self.assertEqual(*want_have(stdout, self.msg, 2))
43
+
44
+ def test_ctrlc(self) -> None:
45
+ with mock_print() as stdout, mock_input("test", KeyboardInterrupt) as stdout:
46
+ with self.assertRaises(KeyboardInterrupt):
47
+ prompt_until(self.msg, until="A")
48
+ self.assertEqual(*want_have(stdout, self.msg, 2))
49
+ with mock_print() as stdout, mock_input("test", KeyboardInterrupt) as stdout:
50
+ self.assertIs(prompt_until(self.msg, until="A", ctrl_c=True), None)
51
+ self.assertEqual(*want_have(stdout, self.msg, 2))
52
+
53
+
54
+ class TestPromptFor(unittest.TestCase):
55
+ out = "Continue? [continue] "
56
+
57
+ def test_basic(self) -> None:
58
+ with mock_print() as stdout, mock_input("test", "test", "cOntinue") as stdout:
59
+ self.assertTrue(prompt_for())
60
+ self.assertEqual(*want_have(stdout, self.out, 3))
61
+
62
+ def test_eof(self) -> None:
63
+ with mock_print() as stdout, mock_input("test", EOFError) as stdout:
64
+ with self.assertRaises(EOFError):
65
+ prompt_for()
66
+ self.assertEqual(*want_have(stdout, self.out, 2))
67
+ with mock_print() as stdout, mock_input("test", EOFError) as stdout:
68
+ self.assertFalse(prompt_for(eof=True))
69
+ self.assertEqual(*want_have(stdout, self.out, 2))
70
+
71
+
72
+ class TestYNPrompt(unittest.TestCase):
73
+ out = "Continue? [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_not_short(self) -> None:
84
+ out = "Continue? [yes/no] "
85
+ with mock_print() as stdout, mock_input("test", "y", "yes") as stdout:
86
+ self.assertTrue(yn_prompt(short=False))
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(short=False))
90
+ self.assertEqual(*want_have(stdout, out, 3))
91
+
92
+ def test_empty(self) -> None:
93
+ with mock_print() as stdout, mock_input("test", "") as stdout:
94
+ with self.assertRaises(StopIteration):
95
+ yn_prompt()
96
+ self.assertEqual(*want_have(stdout, self.out, 3))
97
+ with mock_print() as stdout, mock_input("test", "") as stdout:
98
+ self.assertFalse(yn_prompt(empty_ok=True))
99
+ self.assertEqual(*want_have(stdout, self.out, 2))
100
+
101
+ def test_empty_not_short(self) -> None:
102
+ out = "Continue? [yes/no] "
103
+ with mock_print() as stdout, mock_input("test", "") as stdout:
104
+ with self.assertRaises(StopIteration):
105
+ yn_prompt(short=False)
106
+ self.assertEqual(*want_have(stdout, out, 3))
107
+ with mock_print() as stdout, mock_input("test", "") as stdout:
108
+ with self.assertRaises(StopIteration):
109
+ yn_prompt(short=False, empty_ok=True)
110
+ self.assertEqual(*want_have(stdout, out, 3))
111
+
112
+ def test_eof(self) -> None:
113
+ with mock_print() as stdout, mock_input("test", EOFError) as stdout:
114
+ with self.assertRaises(EOFError):
115
+ yn_prompt()
116
+ self.assertEqual(*want_have(stdout, self.out, 2))
117
+ with mock_print() as stdout, mock_input("test", EOFError) as stdout:
118
+ self.assertFalse(yn_prompt(eof=True))
119
+ self.assertEqual(*want_have(stdout, self.out, 2))
120
+
121
+
122
+ if __name__ == "__main__":
123
+ unittest.main()
@@ -1,5 +1,6 @@
1
- __version__ = "0.10.5"
1
+ __version__ = "0.10.7"
2
2
 
3
+ from .prompt import prompt_until, prompt_for, yn_prompt
3
4
  from .exc_capture import ExcCapture, ExcDelay
4
5
  from .hybridmethod import hybridmethod
5
6
  from .not_set import NotSetType, NotSet
@@ -24,5 +25,8 @@ __all__ = (
24
25
  "lazy",
25
26
  "log",
26
27
  "macos",
28
+ "prompt_for",
29
+ "prompt_until",
27
30
  "signal_cm",
31
+ "yn_prompt",
28
32
  )
@@ -0,0 +1,98 @@
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
+
8
+ @overload
9
+ def prompt_until(
10
+ msg: str,
11
+ until: str | Iterable[str],
12
+ *,
13
+ ignore_case: bool = True,
14
+ eof: Literal[False] = False,
15
+ ctrl_c: Literal[False] = False,
16
+ ) -> str: ...
17
+
18
+
19
+ @overload
20
+ def prompt_until(
21
+ msg: str,
22
+ until: str | Iterable[str],
23
+ *,
24
+ ignore_case: bool = True,
25
+ eof: bool = False,
26
+ ctrl_c: bool = False,
27
+ ) -> str | None: ...
28
+
29
+
30
+ def prompt_until(
31
+ msg: str,
32
+ until: str | Iterable[str],
33
+ *,
34
+ ignore_case: bool = True,
35
+ eof: bool = False,
36
+ ctrl_c: bool = False,
37
+ ) -> str | None:
38
+ """
39
+ Prompt the user with msg until they respone with a string in until, ignore case as needed
40
+ Whitespace is stripped
41
+ If ctrl_c and KeyboardInterrupt is caught, return None
42
+ If eof EOFError is caught, return None
43
+ Return the case-adjusted string the user entered
44
+ """
45
+ cased = lambda x: (x.casefold() if ignore_case else x)
46
+ until = frozenset(map(cased, (until,) if isinstance(until, str) else until))
47
+ try:
48
+ while (ret := cased(input(msg).strip())) not in until:
49
+ pass
50
+ return ret
51
+ except (EOFError, KeyboardInterrupt) as e:
52
+ if eof if isinstance(e, EOFError) else ctrl_c:
53
+ return None
54
+ raise
55
+
56
+
57
+ def prompt_for(
58
+ *,
59
+ msg: str = "Continue?",
60
+ key: str = "continue",
61
+ ignore_case: bool = True,
62
+ eof: bool = False,
63
+ ctrl_c: bool = False,
64
+ ) -> bool:
65
+ """
66
+ Prompt the user with msg to contine by entering key, whitespace is stripped
67
+ If eof/ctrl_c, interpret EOFError/KeyboardInterrupt as commands to return False
68
+ Returns True otherwise
69
+ """
70
+ ret = prompt_until(
71
+ f"{msg} [{quote(key)}] ",
72
+ ignore_case=ignore_case,
73
+ until={key},
74
+ eof=eof,
75
+ ctrl_c=ctrl_c,
76
+ )
77
+ return isinstance(ret, str)
78
+
79
+
80
+ def yn_prompt(
81
+ msg: str = "Continue?",
82
+ *,
83
+ empty_ok: bool = False,
84
+ eof: bool = False,
85
+ ctrl_c: bool = False,
86
+ short: bool = True,
87
+ ) -> bool:
88
+ """
89
+ Prompt the user to select from [yn] if short else [yes/no], whitespace is stripped
90
+ If eof/ctrl_c, interpret EOFError/KeyboardInterrupt as commands to return False
91
+ If empty_ok, "" is considered the same as "n"; empty_ok is a nop when not short
92
+ """
93
+ return prompt_until(
94
+ f"{msg} [{"yn" if short else "yes/no"}] ",
95
+ until=(("y", "n") + (("",) if empty_ok else ())) if short else ("yes", "no"),
96
+ eof=eof,
97
+ ctrl_c=ctrl_c,
98
+ ) in ("y", "yes")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zstdlib
3
- Version: 0.10.5
3
+ Version: 0.10.7
4
4
  Summary: A set of useful python utilities
5
5
  License: GPLv3
6
6
  Project-URL: Homepage, https://github.com/zwimer/zstdlib
@@ -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