Skip to content

Commit

Permalink
Merge pull request #6 from sharbov/fix_completion_issue
Browse files Browse the repository at this point in the history
Fix some completion issues
  • Loading branch information
sharbov authored Apr 27, 2019
2 parents 99c6e21 + 0d4f3eb commit ea4460f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
1 change: 1 addition & 0 deletions open_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def run_loop(self):

except Exception as exc:
self.logger.error(exc)
print(exc)

def execute(self, command):
"""Parse and execute the given command."""
Expand Down
75 changes: 56 additions & 19 deletions open_cli/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ def _text_to_completions(self, text):
except ValueError:
words = text.split(" ")

operation, text = self._extract_operation(words=words)
operation, remaining_text = self._extract_operation(words=words)

if callable(operation):
return self._get_operation_params_completions(operation=operation, text=text)

return self._get_completion(text=text, options=dir(operation))
return self._get_operation_params_completions(
original_text=text,
remaining_text=remaining_text,
operation=operation,
)

return self._get_completion(
original_text=text,
remaining_text=remaining_text,
options=dir(operation)
)

def _extract_operation(self, words):
"""Get the required client operation and separate it from the remaining text."""
Expand All @@ -43,26 +51,41 @@ def _extract_operation(self, words):

return operation, ""

def _get_operation_params_completions(self, operation, text):
def _get_operation_params_completions(self, original_text, remaining_text, operation):
"""Get suggestions based on operation and remaining text."""
completion_offset = 0

# Strip argument prefix
if text.startswith("--"):
text = text[2:]
completion_offset = 2
if remaining_text.startswith("--"):

if len(remaining_text.split("=")) == 2:
# Already a valid param
remaining_text = ""

else:
remaining_text = remaining_text[2:]
completion_offset = 2

# Handel definition type argument completions
if "." in text:
return self._get_definition_completions(operation, text)
if "." in remaining_text:
return self._get_definition_completions(
original_text=original_text,
remaining_text=remaining_text,
operation=operation
)

if self.should_hide_completions(original_text=original_text,
remaining_text=remaining_text,
allowed_suffixes=(" ", "-")):
return []

return [("--" + attribute, len(text) + completion_offset)
return [("--" + attribute, len(remaining_text) + completion_offset)
for attribute in operation.operation.params
if attribute.startswith(text) and not attribute.startswith("_")]
if attribute.startswith(remaining_text) and not attribute.startswith("_")]

def _get_definition_completions(self, operation, text):
def _get_definition_completions(self, original_text, remaining_text, operation):
"""Get suggestions based on definition and remaining text."""
param_words = text.split(".")
param_words = remaining_text.split(".")

# Only two words parameter completion are supported
if len(param_words) != 2:
Expand All @@ -87,10 +110,24 @@ def _get_definition_completions(self, operation, text):
if not definition:
return []

return self._get_completion(text=sub_name, options=dir(definition()))
return self._get_completion(
original_text=original_text,
remaining_text=sub_name,
options=dir(definition())
)

@staticmethod
def _get_completion(text, options):
def _get_completion(self, original_text, remaining_text, options):
"""Get completion properties based on text and possible options."""
return [(option, len(text)) for option in options
if option.startswith(text) and not option.startswith("_")]
if self.should_hide_completions(original_text=original_text,
remaining_text=remaining_text,
allowed_suffixes=(" ", ".")):
return []

return [(option, len(remaining_text)) for option in options
if option.startswith(remaining_text) and not option.startswith("_")]

@staticmethod
def should_hide_completions(original_text, remaining_text, allowed_suffixes):
return (original_text and
not remaining_text and
original_text[-1] not in allowed_suffixes)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fire==0.1.3
bravado==9.2.2
bravado==10.3.2
tabulate==0.8.2
prompt-toolkit==1.0.15

0 comments on commit ea4460f

Please sign in to comment.