override complete() to change the behaviour of Cmd completion
This commit is contained in:
parent
d0730472d8
commit
0eba4302a5
4 changed files with 34 additions and 17 deletions
|
|
@ -55,6 +55,9 @@ class Videoob(ReplApplication):
|
|||
self.format(video)
|
||||
self.flush()
|
||||
|
||||
def complete_nsfw(self, text, line, begidx, endidx):
|
||||
return ['on', 'off']
|
||||
|
||||
def do_nsfw(self, line):
|
||||
"""
|
||||
nsfw [on | off]
|
||||
|
|
|
|||
|
|
@ -213,7 +213,7 @@ class BaseApplication(object):
|
|||
version = '%s v%s' % (self.APPNAME, self.VERSION)
|
||||
return version
|
||||
|
||||
def _complete_obj(self, backend, fields, obj):
|
||||
def _do_complete_obj(self, backend, fields, obj):
|
||||
if fields:
|
||||
try:
|
||||
backend.fillobj(obj, fields)
|
||||
|
|
@ -224,14 +224,14 @@ class BaseApplication(object):
|
|||
setattr(obj, field, NotAvailable)
|
||||
return obj
|
||||
|
||||
def _complete_iter(self, backend, count, fields, res):
|
||||
def _do_complete_iter(self, backend, count, fields, res):
|
||||
for i, sub in enumerate(res):
|
||||
if count and i == count:
|
||||
break
|
||||
sub = self._complete_obj(backend, fields, sub)
|
||||
sub = self._do_complete_obj(backend, fields, sub)
|
||||
yield sub
|
||||
|
||||
def _complete(self, backend, count, selected_fields, function, *args, **kwargs):
|
||||
def _do_complete(self, backend, count, selected_fields, function, *args, **kwargs):
|
||||
assert count is None or count > 0
|
||||
if callable(function):
|
||||
res = function(backend, *args, **kwargs)
|
||||
|
|
@ -244,9 +244,9 @@ class BaseApplication(object):
|
|||
fields = None
|
||||
|
||||
if hasattr(res, '__iter__'):
|
||||
return self._complete_iter(backend, count, fields, res)
|
||||
return self._do_complete_iter(backend, count, fields, res)
|
||||
else:
|
||||
return self._complete_obj(backend, fields, res)
|
||||
return self._do_complete_obj(backend, fields, res)
|
||||
|
||||
@classmethod
|
||||
def run(klass, args=None):
|
||||
|
|
|
|||
|
|
@ -322,4 +322,4 @@ class ConsoleApplication(BaseApplication):
|
|||
Call Weboob.do(), after having filled the yielded object, if selected fields are given by user.
|
||||
"""
|
||||
|
||||
return self.weboob.do(self._complete, self.options.count, self.selected_fields, function, *args, **kwargs)
|
||||
return self.weboob.do(self._do_complete, self.options.count, self.selected_fields, function, *args, **kwargs)
|
||||
|
|
|
|||
|
|
@ -187,7 +187,7 @@ class ReplApplication(Cmd, BaseApplication):
|
|||
# PLEASE REVIEW THIS CODE.
|
||||
#fields = [k for k, v in iter_fields(obj)]
|
||||
fields = None
|
||||
return self.weboob.do(self._complete, self.options.count, fields, function, *args, **kwargs)
|
||||
return self.weboob.do(self._do_complete, self.options.count, fields, function, *args, **kwargs)
|
||||
|
||||
# options related methods
|
||||
|
||||
|
|
@ -299,13 +299,28 @@ class ReplApplication(Cmd, BaseApplication):
|
|||
print 'Unknown command: "%s"' % line
|
||||
|
||||
def completenames(self, text, *ignored):
|
||||
return ['%s ' % name for name in Cmd.completenames(self, text, *ignored) if name not in self.hidden_commands]
|
||||
return [name for name in Cmd.completenames(self, text, *ignored) if name not in self.hidden_commands]
|
||||
|
||||
def completion_helper(self, text, choices):
|
||||
if text:
|
||||
return [x for x in choices if x.startswith(text)]
|
||||
else:
|
||||
return choices
|
||||
def complete(self, text, state):
|
||||
"""
|
||||
Override of the Cmd.complete() method to:
|
||||
- add a space at end of proposals
|
||||
- display only proposals for words which match the
|
||||
text already written by user.
|
||||
"""
|
||||
super(ReplApplication, self).complete(text, state)
|
||||
|
||||
# When state = 0, Cmd.complete() set the 'completion_matches' attribute by
|
||||
# calling the completion function. Then, for other states, it only try to
|
||||
# get the right item in list.
|
||||
# So that's the good place to rework the choices.
|
||||
if state == 0:
|
||||
self.completion_matches = [choice for choice in self.completion_matches if choice.startswith(text)]
|
||||
|
||||
try:
|
||||
return '%s ' % self.completion_matches[state]
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
def do_backends(self, line):
|
||||
"""
|
||||
|
|
@ -373,7 +388,7 @@ class ReplApplication(Cmd, BaseApplication):
|
|||
return False
|
||||
|
||||
def complete_backends(self, text, line, begidx, endidx):
|
||||
choices = None
|
||||
choices = []
|
||||
commands = ['enable', 'disable', 'only', 'list']
|
||||
available_backends_names = set(backend.name for backend in self.weboob.iter_backends())
|
||||
enabled_backends_names = set(backend.name for backend in self.enabled_backends)
|
||||
|
|
@ -391,8 +406,7 @@ class ReplApplication(Cmd, BaseApplication):
|
|||
elif args[1] == 'disable':
|
||||
choices = sorted(enabled_backends_names)
|
||||
|
||||
if choices is not None:
|
||||
return ['%s ' % choice for choice in choices if choice.startswith(text)] if text else choices
|
||||
return choices
|
||||
|
||||
def do_condition(self, line):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue