annotate frontends/src/jp/cmd_debug.py @ 2417:192ae573901a

jp (debug): new monitor command to show pretty formatted XML stream
author Goffi <goffi@goffi.org>
date Sat, 04 Nov 2017 22:11:45 +0100
parents 8b37a62336c3
children e86dc8cb4345
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SàT command line tool
2414
8b37a62336c3 misc: date update (yes it's a bit late :p )
Goffi <goffi@goffi.org>
parents: 2068
diff changeset
5 # Copyright (C) 2009-2017 Jérôme Poisson (goffi@goffi.org)
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import base
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.i18n import _
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import json
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 __commands__ = ["Debug"]
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class BridgeCommon(object):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 def evalArgs(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 if self.args.arg:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 try:
2068
741db5abf077 jp (debug/bridge/method,signal): fixed argument parsing
Goffi <goffi@goffi.org>
parents: 2053
diff changeset
35 return eval(u'[{}]'.format(u",".join(self.args.arg)))
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 except SyntaxError as e:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.disp(u"Can't evaluate arguments: {mess}\n{text}\n{offset}^".format(
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 mess=e,
2041
456abbceee19 jp (debug/bridge): fixed unicode handling of arguments
Goffi <goffi@goffi.org>
parents: 2038
diff changeset
39 text=e.text.decode('utf-8'),
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 offset=u" "*(e.offset-1)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 ), error=True)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.host.quit(C.EXIT_BAD_ARG)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 else:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 return []
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 class Method(base.CommandBase, BridgeCommon):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def __init__(self, host):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 base.CommandBase.__init__(self, host, 'method', help=_(u'call a bridge method'))
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 BridgeCommon.__init__(self)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 self.need_loop=True
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def add_parser_options(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.parser.add_argument("method", type=str, help=_(u"name of the method to execute"))
2041
456abbceee19 jp (debug/bridge): fixed unicode handling of arguments
Goffi <goffi@goffi.org>
parents: 2038
diff changeset
56 self.parser.add_argument("arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the method"))
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
2053
a3c2866841f7 jp (debug): method callback now handles methods without return value
Goffi <goffi@goffi.org>
parents: 2041
diff changeset
58 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
59 if ret is not None:
a3c2866841f7 jp (debug): method callback now handles methods without return value
Goffi <goffi@goffi.org>
parents: 2041
diff changeset
60 self.disp(unicode(ret))
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.host.quit()
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def method_eb(self, failure):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.disp(_(u"Error while executing {}: {}".format(self.args.method, failure)), error=True)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.host.quit(C.EXIT_ERROR)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def start(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 method = getattr(self.host.bridge, self.args.method)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 args = self.evalArgs()
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 try:
2068
741db5abf077 jp (debug/bridge/method,signal): fixed argument parsing
Goffi <goffi@goffi.org>
parents: 2053
diff changeset
71 method(*args, profile=self.profile, callback=self.method_cb, errback=self.method_eb)
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 except TypeError:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 # maybe the method doesn't need a profile ?
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 try:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 method(*args, callback=self.method_cb, errback=self.method_eb)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 except TypeError:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.method_eb(_(u"bad arguments"))
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 class Signal(base.CommandBase, BridgeCommon):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def __init__(self, host):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 base.CommandBase.__init__(self, host, 'signal', help=_(u'send a fake signal from backend'))
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 BridgeCommon.__init__(self)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def add_parser_options(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.parser.add_argument("signal", type=str, help=_(u"name of the signal to send"))
2041
456abbceee19 jp (debug/bridge): fixed unicode handling of arguments
Goffi <goffi@goffi.org>
parents: 2038
diff changeset
88 self.parser.add_argument("arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the signal"))
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 def start(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 args = self.evalArgs()
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 json_args = json.dumps(args)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 # XXX: we use self.args.profile and not self.profile
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 # because we want the raw profile_key (so plugin handle C.PROF_KEY_NONE)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 self.host.bridge.debugFakeSignal(self.args.signal, json_args, self.args.profile)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 class Bridge(base.CommandBase):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 subcommands = (Method, Signal)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 def __init__(self, host):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 super(Bridge, self).__init__(host, 'bridge', use_profile=False, help=_('bridge s(t)imulation'))
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
103
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
2417
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
105 class Monitor(base.CommandBase):
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
106
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
107 def __init__(self, host):
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
108 super(Monitor, self).__init__(host,
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
109 'monitor',
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
110 use_verbose=True,
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
111 use_profile=False,
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
112 use_output=C.OUTPUT_XML,
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
113 help=_('monitor XML stream'))
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
114 self.need_loop = True
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
115
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
116 def add_parser_options(self):
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
117 self.parser.add_argument("-d", "--direction", choices=('in', 'out', 'both'), default='both', help=_(u"stream direction filter"))
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
118
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
119 def printXML(self, direction, xml_data, profile):
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
120 if self.args.direction == 'in' and direction != 'IN':
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
121 return
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
122 if self.args.direction == 'out' and direction != 'OUT':
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
123 return
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
124 verbosity = self.host.verbosity
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
125 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
126 if verbosity <= 2:
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
127 return
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
128 whiteping = True
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
129 else:
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
130 whiteping = False
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
131
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
132 if verbosity:
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
133 profile_disp = u' ({})'.format(profile) if verbosity>1 else u''
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
134 if direction == 'IN':
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
135 self.disp(A.color(A.BOLD, A.FG_YELLOW, '<<<===== IN ====', A.FG_WHITE, profile_disp))
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
136 else:
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
137 self.disp(A.color(A.BOLD, A.FG_CYAN, '==== OUT ====>>>', A.FG_WHITE, profile_disp))
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
138 if whiteping:
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
139 self.disp('[WHITESPACE PING]')
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
140 else:
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
141 self.output(xml_data)
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
142
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
143 def start(self):
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
144 self.host.bridge.register_signal('xmlLog', self.printXML, 'plugin')
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
145
192ae573901a jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
146
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 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
148 subcommands = (Bridge, Monitor)
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 def __init__(self, host):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 super(Debug, self).__init__(host, 'debug', use_profile=False, help=_('debugging tools'))