annotate sat_frontends/jp/cmd_shell.py @ 2608:0883bac573fd

jp (forums/edit): fixed unicode when dumping in json
author Goffi <goffi@goffi.org>
date Sun, 03 Jun 2018 18:05:01 +0200
parents 26edcf3a30eb
children 56f94936df1e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SàT command line tool
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import base
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import cmd
2313
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
23 import sys
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.i18n import _
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat_frontends.jp.constants import Const as C
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
27 from sat_frontends.jp import arg_tools
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.tools.common.ansi import ANSI as A
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import shlex
2313
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
30 import subprocess
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 __commands__ = ["Shell"]
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 INTRO = _(u"""Welcome to {app_name} shell, the Salut à Toi shell !
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 This enrironment helps you using several {app_name} commands with similar parameters.
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
2354
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
37 To quit, just enter "quit" or press C-d.
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 Enter "help" or "?" to know what to do
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 """).format(app_name = C.APP_NAME)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 class Shell(base.CommandBase, cmd.Cmd):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def __init__(self, host):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 base.CommandBase.__init__(self, host, 'shell', help=_(u'launch jp in shell (REPL) mode'))
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 cmd.Cmd.__init__(self)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def parse_args(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 """parse line arguments"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 return shlex.split(args, posix=True)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def update_path(self):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self._cur_parser = self.host.parser
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.help = u''
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 for idx, path_elt in enumerate(self.path):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 try:
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
57 self._cur_parser = arg_tools.get_cmd_choices(path_elt, self._cur_parser)
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 except exceptions.NotFound:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.disp(_(u'bad command path'), error=True)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.path=self.path[:idx]
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 break
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 else:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self.help = self._cur_parser
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.prompt = A.color(C.A_PROMPT_PATH, u'/'.join(self.path)) + A.color(C.A_PROMPT_SUF, u'> ')
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 try:
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
67 self.actions = arg_tools.get_cmd_choices(parser=self._cur_parser).keys()
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 except exceptions.NotFound:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.actions = []
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def add_parser_options(self):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 pass
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def format_args(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 """format argument to be printed with quotes if needed"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 for arg in args:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 if " " in arg:
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
78 yield arg_tools.escape(arg)
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 else:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 yield arg
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
2313
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
82 def run_cmd(self, args, external=False):
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
83 """run command and retur exit code
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
84
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
85 @param args[list[string]]: arguments of the command
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
86 must not include program name
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
87 @param external(bool): True if it's an external command (i.e. not jp)
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
88 @return (int): exit code (0 success, any other int failure)
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
89 """
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
90 # FIXME: we have to use subprocess
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
91 # and relaunch whole python for now
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
92 # because if host.quit() is called in D-Bus callback
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
93 # GLib quit the whole app without possibility to stop it
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
94 # didn't found a nice way to work around it so far
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
95 # Situation should be better when we'll move away from python-dbus
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
96 if self.verbose:
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
97 self.disp(_(u"COMMAND {external}=> {args}").format(
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
98 external = _(u'(external) ') if external else u'',
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
99 args=u' '.join(self.format_args(args))))
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
100 if not external:
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
101 args = sys.argv[0:1] + args
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
102 ret_code= subprocess.call(args)
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
103 # XXX: below is a way to launch the command without creating a new process
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
104 # may be used when a solution to the aforementioned issue is there
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
105 # try:
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
106 # self.host.run(args)
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
107 # except SystemExit as e:
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
108 # ret_code = e.code
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
109 # except Exception as e:
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
110 # self.disp(A.color(C.A_FAILURE, u'command failed with an exception: {msg}'.format(msg=e)), error=True)
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
111 # ret_code = 1
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
112 # else:
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
113 # ret_code = 0
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
114
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
115 if ret_code != 0:
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
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)
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
117 return ret_code
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
118
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def default(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 """called when no shell command is recognized
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 will launch the command with args on the line
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 (i.e. will launch do [args])
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 """
2354
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
125 if args=='EOF':
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
126 self.do_quit('')
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 self.do_do(args)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 def do_help(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 """show help message"""
2310
5996063ecad7 jp (shell): don't print header if a command is specified in help
Goffi <goffi@goffi.org>
parents: 2309
diff changeset
131 if not args:
5996063ecad7 jp (shell): don't print header if a command is specified in help
Goffi <goffi@goffi.org>
parents: 2309
diff changeset
132 self.disp(A.color(C.A_HEADER, _(u'Shell commands:')), no_lf=True)
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 super(Shell, self).do_help(args)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 if not args:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 self.disp(A.color(C.A_HEADER, _(u'Action commands:')))
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 help_list = self._cur_parser.format_help().split('\n\n')
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 print('\n\n'.join(help_list[1 if self.path else 2:]))
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 def do_debug(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 """launch internal debugger"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 try:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 import ipdb as pdb
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 except ImportError:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 import pdb
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 pdb.set_trace()
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 def do_verbose(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 """show verbose mode, or (de)activate it"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 args = self.parse_args(args)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 if args:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 self.verbose = C.bool(args[0])
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self.disp(_(u'verbose mode is {status}').format(
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 status = _(u'ENABLED') if self.verbose else _(u'DISABLED')))
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 def do_cmd(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 """change command path"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 if args == '..':
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 self.path = self.path[:-1]
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 else:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 if not args or args[0] == '/':
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 self.path = []
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 args = '/'.join(args.split())
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 for path_elt in args.split('/'):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 path_elt = path_elt.strip()
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 if not path_elt:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 continue
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 self.path.append(path_elt)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self.update_path()
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 def do_version(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 """show current SàT/jp version"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 try:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 self.host.run(['--version'])
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 except SystemExit:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 pass
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
2313
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
177 def do_shell(self, args):
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
178 """launch an external command (you can use ![command] too)"""
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
179 args = self.parse_args(args)
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
180 self.run_cmd(args, external=True)
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
181
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 def do_do(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 """lauch a command"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 args = self.parse_args(args)
2354
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
185 if (self._not_default_profile and
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
186 not '-p' in args and
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
187 not '--profile' in args and
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
188 not 'profile' in self.use):
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
189 # profile is not specified and we are not using the default profile
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
190 # so we need to add it in arguments to use current user profile
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
191 if self.verbose:
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
192 self.disp(_(u'arg profile={profile} (logged profile)').format(profile=self.profile))
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
193 use = self.use.copy()
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
194 use['profile'] = self.profile
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
195 else:
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
196 use = self.use
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
197
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
198
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 # args may be modified by use_args
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 # to remove subparsers from it
2317
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
201 parser_args, use_args = arg_tools.get_use_args(self.host,
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
202 args,
2354
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
203 use,
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
204 verbose=self.verbose,
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
205 parser=self._cur_parser
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
206 )
2317
f4e05600577b jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents: 2315
diff changeset
207 cmd_args = self.path + parser_args + use_args
2313
6ff5212997c7 jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents: 2311
diff changeset
208 self.run_cmd(cmd_args)
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 def do_use(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 """fix an argument"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 args = self.parse_args(args)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 if not args:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 if not self.use:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 self.disp(_(u'no argument in USE'))
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 else:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 self.disp(_(u'arguments in USE:'))
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 for arg, value in self.use.iteritems():
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
219 self.disp(_(A.color(C.A_SUBHEADER, arg, A.RESET, u' = ', arg_tools.escape(value))))
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 elif len(args) != 2:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 self.disp(u'bad syntax, please use:\nuse [arg] [value]', error=True)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 else:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 self.use[args[0]] = u' '.join(args[1:])
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 if self.verbose:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 self.disp('set {name} = {value}'.format(
2315
8d9bd5d77336 jp (arg_tools): moved get_cmd_choices, get_use_args and escape to a new arg_tools module, so they can be used in other commands than shell
Goffi <goffi@goffi.org>
parents: 2313
diff changeset
226 name = args[0], value=arg_tools.escape(args[1])))
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 def do_use_clear(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 """unset one or many argument(s) in USE, or all of them if no arg is specified"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 args = self.parse_args(args)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 if not args:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 self.use.clear()
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 else:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 for arg in args:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 try:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 del self.use[arg]
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 except KeyError:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 self.disp(A.color(C.A_FAILURE, _(u'argument {name} not found').format(name=arg)), error=True)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 else:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 if self.verbose:
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 self.disp(_(u'argument {name} removed').format(name=arg))
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
242
2311
a42a2478abd2 jp (shell): added "whoami" command to print currently used profile
Goffi <goffi@goffi.org>
parents: 2310
diff changeset
243 def do_whoami(self, args):
a42a2478abd2 jp (shell): added "whoami" command to print currently used profile
Goffi <goffi@goffi.org>
parents: 2310
diff changeset
244 u"""print profile currently used"""
a42a2478abd2 jp (shell): added "whoami" command to print currently used profile
Goffi <goffi@goffi.org>
parents: 2310
diff changeset
245 self.disp(self.profile)
a42a2478abd2 jp (shell): added "whoami" command to print currently used profile
Goffi <goffi@goffi.org>
parents: 2310
diff changeset
246
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 def do_quit(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 u"""quit the shell"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 self.disp(_(u'good bye!'))
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 self.host.quit()
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 def do_exit(self, args):
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 u"""alias for quit"""
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 self.do_quit(args)
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 def start(self):
2354
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
257 default_profile = self.host.bridge.profileNameGet(C.PROF_KEY_DEFAULT)
5129a0506739 jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents: 2317
diff changeset
258 self._not_default_profile = self.profile != default_profile
2309
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 self.path = []
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 self._cur_parser = self.host.parser
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 self.use = {}
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 self.verbose = False
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 self.update_path()
c7a72b75232b jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 self.cmdloop(INTRO.encode('utf-8'))