Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_debug.py @ 2740:8fd8ce5a5855
jp (message/send, encryption): encryption handling:
- encryption algorithm can now be requested when sending a message (using --encrypt option)
- new encryption commands to (de)activate encryption session, check available algorithms, or manage trust.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 02 Jan 2019 18:50:57 +0100 |
parents | 56f94936df1e |
children | 003b8b4b56a7 |
rev | line source |
---|---|
2038 | 1 #!/usr/bin/env python2 |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # jp: a SàT command line tool | |
2483 | 5 # Copyright (C) 2009-2018 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 | |
21 import base | |
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: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
34 return eval(u"[{}]".format(u",".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( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
37 u"Can't evaluate arguments: {mess}\n{text}\n{offset}^".format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
38 mess=e, text=e.text.decode("utf-8"), offset=u" " * (e.offset - 1) |
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): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
49 base.CommandBase.__init__(self, host, "method", help=_(u"call a bridge method")) |
2038 | 50 BridgeCommon.__init__(self) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 self.need_loop = True |
2038 | 52 |
53 def add_parser_options(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 "method", type=str, help=_(u"name of the method to execute") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
57 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 "arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the method") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 ) |
2038 | 60 |
2053
a3c2866841f7
jp (debug): method callback now handles methods without return value
Goffi <goffi@goffi.org>
parents:
2041
diff
changeset
|
61 def method_cb(self, ret=None): |
a3c2866841f7
jp (debug): method callback now handles methods without return value
Goffi <goffi@goffi.org>
parents:
2041
diff
changeset
|
62 if ret is not None: |
a3c2866841f7
jp (debug): method callback now handles methods without return value
Goffi <goffi@goffi.org>
parents:
2041
diff
changeset
|
63 self.disp(unicode(ret)) |
2038 | 64 self.host.quit() |
65 | |
66 def method_eb(self, failure): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
68 _(u"Error while executing {}: {}".format(self.args.method, failure)), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
69 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 ) |
2038 | 71 self.host.quit(C.EXIT_ERROR) |
72 | |
73 def start(self): | |
74 method = getattr(self.host.bridge, self.args.method) | |
75 args = self.evalArgs() | |
76 try: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 method( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 *args, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 profile=self.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 callback=self.method_cb, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 errback=self.method_eb |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 ) |
2038 | 83 except TypeError: |
84 # maybe the method doesn't need a profile ? | |
85 try: | |
86 method(*args, callback=self.method_cb, errback=self.method_eb) | |
87 except TypeError: | |
88 self.method_eb(_(u"bad arguments")) | |
89 | |
90 | |
91 class Signal(base.CommandBase, BridgeCommon): | |
92 def __init__(self, host): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
93 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 self, host, "signal", help=_(u"send a fake signal from backend") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 ) |
2038 | 96 BridgeCommon.__init__(self) |
97 | |
98 def add_parser_options(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 "signal", type=str, help=_(u"name of the signal to send") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 "arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the signal") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 ) |
2038 | 105 |
106 def start(self): | |
107 args = self.evalArgs() | |
108 json_args = json.dumps(args) | |
109 # XXX: we use self.args.profile and not self.profile | |
110 # because we want the raw profile_key (so plugin handle C.PROF_KEY_NONE) | |
111 self.host.bridge.debugFakeSignal(self.args.signal, json_args, self.args.profile) | |
112 | |
113 | |
114 class Bridge(base.CommandBase): | |
115 subcommands = (Method, Signal) | |
116 | |
117 def __init__(self, host): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 super(Bridge, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
119 host, "bridge", use_profile=False, help=_("bridge s(t)imulation") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 ) |
2038 | 121 |
122 | |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
123 class Monitor(base.CommandBase): |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
124 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
125 super(Monitor, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
126 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
127 "monitor", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
128 use_verbose=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
129 use_profile=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
130 use_output=C.OUTPUT_XML, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
131 help=_("monitor XML stream"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 ) |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
133 self.need_loop = True |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
134 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
135 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
136 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
137 "-d", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
138 "--direction", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 choices=("in", "out", "both"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 default="both", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 help=_(u"stream direction filter"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 ) |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
143 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
144 def printXML(self, direction, xml_data, profile): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 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
|
146 return |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 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
|
148 return |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
149 verbosity = self.host.verbosity |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
150 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
|
151 if verbosity <= 2: |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
152 return |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
153 whiteping = True |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
154 else: |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
155 whiteping = False |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
156 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
157 if verbosity: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
158 profile_disp = u" ({})".format(profile) if verbosity > 1 else u"" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
159 if direction == "IN": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
160 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
161 A.color( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 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
|
163 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 ) |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
165 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
166 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
167 A.color( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
168 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
|
169 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 ) |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
171 if whiteping: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
172 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
|
173 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
|
174 try: |
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 self.output(xml_data) |
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
|
176 except Exception: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
177 # 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
|
178 # in this case we print directly to data |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
179 # 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
|
180 # 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
|
181 # 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
|
182 self.disp(xml_data) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
183 self.disp(u"") |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
184 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
185 def start(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
186 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
|
187 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
188 |
2038 | 189 class Debug(base.CommandBase): |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
190 subcommands = (Bridge, Monitor) |
2038 | 191 |
192 def __init__(self, host): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
193 super(Debug, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
194 host, "debug", use_profile=False, help=_("debugging tools") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
195 ) |