File size: 17,847 Bytes
b93364a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
"""Nested completer for completion of OpenBB hierarchical data structures."""
from typing import (
Any,
Callable,
Dict,
Iterable,
List,
Mapping,
Optional,
Pattern,
Set,
Union,
)
from prompt_toolkit.completion import CompleteEvent, Completer, Completion
from prompt_toolkit.document import Document
from prompt_toolkit.formatted_text import AnyFormattedText
from prompt_toolkit.history import FileHistory
NestedDict = Mapping[str, Union[Any, Set[str], None, Completer]]
# pylint: disable=too-many-arguments,global-statement,too-many-branches,global-variable-not-assigned
class WordCompleter(Completer):
"""Simple autocompletion on a list of words.
:param words: List of words or callable that returns a list of words.
:param ignore_case: If True, case-insensitive completion.
:param meta_dict: Optional dict mapping words to their meta-text. (This
should map strings to strings or formatted text.)
:param WORD: When True, use WORD characters.
:param sentence: When True, don't complete by comparing the word before the
cursor, but by comparing all the text before the cursor. In this case,
the list of words is just a list of strings, where each string can
contain spaces. (Can not be used together with the WORD option.)
:param match_middle: When True, match not only the start, but also in the
middle of the word.
:param pattern: Optional compiled regex for finding the word before
the cursor to complete. When given, use this regex pattern instead of
default one (see document._FIND_WORD_RE)
"""
def __init__(
self,
words: Union[List[str], Callable[[], List[str]]],
ignore_case: bool = False,
display_dict: Optional[Mapping[str, AnyFormattedText]] = None,
meta_dict: Optional[Mapping[str, AnyFormattedText]] = None,
WORD: bool = True,
sentence: bool = False,
match_middle: bool = False,
pattern: Optional[Pattern[str]] = None,
) -> None:
"""Initialize the WordCompleter."""
assert not (WORD and sentence) # noqa: S101
self.words = words
self.ignore_case = ignore_case
self.display_dict = display_dict or {}
self.meta_dict = meta_dict or {}
self.WORD = WORD
self.sentence = sentence
self.match_middle = match_middle
self.pattern = pattern
def get_completions(
self,
document: Document,
_complete_event: CompleteEvent,
) -> Iterable[Completion]:
"""Get completions."""
# Get list of words.
words = self.words
if callable(words):
words = words()
# Get word/text before cursor.
if self.sentence:
word_before_cursor = document.text_before_cursor
else:
word_before_cursor = document.get_word_before_cursor(
WORD=self.WORD, pattern=self.pattern
)
if (
"--" in document.text_before_cursor
and document.text_before_cursor.rfind(" --")
>= document.text_before_cursor.rfind(" -")
):
word_before_cursor = f'--{document.text_before_cursor.split("--")[-1]}'
elif f"--{word_before_cursor}" == document.text_before_cursor:
word_before_cursor = document.text_before_cursor
if self.ignore_case:
word_before_cursor = word_before_cursor.lower()
def word_matches(word: str) -> bool:
"""Set True when the word before the cursor matches."""
if self.ignore_case:
word = word.lower()
if self.match_middle:
return word_before_cursor in word
return word.startswith(word_before_cursor)
for a in words:
if word_matches(a):
display = self.display_dict.get(a, a)
display_meta = self.meta_dict.get(a, "")
yield Completion(
text=a,
start_position=-len(word_before_cursor),
display=display,
display_meta=display_meta,
)
class NestedCompleter(Completer):
"""Completer which wraps around several other completers, and calls any the
one that corresponds with the first word of the input.
By combining multiple `NestedCompleter` instances, we can achieve multiple
hierarchical levels of autocompletion. This is useful when `WordCompleter`
is not sufficient.
If you need multiple levels, check out the `from_nested_dict` classmethod.
"""
complementary: List = list()
def __init__(
self, options: Dict[str, Optional[Completer]], ignore_case: bool = True
) -> None:
"""Initialize the NestedCompleter."""
self.flags_processed: List = list()
self.original_options = options
self.options = options
self.ignore_case = ignore_case
self.complementary = list()
def __repr__(self) -> str:
"""Return string representation of NestedCompleter."""
return f"NestedCompleter({self.options!r}, ignore_case={self.ignore_case!r})"
@classmethod
def from_nested_dict(cls, data: dict) -> "NestedCompleter":
"""Create a `NestedCompleter`.
It starts from a nested dictionary data structure, like this:
.. code::
data = {
'show': {
'version': None,
'interfaces': None,
'clock': None,
'ip': {'interface': {'brief'}}
},
'exit': None
'enable': None
}
The value should be `None` if there is no further completion at some
point. If all values in the dictionary are None, it is also possible to
use a set instead.
Values in this data structure can be a completers as well.
"""
options: Dict[str, Any] = {}
for key, value in data.items():
if isinstance(value, Completer):
options[key] = value
elif isinstance(value, dict):
options[key] = cls.from_nested_dict(value)
elif isinstance(value, set):
options[key] = cls.from_nested_dict({item: None for item in value})
elif isinstance(key, str) and isinstance(value, str):
options[key] = options[value]
else:
assert value is None # noqa: S101
options[key] = None
for items in cls.complementary:
if items[0] in options:
options[items[1]] = options[items[0]]
elif items[1] in options:
options[items[0]] = options[items[1]]
return cls(options)
def get_completions( # noqa: PLR0912
self, document: Document, complete_event: CompleteEvent
) -> Iterable[Completion]:
"""Get completions."""
# Split document.
cmd = ""
text = document.text_before_cursor.lstrip()
if " " in text:
cmd = text.split(" ")[0]
if "-" in text:
if text.rfind("--") == -1 or text.rfind("-") - 1 > text.rfind("--"):
unprocessed_text = "-" + text.split("-")[-1]
else:
unprocessed_text = "--" + text.split("--")[-1]
else:
unprocessed_text = text
stripped_len = len(document.text_before_cursor) - len(text)
# Check if there are multiple flags for the same command
if self.complementary:
for same_flags in self.complementary:
if (
same_flags[0] in self.flags_processed
and same_flags[1] not in self.flags_processed
) or (
same_flags[1] in self.flags_processed
and same_flags[0] not in self.flags_processed
):
if same_flags[0] in self.flags_processed:
self.flags_processed.append(same_flags[1])
elif same_flags[1] in self.flags_processed:
self.flags_processed.append(same_flags[0])
if cmd:
self.options = {
k: self.original_options.get(cmd).options[k] # type: ignore
for k in self.original_options.get(cmd).options # type: ignore
if k not in self.flags_processed
}
else:
self.options = {
k: self.original_options[k]
for k in self.original_options
if k not in self.flags_processed
}
# If there is a space, check for the first term, and use a subcompleter.
if " " in unprocessed_text:
first_term = unprocessed_text.split()[0]
# user is updating one of the values
if unprocessed_text[-1] != " ":
self.flags_processed = [
flag for flag in self.flags_processed if flag != first_term
]
if self.complementary:
for same_flags in self.complementary:
if (
same_flags[0] in self.flags_processed
and same_flags[1] not in self.flags_processed
) or (
same_flags[1] in self.flags_processed
and same_flags[0] not in self.flags_processed
):
if same_flags[0] in self.flags_processed:
self.flags_processed.remove(same_flags[0])
elif same_flags[1] in self.flags_processed:
self.flags_processed.remove(same_flags[1])
if cmd and self.original_options.get(cmd):
self.options = self.original_options
else:
self.options = {
k: self.original_options[k]
for k in self.original_options
if k not in self.flags_processed
}
if "-" not in text:
completer = self.options.get(first_term)
elif cmd in self.options and self.options.get(cmd):
completer = self.options.get(cmd).options.get(first_term) # type: ignore
else:
completer = self.options.get(first_term)
# If we have a sub completer, use this for the completions.
if completer is not None:
remaining_text = unprocessed_text[len(first_term) :].lstrip()
move_cursor = len(text) - len(remaining_text) + stripped_len
new_document = Document(
remaining_text,
cursor_position=document.cursor_position - move_cursor,
)
# Provides auto-completion but if user doesn't take it still keep going
if " " in new_document.text:
if (
new_document.text in [f"{opt} " for opt in self.options]
or unprocessed_text[-1] == " "
):
self.flags_processed.append(first_term)
if cmd:
self.options = {
k: self.original_options.get(cmd).options[k] # type: ignore
for k in self.original_options.get(cmd).options # type: ignore
if k not in self.flags_processed
}
else:
self.options = {
k: self.original_options[k]
for k in self.original_options
if k not in self.flags_processed
}
# In case the users inputs a single boolean flag
elif not completer.options: # type: ignore
self.flags_processed.append(first_term)
if self.complementary:
for same_flags in self.complementary:
if (
same_flags[0] in self.flags_processed
and same_flags[1] not in self.flags_processed
) or (
same_flags[1] in self.flags_processed
and same_flags[0] not in self.flags_processed
):
if same_flags[0] in self.flags_processed:
self.flags_processed.append(same_flags[1])
elif same_flags[1] in self.flags_processed:
self.flags_processed.append(same_flags[0])
if cmd:
self.options = {
k: self.original_options.get(cmd).options[k] # type: ignore
for k in self.original_options.get(cmd).options # type: ignore
if k not in self.flags_processed
}
else:
self.options = {
k: self.original_options[k]
for k in self.original_options
if k not in self.flags_processed
}
else:
# This is a NestedCompleter
yield from completer.get_completions(new_document, complete_event)
# No space in the input: behave exactly like `WordCompleter`.
else:
# check if the prompt has been updated in the meantime
if " " in text or "-" in text:
actual_flags_processed = [
flag for flag in self.flags_processed if flag in text
]
if self.complementary:
for same_flags in self.complementary:
if (
same_flags[0] in actual_flags_processed
and same_flags[1] not in actual_flags_processed
) or (
same_flags[1] in actual_flags_processed
and same_flags[0] not in actual_flags_processed
):
if same_flags[0] in actual_flags_processed:
actual_flags_processed.append(same_flags[1])
elif same_flags[1] in actual_flags_processed:
actual_flags_processed.append(same_flags[0])
if len(actual_flags_processed) < len(self.flags_processed):
self.flags_processed = actual_flags_processed
if cmd:
self.options = {
k: self.original_options.get(cmd).options[k] # type: ignore
for k in self.original_options.get(cmd).options # type: ignore
if k not in self.flags_processed
}
else:
self.options = {
k: self.original_options[k]
for k in self.original_options
if k not in self.flags_processed
}
command = self.options.get(cmd)
options = command.options if command else {} # type: ignore
command_options = [f"{cmd} {opt}" for opt in options]
text_list = [text in val for val in command_options]
if cmd and cmd in self.options and text_list:
completer = WordCompleter(
list(self.options.get(cmd).options.keys()), # type: ignore
ignore_case=self.ignore_case,
)
elif bool([val for val in self.options if text in val]):
completer = WordCompleter(
list(self.options.keys()), ignore_case=self.ignore_case
)
else:
# The user has delete part of the first command and we need to reset options
if bool([val for val in self.original_options if text in val]):
self.options = self.original_options
self.flags_processed = list()
completer = WordCompleter(
list(self.options.keys()), ignore_case=self.ignore_case
)
# This is a WordCompleter
yield from completer.get_completions(document, complete_event)
class CustomFileHistory(FileHistory):
"""Filtered file history."""
def sanitize_input(self, string: str) -> str:
"""Sanitize sensitive information from the input string by parsing arguments."""
keywords = ["--password", "--email", "--pat"]
string_list = string.split(" ")
for kw in keywords:
if kw in string_list:
index = string_list.index(kw)
if len(string_list) > index + 1:
string_list[index + 1] = "********"
result = " ".join(string_list)
return result
def store_string(self, string: str) -> None:
"""Store string in history."""
string = self.sanitize_input(string)
super().store_string(string)
|