code style reformatting using black

This commit is contained in:
Goffi 2018-06-27 20:14:46 +02:00
commit 744cb0c503

View file

@ -30,19 +30,22 @@ import shlex
import subprocess
__commands__ = ["Shell"]
INTRO = _(u"""Welcome to {app_name} shell, the Salut à Toi shell !
INTRO = _(
u"""Welcome to {app_name} shell, the Salut à Toi shell !
This enrironment helps you using several {app_name} commands with similar parameters.
To quit, just enter "quit" or press C-d.
Enter "help" or "?" to know what to do
""").format(app_name = C.APP_NAME)
"""
).format(app_name=C.APP_NAME)
class Shell(base.CommandBase, cmd.Cmd):
def __init__(self, host):
base.CommandBase.__init__(self, host, 'shell', help=_(u'launch jp in shell (REPL) mode'))
base.CommandBase.__init__(
self, host, "shell", help=_(u"launch jp in shell (REPL) mode")
)
cmd.Cmd.__init__(self)
def parse_args(self, args):
@ -51,18 +54,20 @@ class Shell(base.CommandBase, cmd.Cmd):
def update_path(self):
self._cur_parser = self.host.parser
self.help = u''
self.help = u""
for idx, path_elt in enumerate(self.path):
try:
self._cur_parser = arg_tools.get_cmd_choices(path_elt, self._cur_parser)
except exceptions.NotFound:
self.disp(_(u'bad command path'), error=True)
self.path=self.path[:idx]
self.disp(_(u"bad command path"), error=True)
self.path = self.path[:idx]
break
else:
self.help = self._cur_parser
self.prompt = A.color(C.A_PROMPT_PATH, u'/'.join(self.path)) + A.color(C.A_PROMPT_SUF, u'> ')
self.prompt = A.color(C.A_PROMPT_PATH, u"/".join(self.path)) + A.color(
C.A_PROMPT_SUF, u"> "
)
try:
self.actions = arg_tools.get_cmd_choices(parser=self._cur_parser).keys()
except exceptions.NotFound:
@ -94,12 +99,15 @@ class Shell(base.CommandBase, cmd.Cmd):
# didn't found a nice way to work around it so far
# Situation should be better when we'll move away from python-dbus
if self.verbose:
self.disp(_(u"COMMAND {external}=> {args}").format(
external = _(u'(external) ') if external else u'',
args=u' '.join(self.format_args(args))))
self.disp(
_(u"COMMAND {external}=> {args}").format(
external=_(u"(external) ") if external else u"",
args=u" ".join(self.format_args(args)),
)
)
if not external:
args = sys.argv[0:1] + args
ret_code= subprocess.call(args)
ret_code = subprocess.call(args)
# XXX: below is a way to launch the command without creating a new process
# may be used when a solution to the aforementioned issue is there
# try:
@ -113,7 +121,15 @@ class Shell(base.CommandBase, cmd.Cmd):
# ret_code = 0
if ret_code != 0:
self.disp(A.color(C.A_FAILURE, u'command failed with an error code of {err_no}'.format(err_no=ret_code)), error=True)
self.disp(
A.color(
C.A_FAILURE,
u"command failed with an error code of {err_no}".format(
err_no=ret_code
),
),
error=True,
)
return ret_code
def default(self, args):
@ -122,19 +138,19 @@ class Shell(base.CommandBase, cmd.Cmd):
will launch the command with args on the line
(i.e. will launch do [args])
"""
if args=='EOF':
self.do_quit('')
if args == "EOF":
self.do_quit("")
self.do_do(args)
def do_help(self, args):
"""show help message"""
if not args:
self.disp(A.color(C.A_HEADER, _(u'Shell commands:')), no_lf=True)
self.disp(A.color(C.A_HEADER, _(u"Shell commands:")), no_lf=True)
super(Shell, self).do_help(args)
if not args:
self.disp(A.color(C.A_HEADER, _(u'Action commands:')))
help_list = self._cur_parser.format_help().split('\n\n')
print('\n\n'.join(help_list[1 if self.path else 2:]))
self.disp(A.color(C.A_HEADER, _(u"Action commands:")))
help_list = self._cur_parser.format_help().split("\n\n")
print("\n\n".join(help_list[1 if self.path else 2 :]))
def do_debug(self, args):
"""launch internal debugger"""
@ -149,18 +165,21 @@ class Shell(base.CommandBase, cmd.Cmd):
args = self.parse_args(args)
if args:
self.verbose = C.bool(args[0])
self.disp(_(u'verbose mode is {status}').format(
status = _(u'ENABLED') if self.verbose else _(u'DISABLED')))
self.disp(
_(u"verbose mode is {status}").format(
status=_(u"ENABLED") if self.verbose else _(u"DISABLED")
)
)
def do_cmd(self, args):
"""change command path"""
if args == '..':
if args == "..":
self.path = self.path[:-1]
else:
if not args or args[0] == '/':
if not args or args[0] == "/":
self.path = []
args = '/'.join(args.split())
for path_elt in args.split('/'):
args = "/".join(args.split())
for path_elt in args.split("/"):
path_elt = path_elt.strip()
if not path_elt:
continue
@ -170,7 +189,7 @@ class Shell(base.CommandBase, cmd.Cmd):
def do_version(self, args):
"""show current SàT/jp version"""
try:
self.host.run(['--version'])
self.host.run(["--version"])
except SystemExit:
pass
@ -182,28 +201,30 @@ class Shell(base.CommandBase, cmd.Cmd):
def do_do(self, args):
"""lauch a command"""
args = self.parse_args(args)
if (self._not_default_profile and
not '-p' in args and
not '--profile' in args and
not 'profile' in self.use):
if (
self._not_default_profile
and not "-p" in args
and not "--profile" in args
and not "profile" in self.use
):
# profile is not specified and we are not using the default profile
# so we need to add it in arguments to use current user profile
if self.verbose:
self.disp(_(u'arg profile={profile} (logged profile)').format(profile=self.profile))
self.disp(
_(u"arg profile={profile} (logged profile)").format(
profile=self.profile
)
)
use = self.use.copy()
use['profile'] = self.profile
use["profile"] = self.profile
else:
use = self.use
# args may be modified by use_args
# to remove subparsers from it
parser_args, use_args = arg_tools.get_use_args(self.host,
args,
use,
verbose=self.verbose,
parser=self._cur_parser
)
parser_args, use_args = arg_tools.get_use_args(
self.host, args, use, verbose=self.verbose, parser=self._cur_parser
)
cmd_args = self.path + parser_args + use_args
self.run_cmd(cmd_args)
@ -212,18 +233,31 @@ class Shell(base.CommandBase, cmd.Cmd):
args = self.parse_args(args)
if not args:
if not self.use:
self.disp(_(u'no argument in USE'))
self.disp(_(u"no argument in USE"))
else:
self.disp(_(u'arguments in USE:'))
self.disp(_(u"arguments in USE:"))
for arg, value in self.use.iteritems():
self.disp(_(A.color(C.A_SUBHEADER, arg, A.RESET, u' = ', arg_tools.escape(value))))
self.disp(
_(
A.color(
C.A_SUBHEADER,
arg,
A.RESET,
u" = ",
arg_tools.escape(value),
)
)
)
elif len(args) != 2:
self.disp(u'bad syntax, please use:\nuse [arg] [value]', error=True)
self.disp(u"bad syntax, please use:\nuse [arg] [value]", error=True)
else:
self.use[args[0]] = u' '.join(args[1:])
self.use[args[0]] = u" ".join(args[1:])
if self.verbose:
self.disp('set {name} = {value}'.format(
name = args[0], value=arg_tools.escape(args[1])))
self.disp(
"set {name} = {value}".format(
name=args[0], value=arg_tools.escape(args[1])
)
)
def do_use_clear(self, args):
"""unset one or many argument(s) in USE, or all of them if no arg is specified"""
@ -235,10 +269,15 @@ class Shell(base.CommandBase, cmd.Cmd):
try:
del self.use[arg]
except KeyError:
self.disp(A.color(C.A_FAILURE, _(u'argument {name} not found').format(name=arg)), error=True)
self.disp(
A.color(
C.A_FAILURE, _(u"argument {name} not found").format(name=arg)
),
error=True,
)
else:
if self.verbose:
self.disp(_(u'argument {name} removed').format(name=arg))
self.disp(_(u"argument {name} removed").format(name=arg))
def do_whoami(self, args):
u"""print profile currently used"""
@ -246,7 +285,7 @@ class Shell(base.CommandBase, cmd.Cmd):
def do_quit(self, args):
u"""quit the shell"""
self.disp(_(u'good bye!'))
self.disp(_(u"good bye!"))
self.host.quit()
def do_exit(self, args):
@ -261,4 +300,4 @@ class Shell(base.CommandBase, cmd.Cmd):
self.use = {}
self.verbose = False
self.update_path()
self.cmdloop(INTRO.encode('utf-8'))
self.cmdloop(INTRO.encode("utf-8"))