-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompleter.py
More file actions
134 lines (112 loc) · 4.84 KB
/
completer.py
File metadata and controls
134 lines (112 loc) · 4.84 KB
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
"""Command Completion for pymodbus REPL."""
from prompt_toolkit.application.current import get_app
# pylint: disable=missing-type-doc
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.filters import Condition
from pymodbus_repl.lib.helper import Command, get_commands
@Condition
def has_selected_completion():
"""Check for selected completion."""
complete_state = get_app().current_buffer.complete_state
return complete_state is not None and complete_state.current_completion is not None
class CmdCompleter(Completer):
"""Completer for Pymodbus REPL."""
def __init__(self, client=None, commands=None, ignore_case=True):
"""Initialize.
:param client: Modbus Client
:param commands: Commands to be added for Completion (list)
:param ignore_case: Ignore Case while looking up for commands
"""
self._commands = commands or get_commands(client)
self._commands["help"] = ""
self._command_names = self._commands.keys()
self.ignore_case = ignore_case
@property
def commands(self):
"""Return commands."""
return self._commands
@property
def command_names(self):
"""Return command names."""
return self._commands.keys()
def completing_command(self, words, word_before_cursor):
"""Determine if we are dealing with supported command.
:param words: Input text broken in to word tokens.
:param word_before_cursor: The current word before the cursor, \
which might be one or more blank spaces.
:return:
"""
return len(words) == 1 and len(word_before_cursor)
def completing_arg(self, words, word_before_cursor):
"""Determine if we are currently completing an argument.
:param words: The input text broken into word tokens.
:param word_before_cursor: The current word before the cursor, \
which might be one or more blank spaces.
:return: Specifies whether we are currently completing an arg.
"""
return len(words) > 1 and len(word_before_cursor)
def arg_completions(self, words, _word_before_cursor):
"""Generate arguments completions based on the input."""
cmd = words[0].strip()
cmd = self._commands.get(cmd, None)
return cmd if cmd else None
def _get_completions(self, word, word_before_cursor):
"""Get completions."""
if self.ignore_case:
word_before_cursor = word_before_cursor.lower()
return self.word_matches(word, word_before_cursor)
def word_matches(self, word, word_before_cursor):
"""Match the word and word before cursor.
:param word: The input text broken into word tokens.
:param word_before_cursor: The current word before the cursor, \
which might be one or more blank spaces.
:return: True if matched.
"""
if self.ignore_case:
word = word.lower()
return word.startswith(word_before_cursor)
def get_completions(self, document, complete_event):
"""Get completions for the current scope.
:param document: An instance of `prompt_toolkit.Document`.
:param complete_event: (Unused).
:return: Yields an instance of `prompt_toolkit.completion.Completion`.
"""
word_before_cursor = document.get_word_before_cursor(WORD=True)
text = document.text_before_cursor.lstrip()
words = document.text.strip().split()
meta = None
commands = []
if not words:
# yield commands
pass
if self.completing_command(words, word_before_cursor):
commands = self._command_names
c_meta = {
k: v.help_text if isinstance(v, Command) else v
for k, v in self._commands.items()
}
meta = lambda x: ( # pylint: disable=unnecessary-lambda-assignment
x,
c_meta.get(x, ""),
)
else:
if not list(
filter(lambda cmd: any(x == cmd for x in words), self._command_names)
):
# yield commands
pass
if " " in text:
command = self.arg_completions(words, word_before_cursor)
if not command:
return
commands = list(command.get_completion())
commands = list(
filter(lambda cmd: not (any(cmd in x for x in words)), commands)
)
meta = command.get_meta
for command in commands:
if self._get_completions(command, word_before_cursor):
_, display_meta = meta(command) if meta else ("", "")
yield Completion(
command, -len(word_before_cursor), display_meta=display_meta
)