Mercurial > libervia-backend
annotate frontends/src/jp/cmd_debug.py @ 2504:67cc54b01a12
plugin file sharing component: first draft:
this component act as an endpoint where a user can store and retrieve its files using Jingle.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 28 Feb 2018 18:28:39 +0100 |
parents | 0046283a285d |
children |
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 | |
32 def evalArgs(self): | |
33 if self.args.arg: | |
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 | 36 except SyntaxError as e: |
37 self.disp(u"Can't evaluate arguments: {mess}\n{text}\n{offset}^".format( | |
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 | 40 offset=u" "*(e.offset-1) |
41 ), error=True) | |
42 self.host.quit(C.EXIT_BAD_ARG) | |
43 else: | |
44 return [] | |
45 | |
46 | |
47 class Method(base.CommandBase, BridgeCommon): | |
48 | |
49 def __init__(self, host): | |
50 base.CommandBase.__init__(self, host, 'method', help=_(u'call a bridge method')) | |
51 BridgeCommon.__init__(self) | |
52 self.need_loop=True | |
53 | |
54 def add_parser_options(self): | |
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 | 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 | 61 self.host.quit() |
62 | |
63 def method_eb(self, failure): | |
64 self.disp(_(u"Error while executing {}: {}".format(self.args.method, failure)), error=True) | |
65 self.host.quit(C.EXIT_ERROR) | |
66 | |
67 def start(self): | |
68 method = getattr(self.host.bridge, self.args.method) | |
69 args = self.evalArgs() | |
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 | 72 except TypeError: |
73 # maybe the method doesn't need a profile ? | |
74 try: | |
75 method(*args, callback=self.method_cb, errback=self.method_eb) | |
76 except TypeError: | |
77 self.method_eb(_(u"bad arguments")) | |
78 | |
79 | |
80 class Signal(base.CommandBase, BridgeCommon): | |
81 | |
82 def __init__(self, host): | |
83 base.CommandBase.__init__(self, host, 'signal', help=_(u'send a fake signal from backend')) | |
84 BridgeCommon.__init__(self) | |
85 | |
86 def add_parser_options(self): | |
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 | 89 |
90 def start(self): | |
91 args = self.evalArgs() | |
92 json_args = json.dumps(args) | |
93 # XXX: we use self.args.profile and not self.profile | |
94 # because we want the raw profile_key (so plugin handle C.PROF_KEY_NONE) | |
95 self.host.bridge.debugFakeSignal(self.args.signal, json_args, self.args.profile) | |
96 | |
97 | |
98 class Bridge(base.CommandBase): | |
99 subcommands = (Method, Signal) | |
100 | |
101 def __init__(self, host): | |
102 super(Bridge, self).__init__(host, 'bridge', use_profile=False, help=_('bridge s(t)imulation')) | |
103 | |
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: |
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
|
141 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
|
142 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
|
143 except 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
|
144 # initial stream is not valid XML, |
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
|
145 # in this case we print directly to 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
|
146 # FIXME: we should test directly lxml.etree.XMLSyntaxError |
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
|
147 # 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
|
148 # 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
|
149 self.disp(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
|
150 self.disp(u'') |
2417
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
151 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
152 def start(self): |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
153 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
|
154 |
192ae573901a
jp (debug): new monitor command to show pretty formatted XML stream
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
155 |
2038 | 156 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
|
157 subcommands = (Bridge, Monitor) |
2038 | 158 |
159 def __init__(self, host): | |
160 super(Debug, self).__init__(host, 'debug', use_profile=False, help=_('debugging tools')) |