InquirerPrompt 0.3.0__py3-none-any.whl
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.
- InquirerPy/__init__.py +4 -0
- InquirerPy/base/__init__.py +24 -0
- InquirerPy/base/complex.py +295 -0
- InquirerPy/base/control.py +230 -0
- InquirerPy/base/list.py +239 -0
- InquirerPy/base/simple.py +379 -0
- InquirerPy/containers/__init__.py +3 -0
- InquirerPy/containers/instruction.py +38 -0
- InquirerPy/containers/message.py +42 -0
- InquirerPy/containers/spinner.py +109 -0
- InquirerPy/containers/validation.py +60 -0
- InquirerPy/enum.py +8 -0
- InquirerPy/exceptions.py +25 -0
- InquirerPy/inquirer.py +31 -0
- InquirerPy/prompts/__init__.py +25 -0
- InquirerPy/prompts/checkbox.py +249 -0
- InquirerPy/prompts/confirm.py +202 -0
- InquirerPy/prompts/expand.py +459 -0
- InquirerPy/prompts/filepath.py +193 -0
- InquirerPy/prompts/fuzzy.py +685 -0
- InquirerPy/prompts/input.py +256 -0
- InquirerPy/prompts/list.py +371 -0
- InquirerPy/prompts/number.py +621 -0
- InquirerPy/prompts/rawlist.py +286 -0
- InquirerPy/prompts/secret.py +137 -0
- InquirerPy/py.typed +1 -0
- InquirerPy/resolver.py +220 -0
- InquirerPy/separator.py +23 -0
- InquirerPy/utils.py +291 -0
- InquirerPy/validator.py +166 -0
- inquirerprompt-0.3.0.dist-info/METADATA +217 -0
- inquirerprompt-0.3.0.dist-info/RECORD +34 -0
- inquirerprompt-0.3.0.dist-info/WHEEL +4 -0
- inquirerprompt-0.3.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"""Module contains the class to create filepath prompt and filepath completer class."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Callable, Generator, Optional
|
|
6
|
+
|
|
7
|
+
from prompt_toolkit.completion import Completer, Completion
|
|
8
|
+
from prompt_toolkit.completion.base import ThreadedCompleter
|
|
9
|
+
|
|
10
|
+
from InquirerPy.prompts.input import InputPrompt
|
|
11
|
+
from InquirerPy.utils import (
|
|
12
|
+
InquirerPyDefault,
|
|
13
|
+
InquirerPyKeybindings,
|
|
14
|
+
InquirerPyMessage,
|
|
15
|
+
InquirerPySessionResult,
|
|
16
|
+
InquirerPyStyle,
|
|
17
|
+
InquirerPyValidate,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from prompt_toolkit.input.base import Input
|
|
22
|
+
from prompt_toolkit.output.base import Output
|
|
23
|
+
|
|
24
|
+
__all__ = ["FilePathPrompt", "FilePathCompleter"]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class FilePathCompleter(Completer):
|
|
28
|
+
"""An auto completion class which generates system filepath.
|
|
29
|
+
|
|
30
|
+
See Also:
|
|
31
|
+
:class:`~prompt_toolkit.completion.Completer`
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
only_directories: Only complete directories.
|
|
35
|
+
only_files: Only complete files.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, only_directories: bool = False, only_files: bool = False):
|
|
39
|
+
self._only_directories = only_directories
|
|
40
|
+
self._only_files = only_files
|
|
41
|
+
self._delimiter = "/" if os.name == "posix" else "\\"
|
|
42
|
+
|
|
43
|
+
def get_completions(
|
|
44
|
+
self, document, complete_event
|
|
45
|
+
) -> Generator[Completion, None, None]:
|
|
46
|
+
"""Get a list of valid system paths."""
|
|
47
|
+
if document.text == "~":
|
|
48
|
+
return
|
|
49
|
+
|
|
50
|
+
validation = lambda file, doc_text: str(file).startswith(doc_text)
|
|
51
|
+
|
|
52
|
+
if document.cursor_position == 0:
|
|
53
|
+
dirname = Path.cwd()
|
|
54
|
+
validation = lambda file, doc_text: True
|
|
55
|
+
elif document.text.startswith("~"):
|
|
56
|
+
dirname = Path(os.path.dirname(f"{Path.home()}{document.text[1:]}"))
|
|
57
|
+
validation = lambda file, doc_text: str(file).startswith(
|
|
58
|
+
f"{Path.home()}{doc_text[1:]}"
|
|
59
|
+
)
|
|
60
|
+
elif document.text.startswith(f".{self._delimiter}"):
|
|
61
|
+
dirname = Path(os.path.dirname(document.text))
|
|
62
|
+
validation = lambda file, doc_text: str(file).startswith(doc_text[2:])
|
|
63
|
+
else:
|
|
64
|
+
dirname = Path(os.path.dirname(document.text))
|
|
65
|
+
|
|
66
|
+
for item in self._get_completion(document, dirname, validation):
|
|
67
|
+
yield item
|
|
68
|
+
|
|
69
|
+
def _get_completion(
|
|
70
|
+
self, document, path, validation
|
|
71
|
+
) -> Generator[Completion, None, None]:
|
|
72
|
+
if not path.is_dir():
|
|
73
|
+
return
|
|
74
|
+
for file in path.iterdir():
|
|
75
|
+
if self._only_directories and not file.is_dir():
|
|
76
|
+
continue
|
|
77
|
+
if self._only_files and not file.is_file():
|
|
78
|
+
continue
|
|
79
|
+
if validation(file, document.text):
|
|
80
|
+
file_name = file.name
|
|
81
|
+
display_name = file_name
|
|
82
|
+
if file.is_dir():
|
|
83
|
+
display_name = f"{file_name}{self._delimiter}"
|
|
84
|
+
yield Completion(
|
|
85
|
+
file.name,
|
|
86
|
+
start_position=-1 * len(os.path.basename(document.text)),
|
|
87
|
+
display=display_name,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class FilePathPrompt(InputPrompt):
|
|
92
|
+
"""Create a prompt that provides auto completion for system filepaths.
|
|
93
|
+
|
|
94
|
+
A wrapper class around :class:`~prompt_toolkit.shortcuts.PromptSession`.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
message: The question to ask the user.
|
|
98
|
+
Refer to :ref:`pages/dynamic:message` documentation for more details.
|
|
99
|
+
style: An :class:`InquirerPyStyle` instance.
|
|
100
|
+
Refer to :ref:`Style <pages/style:Alternate Syntax>` documentation for more details.
|
|
101
|
+
vi_mode: Use vim keybinding for the prompt.
|
|
102
|
+
Refer to :ref:`pages/kb:Keybindings` documentation for more details.
|
|
103
|
+
default: Set the default text value of the prompt.
|
|
104
|
+
Refer to :ref:`pages/dynamic:default` documentation for more details.
|
|
105
|
+
qmark: Question mark symbol. Custom symbol that will be displayed infront of the question before its answered.
|
|
106
|
+
amark: Answer mark symbol. Custom symbol that will be displayed infront of the question after its answered.
|
|
107
|
+
instruction: Short instruction to display next to the question.
|
|
108
|
+
long_instruction: Long instructions to display at the bottom of the prompt.
|
|
109
|
+
multicolumn_complete: Change the auto-completion UI to a multi column display.
|
|
110
|
+
validate: Add validation to user input.
|
|
111
|
+
Refer to :ref:`pages/validator:Validator` documentation for more details.
|
|
112
|
+
invalid_message: Error message to display when user input is invalid.
|
|
113
|
+
Refer to :ref:`pages/validator:Validator` documentation for more details.
|
|
114
|
+
transformer: A function which performs additional transformation on the value that gets printed to the terminal.
|
|
115
|
+
Different than `filter` parameter, this is only visual effect and won’t affect the actual value returned by :meth:`~InquirerPy.base.simple.BaseSimplePrompt.execute`.
|
|
116
|
+
Refer to :ref:`pages/dynamic:transformer` documentation for more details.
|
|
117
|
+
filter: A function which performs additional transformation on the result.
|
|
118
|
+
This affects the actual value returned by :meth:`~InquirerPy.base.simple.BaseSimplePrompt.execute`.
|
|
119
|
+
Refer to :ref:`pages/dynamic:filter` documentation for more details.
|
|
120
|
+
keybindings: Customise the builtin keybindings.
|
|
121
|
+
Refer to :ref:`pages/kb:Keybindings` for more details.
|
|
122
|
+
wrap_lines: Soft wrap question lines when question exceeds the terminal width.
|
|
123
|
+
only_directories: Only complete directories.
|
|
124
|
+
only_files: Only complete files.
|
|
125
|
+
raise_keyboard_interrupt: Raise the :class:`KeyboardInterrupt` exception when `ctrl-c` is pressed. If false, the result
|
|
126
|
+
will be `None` and the question is skiped.
|
|
127
|
+
mandatory: Indicate if the prompt is mandatory. If True, then the question cannot be skipped.
|
|
128
|
+
mandatory_message: Error message to show when user attempts to skip mandatory prompt.
|
|
129
|
+
session_result: Used internally for :ref:`index:Classic Syntax (PyInquirer)`.
|
|
130
|
+
input: Used internally and will be removed in future updates.
|
|
131
|
+
output: Used internally and will be removed in future updates.
|
|
132
|
+
|
|
133
|
+
Examples:
|
|
134
|
+
>>> from InquirerPy import inquirer
|
|
135
|
+
>>> result = inquirer.filepath(message="Enter a path:").execute()
|
|
136
|
+
>>> print(result)
|
|
137
|
+
/home/ubuntu/README.md
|
|
138
|
+
"""
|
|
139
|
+
|
|
140
|
+
def __init__(
|
|
141
|
+
self,
|
|
142
|
+
message: InquirerPyMessage,
|
|
143
|
+
style: Optional[InquirerPyStyle] = None,
|
|
144
|
+
vi_mode: bool = False,
|
|
145
|
+
default: InquirerPyDefault = "",
|
|
146
|
+
qmark: str = "?",
|
|
147
|
+
amark: str = "?",
|
|
148
|
+
instruction: str = "",
|
|
149
|
+
long_instruction: str = "",
|
|
150
|
+
multicolumn_complete: bool = False,
|
|
151
|
+
validate: Optional[InquirerPyValidate] = None,
|
|
152
|
+
invalid_message: str = "Invalid input",
|
|
153
|
+
only_directories: bool = False,
|
|
154
|
+
only_files: bool = False,
|
|
155
|
+
transformer: Optional[Callable[[str], Any]] = None,
|
|
156
|
+
filter: Optional[Callable[[str], Any]] = None,
|
|
157
|
+
keybindings: Optional[InquirerPyKeybindings] = None,
|
|
158
|
+
wrap_lines: bool = True,
|
|
159
|
+
raise_keyboard_interrupt: bool = True,
|
|
160
|
+
mandatory: bool = True,
|
|
161
|
+
mandatory_message: str = "Mandatory prompt",
|
|
162
|
+
session_result: Optional[InquirerPySessionResult] = None,
|
|
163
|
+
input: Optional["Input"] = None,
|
|
164
|
+
output: Optional["Output"] = None,
|
|
165
|
+
) -> None:
|
|
166
|
+
super().__init__(
|
|
167
|
+
message=message,
|
|
168
|
+
style=style,
|
|
169
|
+
vi_mode=vi_mode,
|
|
170
|
+
default=default,
|
|
171
|
+
qmark=qmark,
|
|
172
|
+
amark=amark,
|
|
173
|
+
instruction=instruction,
|
|
174
|
+
long_instruction=long_instruction,
|
|
175
|
+
completer=ThreadedCompleter(
|
|
176
|
+
FilePathCompleter(
|
|
177
|
+
only_directories=only_directories, only_files=only_files
|
|
178
|
+
)
|
|
179
|
+
),
|
|
180
|
+
multicolumn_complete=multicolumn_complete,
|
|
181
|
+
validate=validate,
|
|
182
|
+
invalid_message=invalid_message,
|
|
183
|
+
transformer=transformer,
|
|
184
|
+
filter=filter,
|
|
185
|
+
keybindings=keybindings,
|
|
186
|
+
wrap_lines=wrap_lines,
|
|
187
|
+
raise_keyboard_interrupt=raise_keyboard_interrupt,
|
|
188
|
+
mandatory=mandatory,
|
|
189
|
+
mandatory_message=mandatory_message,
|
|
190
|
+
session_result=session_result,
|
|
191
|
+
input=input,
|
|
192
|
+
output=output,
|
|
193
|
+
)
|