Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_debug.py @ 3050:d2a26ec74b31
doc (jp/common_arguments): updated note about `--pwd`
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 01 Oct 2019 22:54:18 +0200 |
parents | cf843dd7c345 |
children | 9d0df638c8b4 |
rev | line source |
---|---|
2038 | 1 #!/usr/bin/env python2 |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SàT command line tool | |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
2038 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
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/>. | |
19 | |
20 | |
3028 | 21 from . import base |
2038 | 22 from sat.core.i18n import _ |
23 from sat_frontends.jp.constants import Const as C | |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
24 from sat.tools.common.ansi import ANSI as A |
2038 | 25 import json |
26 | |
27 __commands__ = ["Debug"] | |
28 | |
29 | |
30 class BridgeCommon(object): | |
31 def evalArgs(self): | |
32 if self.args.arg: | |
33 try: | |
3028 | 34 return eval("[{}]".format(",".join(self.args.arg))) |
2038 | 35 except SyntaxError as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
36 self.disp( |
3028 | 37 "Can't evaluate arguments: {mess}\n{text}\n{offset}^".format( |
38 mess=e, text=e.text, offset=" " * (e.offset - 1) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
40 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
41 ) |
2038 | 42 self.host.quit(C.EXIT_BAD_ARG) |
43 else: | |
44 return [] | |
45 | |
46 | |
47 class Method(base.CommandBase, BridgeCommon): | |
48 def __init__(self, host): | |
3028 | 49 base.CommandBase.__init__(self, host, "method", help=_("call a bridge method")) |
2038 | 50 BridgeCommon.__init__(self) |
51 | |
52 def add_parser_options(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 self.parser.add_argument( |
3028 | 54 "method", type=str, help=_("name of the method to execute") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 self.parser.add_argument( |
3028 | 57 "arg", nargs="*", help=_("argument of the method") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 ) |
2038 | 59 |
3040 | 60 async def start(self): |
2038 | 61 method = getattr(self.host.bridge, self.args.method) |
3040 | 62 import inspect |
63 argspec = inspect.getargspec(method) | |
64 | |
65 kwargs = {} | |
66 if 'profile_key' in argspec.args: | |
67 kwargs['profile_key'] = self.profile | |
68 elif 'profile' in argspec.args: | |
69 kwargs['profile'] = self.profile | |
70 | |
2038 | 71 args = self.evalArgs() |
3040 | 72 |
2038 | 73 try: |
3040 | 74 ret = await method( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 *args, |
3040 | 76 **kwargs, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 ) |
3040 | 78 except Exception as e: |
79 self.disp(_(f"Error while executing {self.args.method}: {e}"), error=True) | |
80 self.host.quit(C.EXIT_ERROR) | |
81 else: | |
82 if ret is not None: | |
83 self.disp(str(ret)) | |
84 self.host.quit() | |
2038 | 85 |
86 | |
87 class Signal(base.CommandBase, BridgeCommon): | |
88 def __init__(self, host): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
89 base.CommandBase.__init__( |
3028 | 90 self, host, "signal", help=_("send a fake signal from backend") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 ) |
2038 | 92 BridgeCommon.__init__(self) |
93 | |
94 def add_parser_options(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 self.parser.add_argument( |
3028 | 96 "signal", type=str, help=_("name of the signal to send") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 self.parser.add_argument( |
3028 | 99 "arg", nargs="*", help=_("argument of the signal") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 ) |
2038 | 101 |
3040 | 102 async def start(self): |
2038 | 103 args = self.evalArgs() |
104 json_args = json.dumps(args) | |
105 # XXX: we use self.args.profile and not self.profile | |
106 # because we want the raw profile_key (so plugin handle C.PROF_KEY_NONE) | |
3040 | 107 try: |
108 await self.host.bridge.debugFakeSignal(self.args.signal, json_args, self.args.profile) | |
109 except Exception as e: | |
110 self.disp(_(f"Can't send fake signal: {e}"), error=True) | |
111 self.host.quit(C.EXIT_ERROR) | |
112 else: | |
113 self.host.quit() | |
2038 | 114 |
115 | |
116 class Bridge(base.CommandBase): | |
117 subcommands = (Method, Signal) | |
118 | |
119 def __init__(self, host): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 super(Bridge, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
121 host, "bridge", use_profile=False, help=_("bridge s(t)imulation") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
122 ) |
2038 | 123 |
124 | |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
125 class Monitor(base.CommandBase): |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
126 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
127 super(Monitor, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
128 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
129 "monitor", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
130 use_verbose=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
131 use_profile=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 use_output=C.OUTPUT_XML, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
133 help=_("monitor XML stream"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
134 ) |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
135 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
136 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
137 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
138 "-d", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 "--direction", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 choices=("in", "out", "both"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 default="both", |
3028 | 142 help=_("stream direction filter"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
143 ) |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
144 |
3040 | 145 async def printXML(self, direction, xml_data, profile): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 if self.args.direction == "in" and direction != "IN": |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
147 return |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 if self.args.direction == "out" and direction != "OUT": |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
149 return |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
150 verbosity = self.host.verbosity |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
151 if not xml_data.strip(): |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
152 if verbosity <= 2: |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
153 return |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
154 whiteping = True |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
155 else: |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
156 whiteping = False |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
157 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
158 if verbosity: |
3040 | 159 profile_disp = f" ({profile})" if verbosity > 1 else "" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
160 if direction == "IN": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
161 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 A.color( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
163 A.BOLD, A.FG_YELLOW, "<<<===== IN ====", A.FG_WHITE, profile_disp |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 ) |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
166 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
167 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
168 A.color( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 A.BOLD, A.FG_CYAN, "==== OUT ====>>>", A.FG_WHITE, profile_disp |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
171 ) |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
172 if whiteping: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
173 self.disp("[WHITESPACE PING]") |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
174 else: |
2441
e86dc8cb4345
jp (debug/monitor): if data can't be parsed (happen at beginning/end of stream because it's not complete XML), it is printed directly.
Goffi <goffi@goffi.org>
parents:
2417
diff
changeset
|
175 try: |
3040 | 176 await self.output(xml_data) |
2441
e86dc8cb4345
jp (debug/monitor): if data can't be parsed (happen at beginning/end of stream because it's not complete XML), it is printed directly.
Goffi <goffi@goffi.org>
parents:
2417
diff
changeset
|
177 except Exception: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
178 # initial stream is not valid XML, |
2441
e86dc8cb4345
jp (debug/monitor): if data can't be parsed (happen at beginning/end of stream because it's not complete XML), it is printed directly.
Goffi <goffi@goffi.org>
parents:
2417
diff
changeset
|
179 # in this case we print directly to data |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
180 # FIXME: we should test directly lxml.etree.XMLSyntaxError |
2441
e86dc8cb4345
jp (debug/monitor): if data can't be parsed (happen at beginning/end of stream because it's not complete XML), it is printed directly.
Goffi <goffi@goffi.org>
parents:
2417
diff
changeset
|
181 # but importing lxml directly here is not clean |
e86dc8cb4345
jp (debug/monitor): if data can't be parsed (happen at beginning/end of stream because it's not complete XML), it is printed directly.
Goffi <goffi@goffi.org>
parents:
2417
diff
changeset
|
182 # should be wrapped in a custom Exception |
e86dc8cb4345
jp (debug/monitor): if data can't be parsed (happen at beginning/end of stream because it's not complete XML), it is printed directly.
Goffi <goffi@goffi.org>
parents:
2417
diff
changeset
|
183 self.disp(xml_data) |
3028 | 184 self.disp("") |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
185 |
3040 | 186 async def start(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
187 self.host.bridge.register_signal("xmlLog", self.printXML, "plugin") |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
188 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
189 |
3047
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
190 class Theme(base.CommandBase): |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
191 def __init__(self, host): |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
192 base.CommandBase.__init__( |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
193 self, host, "theme", help=_("print colours used with your background") |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
194 ) |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
195 |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
196 def add_parser_options(self): |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
197 pass |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
198 |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
199 async def start(self): |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
200 for attr in dir(C): |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
201 if not attr.startswith('A_'): |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
202 continue |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
203 color = getattr(C, attr) |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
204 if attr == 'A_LEVEL_COLORS': |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
205 # This constant contains multiple colors |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
206 self.disp('LEVEL COLORS: ', no_lf=True) |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
207 for idx, c in enumerate(color): |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
208 last = idx == len(color)-1 |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
209 self.disp(c + f'LEVEL_{idx}' + A.RESET + (', ' if not last else ''), no_lf=not last) |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
210 else: |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
211 text = attr[2:] |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
212 self.disp(A.color(color, text)) |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
213 self.host.quit() |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
214 |
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
215 |
2038 | 216 class Debug(base.CommandBase): |
3047
cf843dd7c345
jp (debug): new "theme" command to print colour theme according to `background` value:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
217 subcommands = (Bridge, Monitor, Theme) |
2038 | 218 |
219 def __init__(self, host): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
220 super(Debug, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
221 host, "debug", use_profile=False, help=_("debugging tools") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
222 ) |