Mercurial > libervia-backend
annotate libervia/cli/cmd_shell.py @ 4276:00a9316547ed
plugin XEP-0167: move ICE candidates parsing/SDP line building in separate methods:
These methods will be used by incoming "Conferences" component.
rel 445
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 05 Jul 2024 17:18:37 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
4 # Libervia CLI |
3479 | 5 # Copyright (C) 2009-2021 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 cmd |
2313
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
22 import sys |
3040 | 23 import shlex |
24 import subprocess | |
25 from . import base | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
26 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
27 from libervia.backend.core import exceptions |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
28 from libervia.cli.constants import Const as C |
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
29 from libervia.cli import arg_tools |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
30 from libervia.backend.tools.common.ansi import ANSI as A |
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"] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
33 INTRO = _( |
3028 | 34 """Welcome to {app_name} shell, the Salut à Toi shell ! |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 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
|
37 |
2354
5129a0506739
jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents:
2317
diff
changeset
|
38 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
|
39 Enter "help" or "?" to know what to do |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
40 """ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
41 ).format(app_name=C.APP_NAME) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
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 class Shell(base.CommandBase, cmd.Cmd): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 base.CommandBase.__init__( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
47 self, host, "shell", help=_("launch libervia-cli in shell (REPL) mode") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
48 ) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 cmd.Cmd.__init__(self) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 def parse_args(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 """parse line arguments""" |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 return shlex.split(args, posix=True) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 def update_path(self): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 self._cur_parser = self.host.parser |
3028 | 57 self.help = "" |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 for idx, path_elt in enumerate(self.path): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 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
|
60 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
|
61 except exceptions.NotFound: |
3028 | 62 self.disp(_("bad command path"), error=True) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 self.path = self.path[:idx] |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 break |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 else: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 self.help = self._cur_parser |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
3028 | 68 self.prompt = A.color(C.A_PROMPT_PATH, "/".join(self.path)) + A.color( |
69 C.A_PROMPT_SUF, "> " | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 ) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 try: |
3028 | 72 self.actions = list(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
|
73 except exceptions.NotFound: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 self.actions = [] |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 def add_parser_options(self): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 pass |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 def format_args(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 """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
|
81 for arg in args: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 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
|
83 yield arg_tools.escape(arg) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 else: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 yield arg |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 |
2313
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
87 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
|
88 """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
|
89 |
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
90 @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
|
91 must not include program name |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
92 @param external(bool): True if it's an external command (i.e. not libervia-cli) |
2313
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
93 @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
|
94 """ |
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
95 # 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
|
96 # 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
|
97 # 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
|
98 # 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
|
99 # 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
|
100 # 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
|
101 if self.verbose: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 self.disp( |
3028 | 103 _("COMMAND {external}=> {args}").format( |
104 external=_("(external) ") if external else "", | |
105 args=" ".join(self.format_args(args)), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
107 ) |
2313
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
108 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
|
109 args = sys.argv[0:1] + args |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
110 ret_code = subprocess.call(args) |
2313
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
111 # 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
|
112 # 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
|
113 # try: |
3481
7892585b7e17
core (setup), jp, primitivus: update console scripts + classifiers:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
114 # self.host._run(args) |
2313
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
115 # 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
|
116 # 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
|
117 # 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
|
118 # 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
|
119 # 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
|
120 # else: |
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
121 # 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
|
122 |
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
123 if ret_code != 0: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
124 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
125 A.color( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
126 C.A_FAILURE, |
3028 | 127 "command failed with an error code of {err_no}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
128 err_no=ret_code |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
129 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
130 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
131 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 ) |
2313
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
133 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
|
134 |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 def default(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 """called when no shell command is recognized |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 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
|
139 (i.e. will launch do [args]) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 if args == "EOF": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 self.do_quit("") |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 self.do_do(args) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 def do_help(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 """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
|
147 if not args: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
148 self.disp(A.color(C.A_HEADER, _("Shell commands:")), end=" ") |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 super(Shell, self).do_help(args) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 if not args: |
3028 | 151 self.disp(A.color(C.A_HEADER, _("Action commands:"))) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
152 help_list = self._cur_parser.format_help().split("\n\n") |
3028 | 153 print(("\n\n".join(help_list[1 if self.path else 2 :]))) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 |
3040 | 155 # FIXME: debug crashes on exit and is not that useful, |
156 # keeping it until refactoring, may be removed entirely then | |
157 # def do_debug(self, args): | |
158 # """launch internal debugger""" | |
159 # try: | |
160 # import ipdb as pdb | |
161 # except ImportError: | |
162 # import pdb | |
163 # pdb.set_trace() | |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 def do_verbose(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 """show verbose mode, or (de)activate it""" |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 args = self.parse_args(args) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 if args: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 self.verbose = C.bool(args[0]) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 self.disp( |
3028 | 171 _("verbose mode is {status}").format( |
172 status=_("ENABLED") if self.verbose else _("DISABLED") | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
173 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
174 ) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 def do_cmd(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 """change command path""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
178 if args == "..": |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 self.path = self.path[:-1] |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
181 if not args or args[0] == "/": |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 self.path = [] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
183 args = "/".join(args.split()) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
184 for path_elt in args.split("/"): |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 path_elt = path_elt.strip() |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 if not path_elt: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 continue |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 self.path.append(path_elt) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 self.update_path() |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 def do_version(self, args): |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
192 """show current backend/CLI version""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
193 self.run_cmd(["--version"]) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 |
2313
6ff5212997c7
jp (shell): use of subprocess instead of running commands in the same process:
Goffi <goffi@goffi.org>
parents:
2311
diff
changeset
|
195 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
|
196 """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
|
197 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
|
198 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
|
199 |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 def do_do(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 """lauch a command""" |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 args = self.parse_args(args) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
203 if ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
204 self._not_default_profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
205 and not "-p" in args |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
206 and not "--profile" in args |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
207 and not "profile" in self.use |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
208 ): |
2354
5129a0506739
jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents:
2317
diff
changeset
|
209 # 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
|
210 # 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
|
211 if self.verbose: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
212 self.disp( |
3028 | 213 _("arg profile={profile} (logged profile)").format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
214 profile=self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
215 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
216 ) |
2354
5129a0506739
jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents:
2317
diff
changeset
|
217 use = self.use.copy() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
218 use["profile"] = self.profile |
2354
5129a0506739
jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents:
2317
diff
changeset
|
219 else: |
5129a0506739
jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents:
2317
diff
changeset
|
220 use = self.use |
5129a0506739
jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents:
2317
diff
changeset
|
221 |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 # args may be modified by use_args |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 # to remove subparsers from it |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
224 parser_args, use_args = arg_tools.get_use_args( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
225 self.host, args, use, verbose=self.verbose, parser=self._cur_parser |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
226 ) |
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
|
227 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
|
228 self.run_cmd(cmd_args) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 def do_use(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 """fix an argument""" |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 args = self.parse_args(args) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 if not args: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 if not self.use: |
3028 | 235 self.disp(_("no argument in USE")) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 else: |
3028 | 237 self.disp(_("arguments in USE:")) |
238 for arg, value in self.use.items(): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
239 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
240 _( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
241 A.color( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
242 C.A_SUBHEADER, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
243 arg, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
244 A.RESET, |
3028 | 245 " = ", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
246 arg_tools.escape(value), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
247 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
248 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
249 ) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 elif len(args) != 2: |
3028 | 251 self.disp("bad syntax, please use:\nuse [arg] [value]", error=True) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 else: |
3028 | 253 self.use[args[0]] = " ".join(args[1:]) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 if self.verbose: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
255 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
256 "set {name} = {value}".format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
257 name=args[0], value=arg_tools.escape(args[1]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
258 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
259 ) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 def do_use_clear(self, args): |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 """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
|
263 args = self.parse_args(args) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 if not args: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 self.use.clear() |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 else: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 for arg in args: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 try: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 del self.use[arg] |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
271 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
272 A.color( |
3028 | 273 C.A_FAILURE, _("argument {name} not found").format(name=arg) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
274 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
275 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
276 ) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 else: |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 if self.verbose: |
3028 | 279 self.disp(_("argument {name} removed").format(name=arg)) |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 |
2311
a42a2478abd2
jp (shell): added "whoami" command to print currently used profile
Goffi <goffi@goffi.org>
parents:
2310
diff
changeset
|
281 def do_whoami(self, args): |
3028 | 282 """print profile currently used""" |
2311
a42a2478abd2
jp (shell): added "whoami" command to print currently used profile
Goffi <goffi@goffi.org>
parents:
2310
diff
changeset
|
283 self.disp(self.profile) |
a42a2478abd2
jp (shell): added "whoami" command to print currently used profile
Goffi <goffi@goffi.org>
parents:
2310
diff
changeset
|
284 |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 def do_quit(self, args): |
3028 | 286 """quit the shell""" |
287 self.disp(_("good bye!")) | |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 self.host.quit() |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 def do_exit(self, args): |
3028 | 291 """alias for quit""" |
2309
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 self.do_quit(args) |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 |
3040 | 294 async def start(self): |
295 # FIXME: "shell" is currently kept synchronous as it works well as it | |
296 # and it will be refactored soon. | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3481
diff
changeset
|
297 default_profile = self.host.bridge.profile_name_get(C.PROF_KEY_DEFAULT) |
2354
5129a0506739
jp (shell): fixed use of profile + added EOF handling:
Goffi <goffi@goffi.org>
parents:
2317
diff
changeset
|
298 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
|
299 self.path = [] |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 self._cur_parser = self.host.parser |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 self.use = {} |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 self.verbose = False |
c7a72b75232b
jp (shell): shell command (REPL mode), first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 self.update_path() |
3040 | 304 self.cmdloop(INTRO) |