Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[crmsh-4.6] Dev: ui_configure: Complete required parameters first #1484

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crmsh/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import inspect
import re
import ctypes
import ctypes.util
from . import help as help_module
from . import ui_utils
from . import log
Expand Down Expand Up @@ -566,6 +568,10 @@
'''
ret = []
if self.completer is not None:
if sort_completion_inst is not None and sort_completion_inst.value == 0:

Check warning on line 571 in crmsh/command.py

View check run for this annotation

Codecov / codecov/patch

crmsh/command.py#L571

Added line #L571 was not covered by tests
# Restore the original value of rl_sort_completion_matches
# before calling the completer again
sort_completion_inst.value = orig_sort_completion_value

Check warning on line 574 in crmsh/command.py

View check run for this annotation

Codecov / codecov/patch

crmsh/command.py#L574

Added line #L574 was not covered by tests
specs = inspect.getfullargspec(self.completer)
if 'context' in specs.args:
ret = self.completer([self.name] + args, context)
Expand All @@ -591,3 +597,16 @@
if argnames != expected:
raise ValueError(fn.__name__ +
": Expected method with signature " + repr(expected))


readline_path = ctypes.util.find_library('readline')
sort_completion_inst = None
if readline_path:
# Inspired by
# https://stackoverflow.com/questions/31229708/python-gnu-readline-bindings-keep-my-sort-order
# Python readline module sort the completion result by
# alphabetical order by default. To archive the custom
# sort order, need to disable the sort_completion_matches
rl = ctypes.cdll.LoadLibrary(readline_path)
sort_completion_inst = ctypes.c_ulong.in_dll(rl, 'rl_sort_completion_matches')
orig_sort_completion_value = sort_completion_inst.value
14 changes: 6 additions & 8 deletions crmsh/ra.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,14 @@ def mk_ra_node(self):
self.broken_ra = False
return self.ra_elem

def params(self, completion=False):
def params(self):
'''
Construct a dict of dicts: parameters are keys and
dictionary of attributes/values are values. Cached too.

completion:
If true, filter some (advanced) parameters out.
'''
if completion:
if self.mk_ra_node() is None:
return None
return [c.get("name")
for c in self.ra_elem.xpath("//parameters/parameter")
if c.get("name") and c.get("name") not in self.excluded_from_completion]
ident = "ra_params-%s" % self
if cache.is_cached(ident):
return cache.retrieve(ident)
Expand All @@ -399,7 +393,7 @@ def params(self, completion=False):
d = {}
for c in self.ra_elem.xpath("//parameters/parameter"):
name = c.get("name")
if not name:
if not name or name in self.excluded_from_completion:
continue
required = c.get("required") if not (c.get("deprecated") or c.get("obsoletes")) else "0"
unique = c.get("unique")
Expand All @@ -410,6 +404,10 @@ def params(self, completion=False):
"type": typ,
"default": default,
}
items = list(d.items())
# Sort the dictionary by required and then alphabetically
items.sort(key=lambda item: (item[1]["required"] != '1', item[0]))
d = dict(items)
return cache.store(ident, d)

def actions(self):
Expand Down
5 changes: 4 additions & 1 deletion crmsh/ui_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@
return []
elif '=' in completing:
return []
return utils.filter_keys(agent.params(completion=True), args)
if command.sort_completion_inst is not None:

Check warning on line 234 in crmsh/ui_configure.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_configure.py#L234

Added line #L234 was not covered by tests
# Set the value to 0 to use the costum sort order
command.sort_completion_inst.value = 0
return utils.filter_keys(agent.params(), args)

Check warning on line 237 in crmsh/ui_configure.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_configure.py#L236-L237

Added lines #L236 - L237 were not covered by tests


def _prim_meta_completer(agent, args):
Expand Down
2 changes: 1 addition & 1 deletion crmsh/ui_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
ret = None
# logging.debug("line:%s, text:%s, ret:%s, state:%s", repr(line), repr(text), ret, state)
if not text or (ret and line.split()[-1].endswith(ret)):
if ret == "id=":
if ret.endswith('='):

Check warning on line 232 in crmsh/ui_context.py

View check run for this annotation

Codecov / codecov/patch

crmsh/ui_context.py#L232

Added line #L232 was not covered by tests
return ret
return ret + ' '
return ret
Expand Down
Loading