comparison sat_frontends/jp/cmd_shell.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
28 from sat.tools.common.ansi import ANSI as A 28 from sat.tools.common.ansi import ANSI as A
29 import shlex 29 import shlex
30 import subprocess 30 import subprocess
31 31
32 __commands__ = ["Shell"] 32 __commands__ = ["Shell"]
33 INTRO = _(u"""Welcome to {app_name} shell, the Salut à Toi shell ! 33 INTRO = _(
34 u"""Welcome to {app_name} shell, the Salut à Toi shell !
34 35
35 This enrironment helps you using several {app_name} commands with similar parameters. 36 This enrironment helps you using several {app_name} commands with similar parameters.
36 37
37 To quit, just enter "quit" or press C-d. 38 To quit, just enter "quit" or press C-d.
38 Enter "help" or "?" to know what to do 39 Enter "help" or "?" to know what to do
39 """).format(app_name = C.APP_NAME) 40 """
41 ).format(app_name=C.APP_NAME)
40 42
41 43
42 class Shell(base.CommandBase, cmd.Cmd): 44 class Shell(base.CommandBase, cmd.Cmd):
43
44 def __init__(self, host): 45 def __init__(self, host):
45 base.CommandBase.__init__(self, host, 'shell', help=_(u'launch jp in shell (REPL) mode')) 46 base.CommandBase.__init__(
47 self, host, "shell", help=_(u"launch jp in shell (REPL) mode")
48 )
46 cmd.Cmd.__init__(self) 49 cmd.Cmd.__init__(self)
47 50
48 def parse_args(self, args): 51 def parse_args(self, args):
49 """parse line arguments""" 52 """parse line arguments"""
50 return shlex.split(args, posix=True) 53 return shlex.split(args, posix=True)
51 54
52 def update_path(self): 55 def update_path(self):
53 self._cur_parser = self.host.parser 56 self._cur_parser = self.host.parser
54 self.help = u'' 57 self.help = u""
55 for idx, path_elt in enumerate(self.path): 58 for idx, path_elt in enumerate(self.path):
56 try: 59 try:
57 self._cur_parser = arg_tools.get_cmd_choices(path_elt, self._cur_parser) 60 self._cur_parser = arg_tools.get_cmd_choices(path_elt, self._cur_parser)
58 except exceptions.NotFound: 61 except exceptions.NotFound:
59 self.disp(_(u'bad command path'), error=True) 62 self.disp(_(u"bad command path"), error=True)
60 self.path=self.path[:idx] 63 self.path = self.path[:idx]
61 break 64 break
62 else: 65 else:
63 self.help = self._cur_parser 66 self.help = self._cur_parser
64 67
65 self.prompt = A.color(C.A_PROMPT_PATH, u'/'.join(self.path)) + A.color(C.A_PROMPT_SUF, u'> ') 68 self.prompt = A.color(C.A_PROMPT_PATH, u"/".join(self.path)) + A.color(
69 C.A_PROMPT_SUF, u"> "
70 )
66 try: 71 try:
67 self.actions = arg_tools.get_cmd_choices(parser=self._cur_parser).keys() 72 self.actions = arg_tools.get_cmd_choices(parser=self._cur_parser).keys()
68 except exceptions.NotFound: 73 except exceptions.NotFound:
69 self.actions = [] 74 self.actions = []
70 75
92 # because if host.quit() is called in D-Bus callback 97 # because if host.quit() is called in D-Bus callback
93 # GLib quit the whole app without possibility to stop it 98 # GLib quit the whole app without possibility to stop it
94 # didn't found a nice way to work around it so far 99 # didn't found a nice way to work around it so far
95 # Situation should be better when we'll move away from python-dbus 100 # Situation should be better when we'll move away from python-dbus
96 if self.verbose: 101 if self.verbose:
97 self.disp(_(u"COMMAND {external}=> {args}").format( 102 self.disp(
98 external = _(u'(external) ') if external else u'', 103 _(u"COMMAND {external}=> {args}").format(
99 args=u' '.join(self.format_args(args)))) 104 external=_(u"(external) ") if external else u"",
105 args=u" ".join(self.format_args(args)),
106 )
107 )
100 if not external: 108 if not external:
101 args = sys.argv[0:1] + args 109 args = sys.argv[0:1] + args
102 ret_code= subprocess.call(args) 110 ret_code = subprocess.call(args)
103 # XXX: below is a way to launch the command without creating a new process 111 # XXX: below is a way to launch the command without creating a new process
104 # may be used when a solution to the aforementioned issue is there 112 # may be used when a solution to the aforementioned issue is there
105 # try: 113 # try:
106 # self.host.run(args) 114 # self.host.run(args)
107 # except SystemExit as e: 115 # except SystemExit as e:
111 # ret_code = 1 119 # ret_code = 1
112 # else: 120 # else:
113 # ret_code = 0 121 # ret_code = 0
114 122
115 if ret_code != 0: 123 if ret_code != 0:
116 self.disp(A.color(C.A_FAILURE, u'command failed with an error code of {err_no}'.format(err_no=ret_code)), error=True) 124 self.disp(
125 A.color(
126 C.A_FAILURE,
127 u"command failed with an error code of {err_no}".format(
128 err_no=ret_code
129 ),
130 ),
131 error=True,
132 )
117 return ret_code 133 return ret_code
118 134
119 def default(self, args): 135 def default(self, args):
120 """called when no shell command is recognized 136 """called when no shell command is recognized
121 137
122 will launch the command with args on the line 138 will launch the command with args on the line
123 (i.e. will launch do [args]) 139 (i.e. will launch do [args])
124 """ 140 """
125 if args=='EOF': 141 if args == "EOF":
126 self.do_quit('') 142 self.do_quit("")
127 self.do_do(args) 143 self.do_do(args)
128 144
129 def do_help(self, args): 145 def do_help(self, args):
130 """show help message""" 146 """show help message"""
131 if not args: 147 if not args:
132 self.disp(A.color(C.A_HEADER, _(u'Shell commands:')), no_lf=True) 148 self.disp(A.color(C.A_HEADER, _(u"Shell commands:")), no_lf=True)
133 super(Shell, self).do_help(args) 149 super(Shell, self).do_help(args)
134 if not args: 150 if not args:
135 self.disp(A.color(C.A_HEADER, _(u'Action commands:'))) 151 self.disp(A.color(C.A_HEADER, _(u"Action commands:")))
136 help_list = self._cur_parser.format_help().split('\n\n') 152 help_list = self._cur_parser.format_help().split("\n\n")
137 print('\n\n'.join(help_list[1 if self.path else 2:])) 153 print("\n\n".join(help_list[1 if self.path else 2 :]))
138 154
139 def do_debug(self, args): 155 def do_debug(self, args):
140 """launch internal debugger""" 156 """launch internal debugger"""
141 try: 157 try:
142 import ipdb as pdb 158 import ipdb as pdb
147 def do_verbose(self, args): 163 def do_verbose(self, args):
148 """show verbose mode, or (de)activate it""" 164 """show verbose mode, or (de)activate it"""
149 args = self.parse_args(args) 165 args = self.parse_args(args)
150 if args: 166 if args:
151 self.verbose = C.bool(args[0]) 167 self.verbose = C.bool(args[0])
152 self.disp(_(u'verbose mode is {status}').format( 168 self.disp(
153 status = _(u'ENABLED') if self.verbose else _(u'DISABLED'))) 169 _(u"verbose mode is {status}").format(
170 status=_(u"ENABLED") if self.verbose else _(u"DISABLED")
171 )
172 )
154 173
155 def do_cmd(self, args): 174 def do_cmd(self, args):
156 """change command path""" 175 """change command path"""
157 if args == '..': 176 if args == "..":
158 self.path = self.path[:-1] 177 self.path = self.path[:-1]
159 else: 178 else:
160 if not args or args[0] == '/': 179 if not args or args[0] == "/":
161 self.path = [] 180 self.path = []
162 args = '/'.join(args.split()) 181 args = "/".join(args.split())
163 for path_elt in args.split('/'): 182 for path_elt in args.split("/"):
164 path_elt = path_elt.strip() 183 path_elt = path_elt.strip()
165 if not path_elt: 184 if not path_elt:
166 continue 185 continue
167 self.path.append(path_elt) 186 self.path.append(path_elt)
168 self.update_path() 187 self.update_path()
169 188
170 def do_version(self, args): 189 def do_version(self, args):
171 """show current SàT/jp version""" 190 """show current SàT/jp version"""
172 try: 191 try:
173 self.host.run(['--version']) 192 self.host.run(["--version"])
174 except SystemExit: 193 except SystemExit:
175 pass 194 pass
176 195
177 def do_shell(self, args): 196 def do_shell(self, args):
178 """launch an external command (you can use ![command] too)""" 197 """launch an external command (you can use ![command] too)"""
180 self.run_cmd(args, external=True) 199 self.run_cmd(args, external=True)
181 200
182 def do_do(self, args): 201 def do_do(self, args):
183 """lauch a command""" 202 """lauch a command"""
184 args = self.parse_args(args) 203 args = self.parse_args(args)
185 if (self._not_default_profile and 204 if (
186 not '-p' in args and 205 self._not_default_profile
187 not '--profile' in args and 206 and not "-p" in args
188 not 'profile' in self.use): 207 and not "--profile" in args
208 and not "profile" in self.use
209 ):
189 # profile is not specified and we are not using the default profile 210 # profile is not specified and we are not using the default profile
190 # so we need to add it in arguments to use current user profile 211 # so we need to add it in arguments to use current user profile
191 if self.verbose: 212 if self.verbose:
192 self.disp(_(u'arg profile={profile} (logged profile)').format(profile=self.profile)) 213 self.disp(
214 _(u"arg profile={profile} (logged profile)").format(
215 profile=self.profile
216 )
217 )
193 use = self.use.copy() 218 use = self.use.copy()
194 use['profile'] = self.profile 219 use["profile"] = self.profile
195 else: 220 else:
196 use = self.use 221 use = self.use
197 222
198
199 # args may be modified by use_args 223 # args may be modified by use_args
200 # to remove subparsers from it 224 # to remove subparsers from it
201 parser_args, use_args = arg_tools.get_use_args(self.host, 225 parser_args, use_args = arg_tools.get_use_args(
202 args, 226 self.host, args, use, verbose=self.verbose, parser=self._cur_parser
203 use, 227 )
204 verbose=self.verbose,
205 parser=self._cur_parser
206 )
207 cmd_args = self.path + parser_args + use_args 228 cmd_args = self.path + parser_args + use_args
208 self.run_cmd(cmd_args) 229 self.run_cmd(cmd_args)
209 230
210 def do_use(self, args): 231 def do_use(self, args):
211 """fix an argument""" 232 """fix an argument"""
212 args = self.parse_args(args) 233 args = self.parse_args(args)
213 if not args: 234 if not args:
214 if not self.use: 235 if not self.use:
215 self.disp(_(u'no argument in USE')) 236 self.disp(_(u"no argument in USE"))
216 else: 237 else:
217 self.disp(_(u'arguments in USE:')) 238 self.disp(_(u"arguments in USE:"))
218 for arg, value in self.use.iteritems(): 239 for arg, value in self.use.iteritems():
219 self.disp(_(A.color(C.A_SUBHEADER, arg, A.RESET, u' = ', arg_tools.escape(value)))) 240 self.disp(
241 _(
242 A.color(
243 C.A_SUBHEADER,
244 arg,
245 A.RESET,
246 u" = ",
247 arg_tools.escape(value),
248 )
249 )
250 )
220 elif len(args) != 2: 251 elif len(args) != 2:
221 self.disp(u'bad syntax, please use:\nuse [arg] [value]', error=True) 252 self.disp(u"bad syntax, please use:\nuse [arg] [value]", error=True)
222 else: 253 else:
223 self.use[args[0]] = u' '.join(args[1:]) 254 self.use[args[0]] = u" ".join(args[1:])
224 if self.verbose: 255 if self.verbose:
225 self.disp('set {name} = {value}'.format( 256 self.disp(
226 name = args[0], value=arg_tools.escape(args[1]))) 257 "set {name} = {value}".format(
258 name=args[0], value=arg_tools.escape(args[1])
259 )
260 )
227 261
228 def do_use_clear(self, args): 262 def do_use_clear(self, args):
229 """unset one or many argument(s) in USE, or all of them if no arg is specified""" 263 """unset one or many argument(s) in USE, or all of them if no arg is specified"""
230 args = self.parse_args(args) 264 args = self.parse_args(args)
231 if not args: 265 if not args:
233 else: 267 else:
234 for arg in args: 268 for arg in args:
235 try: 269 try:
236 del self.use[arg] 270 del self.use[arg]
237 except KeyError: 271 except KeyError:
238 self.disp(A.color(C.A_FAILURE, _(u'argument {name} not found').format(name=arg)), error=True) 272 self.disp(
273 A.color(
274 C.A_FAILURE, _(u"argument {name} not found").format(name=arg)
275 ),
276 error=True,
277 )
239 else: 278 else:
240 if self.verbose: 279 if self.verbose:
241 self.disp(_(u'argument {name} removed').format(name=arg)) 280 self.disp(_(u"argument {name} removed").format(name=arg))
242 281
243 def do_whoami(self, args): 282 def do_whoami(self, args):
244 u"""print profile currently used""" 283 u"""print profile currently used"""
245 self.disp(self.profile) 284 self.disp(self.profile)
246 285
247 def do_quit(self, args): 286 def do_quit(self, args):
248 u"""quit the shell""" 287 u"""quit the shell"""
249 self.disp(_(u'good bye!')) 288 self.disp(_(u"good bye!"))
250 self.host.quit() 289 self.host.quit()
251 290
252 def do_exit(self, args): 291 def do_exit(self, args):
253 u"""alias for quit""" 292 u"""alias for quit"""
254 self.do_quit(args) 293 self.do_quit(args)
259 self.path = [] 298 self.path = []
260 self._cur_parser = self.host.parser 299 self._cur_parser = self.host.parser
261 self.use = {} 300 self.use = {}
262 self.verbose = False 301 self.verbose = False
263 self.update_path() 302 self.update_path()
264 self.cmdloop(INTRO.encode('utf-8')) 303 self.cmdloop(INTRO.encode("utf-8"))