Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def do_delegation(self, cmd, args):
load_plugins()
# project precondition
proj_path = registry.pathfor(cmd)
if proj_path is not None:
os.chdir(proj_path)
cmd = args[0]
args = args[1:]
if '-' in cmd:
cmd, newargs = cmd.split('-', 1)
args = [newargs, ] + args
comcls = command.get(cmd) or command.get('help')
try:
if comcls:
comobj = comcls(args)
if comobj.is_relevant():
comobj._execute()
else:
helpcls = command.get('help')
helpobj = helpcls((cmd,))
helpobj._execute()
print "\n'%s %s' command not relevant here." % (cmd, ' '.join(args).strip())
else:
self.do_default()
except KeyboardInterrupt:
print "\n"
pass
if choices:
return choices
# we want commands to complete before
# project names, so if we don't return any
# choices above, complete a project name now
# if we're completing the first argument
if nargs == 1:
if proj_path is not None:
return os.path.basename(proj_path)
# # # # # # # #
# sub-commands
elif nargs == off + 1:
# get our parent command
com = args[off-1]
# get its class
comcls = command.get(com)
# if it is a delegate command
if hasattr(comcls, 'get_subcommands'):
# get partial subcommand name if user has typed anything
# or else use empty string to complete all
subcom = args[off] if len(args) == off+1 else ''
# get a list of the parent command's subcommands
subcom_choices = comcls.get_subcommands()
# if there are any subcommands
if subcom_choices:
# clean subcommand names (they use command-subcommand format)
choices = [k.split('-')[-1] for k in subcom_choices]
# return the subcommands that start with the partial subcommand name
return " ".join([c for c in choices if c.startswith(subcom)])
# return nothing
return ""
def process_simplecom(self, name, collen):
comcls = command.get(name)
comobj = comcls([])
if comobj.is_relevant() or self.options.all:
usage = comobj.parser.format_usage().replace("usage: ", "")
doc = comcls.__doc__ or ''
print "{0: >{cl}} - {1: ^24}".format(usage,
doc.strip(),
cl=collen)
# Help command
if args:
# complete help command
if nargs == off and "help".startswith(args[-1]):
return 'help'
# or increase offset by 1
elif "help" in args:
off += 1
# # # # # # # #
# base-command
if nargs == off:
arg = '' # default to empty arg
if len(args) == off:
# set working arg to item at offset
arg = args[off-1]
choices = " ".join([c for c in command._commands
if c.startswith(arg)
and command.get(c)([]).is_relevant()])
# return the choices if there are any
if choices:
return choices
# we want commands to complete before
# project names, so if we don't return any
# choices above, complete a project name now
# if we're completing the first argument
if nargs == 1:
if proj_path is not None:
return os.path.basename(proj_path)
# # # # # # # #
# sub-commands
elif nargs == off + 1:
# get our parent command
def do_default_help(self):
'''
Render pin's general help
This method will iterate through all available commands that declare
themselves as being 'relevant'. Some processing is done to determine
formatting widths of the output and help for delegate-commands
that contain subcommands are dynamically computed.
'''
# print generic pin help
CommandDelegator.parser.print_help()
comkeys = [k for k in command.all().keys() if '-' not in k]
maxlength = len(max(comkeys, key=len))
simplekeys = [] # commands without subcommands
containerkeys = [] # subcommand delgates
subcomkeys = []
submaxlength = maxlength
# iterate over all commands
for k in comkeys:
# get command class
comcls = command.get(k)
# get dummy instance
comobj = comcls([])
# check if command is relevant
if comobj.is_relevant() or self.options.all:
# add to specific list based on `get_subcommands' attribute
if hasattr(comcls, 'get_subcommands'):
containerkeys.append(k)