comparison sat_frontends/jp/cmd_shell.py @ 3040:fee60f17ebac

jp: jp asyncio port: /!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\ This patch implements the port of jp to asyncio, so it is now correctly using the bridge asynchronously, and it can be used with bridges like `pb`. This also simplify the code, notably for things which were previously implemented with many callbacks (like pagination with RSM). During the process, some behaviours have been modified/fixed, in jp and backends, check diff for details.
author Goffi <goffi@goffi.org>
date Wed, 25 Sep 2019 08:56:41 +0200
parents ab2696e34d29
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3039:a1bc34f90fa5 3040:fee60f17ebac
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 20
21 from . import base
22 import cmd 21 import cmd
23 import sys 22 import sys
23 import shlex
24 import subprocess
25 from . import base
24 from sat.core.i18n import _ 26 from sat.core.i18n import _
25 from sat.core import exceptions 27 from sat.core import exceptions
26 from sat_frontends.jp.constants import Const as C 28 from sat_frontends.jp.constants import Const as C
27 from sat_frontends.jp import arg_tools 29 from sat_frontends.jp import arg_tools
28 from sat.tools.common.ansi import ANSI as A 30 from sat.tools.common.ansi import ANSI as A
29 import shlex
30 import subprocess
31 31
32 __commands__ = ["Shell"] 32 __commands__ = ["Shell"]
33 INTRO = _( 33 INTRO = _(
34 """Welcome to {app_name} shell, the Salut à Toi shell ! 34 """Welcome to {app_name} shell, the Salut à Toi shell !
35 35
42 42
43 43
44 class Shell(base.CommandBase, cmd.Cmd): 44 class Shell(base.CommandBase, cmd.Cmd):
45 def __init__(self, host): 45 def __init__(self, host):
46 base.CommandBase.__init__( 46 base.CommandBase.__init__(
47 self, host, "shell", help=_("launch jp in shell (REPL) mode") 47 self, host, "shell",
48 help=_("launch jp in shell (REPL) mode")
48 ) 49 )
49 cmd.Cmd.__init__(self) 50 cmd.Cmd.__init__(self)
50 51
51 def parse_args(self, args): 52 def parse_args(self, args):
52 """parse line arguments""" 53 """parse line arguments"""
150 if not args: 151 if not args:
151 self.disp(A.color(C.A_HEADER, _("Action commands:"))) 152 self.disp(A.color(C.A_HEADER, _("Action commands:")))
152 help_list = self._cur_parser.format_help().split("\n\n") 153 help_list = self._cur_parser.format_help().split("\n\n")
153 print(("\n\n".join(help_list[1 if self.path else 2 :]))) 154 print(("\n\n".join(help_list[1 if self.path else 2 :])))
154 155
155 def do_debug(self, args): 156 # FIXME: debug crashes on exit and is not that useful,
156 """launch internal debugger""" 157 # keeping it until refactoring, may be removed entirely then
157 try: 158 # def do_debug(self, args):
158 import ipdb as pdb 159 # """launch internal debugger"""
159 except ImportError: 160 # try:
160 import pdb 161 # import ipdb as pdb
161 pdb.set_trace() 162 # except ImportError:
163 # import pdb
164 # pdb.set_trace()
162 165
163 def do_verbose(self, args): 166 def do_verbose(self, args):
164 """show verbose mode, or (de)activate it""" 167 """show verbose mode, or (de)activate it"""
165 args = self.parse_args(args) 168 args = self.parse_args(args)
166 if args: 169 if args:
186 self.path.append(path_elt) 189 self.path.append(path_elt)
187 self.update_path() 190 self.update_path()
188 191
189 def do_version(self, args): 192 def do_version(self, args):
190 """show current SàT/jp version""" 193 """show current SàT/jp version"""
191 try: 194 self.run_cmd(['--version'])
192 self.host.run(["--version"])
193 except SystemExit:
194 pass
195 195
196 def do_shell(self, args): 196 def do_shell(self, args):
197 """launch an external command (you can use ![command] too)""" 197 """launch an external command (you can use ![command] too)"""
198 args = self.parse_args(args) 198 args = self.parse_args(args)
199 self.run_cmd(args, external=True) 199 self.run_cmd(args, external=True)
290 290
291 def do_exit(self, args): 291 def do_exit(self, args):
292 """alias for quit""" 292 """alias for quit"""
293 self.do_quit(args) 293 self.do_quit(args)
294 294
295 def start(self): 295 async def start(self):
296 # FIXME: "shell" is currently kept synchronous as it works well as it
297 # and it will be refactored soon.
296 default_profile = self.host.bridge.profileNameGet(C.PROF_KEY_DEFAULT) 298 default_profile = self.host.bridge.profileNameGet(C.PROF_KEY_DEFAULT)
297 self._not_default_profile = self.profile != default_profile 299 self._not_default_profile = self.profile != default_profile
298 self.path = [] 300 self.path = []
299 self._cur_parser = self.host.parser 301 self._cur_parser = self.host.parser
300 self.use = {} 302 self.use = {}
301 self.verbose = False 303 self.verbose = False
302 self.update_path() 304 self.update_path()
303 self.cmdloop(INTRO.encode("utf-8")) 305 self.cmdloop(INTRO)