[Solo] move common "hierarchical" bashcomp code to SoloCommand
This commit is contained in:
@@ -10,10 +10,11 @@
|
||||
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
from entropy.i18n import _
|
||||
from entropy.output import darkgreen, print_error
|
||||
from entropy.output import darkgreen, teal, purple, print_error
|
||||
from entropy.exceptions import PermissionDenied
|
||||
from entropy.client.interfaces import Client
|
||||
from entropy.core.settings.base import SystemSettings
|
||||
@@ -100,6 +101,44 @@ class SoloCommand(object):
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def _hierarchical_bashcomp(self, last_arg, outcome, commands):
|
||||
"""
|
||||
This method implements bash completion through
|
||||
a hierarchical (commands) dictionary object.
|
||||
"""
|
||||
parser = self._get_parser()
|
||||
|
||||
# navigate through commands, finding the list of commands
|
||||
|
||||
if not self._args:
|
||||
# show all the commands
|
||||
outcome += sorted(commands.keys())
|
||||
|
||||
for index, item in enumerate(self._args):
|
||||
if item in commands:
|
||||
commands = commands[item]
|
||||
if index == (len(self._args) - 1):
|
||||
# if this is the last one, generate
|
||||
# proper outcome elements.
|
||||
outcome += sorted(commands.keys())
|
||||
# reset last_arg so that outcome list
|
||||
# won't be filtered
|
||||
last_arg = ""
|
||||
elif index == (len(self._args) - 1):
|
||||
# if this is the last one, and item
|
||||
# is not in commands, outcome becomes
|
||||
# commands.keys()
|
||||
outcome += sorted(commands.keys())
|
||||
# no need to break here
|
||||
else:
|
||||
# item not in commands, but that's not the
|
||||
# last one, we must generate proper outcome
|
||||
# elements and stop right after
|
||||
outcome += sorted(commands.keys())
|
||||
break
|
||||
|
||||
return self._bashcomp(sys.stdout, last_arg, outcome)
|
||||
|
||||
def _bashcomp(self, stdout, last_arg, available_args):
|
||||
"""
|
||||
This method must be called from inside bashcomp() and
|
||||
|
||||
@@ -174,40 +174,7 @@ Execute advanced tasks on Entropy packages and the running system.
|
||||
"""
|
||||
Overridden from SoloCommand.
|
||||
"""
|
||||
outcome = []
|
||||
parser = self._get_parser()
|
||||
|
||||
# navigate through commands, finding the list of commands
|
||||
|
||||
if not self._args:
|
||||
# show all the commands
|
||||
outcome += sorted(self._commands.keys())
|
||||
|
||||
commands = self._commands
|
||||
for index, item in enumerate(self._args):
|
||||
if item in commands:
|
||||
commands = self._commands[item]
|
||||
if index == (len(self._args) - 1):
|
||||
# if this is the last one, generate
|
||||
# proper outcome elements.
|
||||
outcome += sorted(commands.keys())
|
||||
# reset last_arg so that outcome list
|
||||
# won't be filtered
|
||||
last_arg = ""
|
||||
elif index == (len(self._args) - 1):
|
||||
# if this is the last one, and item
|
||||
# is not in commands, outcome becomes
|
||||
# commands.keys()
|
||||
outcome += sorted(commands.keys())
|
||||
# no need to break here
|
||||
else:
|
||||
# item not in commands, but that's not the
|
||||
# last one, we must generate proper outcome
|
||||
# elements and stop right after
|
||||
outcome += sorted(commands.keys())
|
||||
break
|
||||
|
||||
return self._bashcomp(sys.stdout, last_arg, outcome)
|
||||
return self._hierarchical_bashcomp(last_arg, [], self._commands)
|
||||
|
||||
def _scan_packages(self, entropy_client, packages, installed=False):
|
||||
"""
|
||||
|
||||
@@ -341,39 +341,8 @@ Repository query tools.
|
||||
Overridden from SoloCommand.
|
||||
"""
|
||||
outcome = ["--quiet", "--verbose"]
|
||||
parser = self._get_parser()
|
||||
|
||||
# navigate through commands, finding the list of commands
|
||||
|
||||
if not self._args:
|
||||
# show all the commands
|
||||
outcome += sorted(self._commands.keys())
|
||||
|
||||
commands = self._commands
|
||||
for index, item in enumerate(self._args):
|
||||
if item in commands:
|
||||
commands = self._commands[item]
|
||||
if index == (len(self._args) - 1):
|
||||
# if this is the last one, generate
|
||||
# proper outcome elements.
|
||||
outcome += sorted(commands.keys())
|
||||
# reset last_arg so that outcome list
|
||||
# won't be filtered
|
||||
last_arg = ""
|
||||
elif index == (len(self._args) - 1):
|
||||
# if this is the last one, and item
|
||||
# is not in commands, outcome becomes
|
||||
# commands.keys()
|
||||
outcome += sorted(commands.keys())
|
||||
# no need to break here
|
||||
else:
|
||||
# item not in commands, but that's not the
|
||||
# last one, we must generate proper outcome
|
||||
# elements and stop right after
|
||||
outcome += sorted(commands.keys())
|
||||
break
|
||||
|
||||
return self._bashcomp(sys.stdout, last_arg, outcome)
|
||||
return self._hierarchical_bashcomp(
|
||||
last_arg, outcome, self._commands)
|
||||
|
||||
def _belongs(self, entropy_client):
|
||||
"""
|
||||
|
||||
@@ -155,40 +155,7 @@ Tools to rescue the running system.
|
||||
"""
|
||||
Overridden from SoloCommand.
|
||||
"""
|
||||
outcome = []
|
||||
parser = self._get_parser()
|
||||
|
||||
# navigate through commands, finding the list of commands
|
||||
|
||||
if not self._args:
|
||||
# show all the commands
|
||||
outcome += sorted(self._commands.keys())
|
||||
|
||||
commands = self._commands
|
||||
for index, item in enumerate(self._args):
|
||||
if item in commands:
|
||||
commands = self._commands[item]
|
||||
if index == (len(self._args) - 1):
|
||||
# if this is the last one, generate
|
||||
# proper outcome elements.
|
||||
outcome += sorted(commands.keys())
|
||||
# reset last_arg so that outcome list
|
||||
# won't be filtered
|
||||
last_arg = ""
|
||||
elif index == (len(self._args) - 1):
|
||||
# if this is the last one, and item
|
||||
# is not in commands, outcome becomes
|
||||
# commands.keys()
|
||||
outcome += sorted(commands.keys())
|
||||
# no need to break here
|
||||
else:
|
||||
# item not in commands, but that's not the
|
||||
# last one, we must generate proper outcome
|
||||
# elements and stop right after
|
||||
outcome += sorted(commands.keys())
|
||||
break
|
||||
|
||||
return self._bashcomp(sys.stdout, last_arg, outcome)
|
||||
return self._hierarchical_bashcomp(last_arg, [], self._commands)
|
||||
|
||||
def _check_repository(self, entropy_client, repo):
|
||||
"""
|
||||
|
||||
@@ -178,40 +178,7 @@ Manage User Generate Content (votes, comments, files).
|
||||
"""
|
||||
Overridden from SoloCommand.
|
||||
"""
|
||||
outcome = []
|
||||
parser = self._get_parser()
|
||||
|
||||
# navigate through commands, finding the list of commands
|
||||
|
||||
if not self._args:
|
||||
# show all the commands
|
||||
outcome += sorted(self._commands.keys())
|
||||
|
||||
commands = self._commands
|
||||
for index, item in enumerate(self._args):
|
||||
if item in commands:
|
||||
commands = self._commands[item]
|
||||
if index == (len(self._args) - 1):
|
||||
# if this is the last one, generate
|
||||
# proper outcome elements.
|
||||
outcome += sorted(commands.keys())
|
||||
# reset last_arg so that outcome list
|
||||
# won't be filtered
|
||||
last_arg = ""
|
||||
elif index == (len(self._args) - 1):
|
||||
# if this is the last one, and item
|
||||
# is not in commands, outcome becomes
|
||||
# commands.keys()
|
||||
outcome += sorted(commands.keys())
|
||||
# no need to break here
|
||||
else:
|
||||
# item not in commands, but that's not the
|
||||
# last one, we must generate proper outcome
|
||||
# elements and stop right after
|
||||
outcome += sorted(commands.keys())
|
||||
break
|
||||
|
||||
return self._bashcomp(sys.stdout, last_arg, outcome)
|
||||
return self._hierarchical_bashcomp(last_arg, [], self._commands)
|
||||
|
||||
def _login(self, entropy_client):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user