annotate frontends/src/jp/cmd_debug.py @ 2041:456abbceee19

jp (debug/bridge): fixed unicode handling of arguments
author Goffi <goffi@goffi.org>
date Sun, 21 Aug 2016 18:57:56 +0200
parents 3a5badbb443d
children a3c2866841f7
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
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
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
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import json
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 __commands__ = ["Debug"]
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
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 class BridgeCommon(object):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 def evalArgs(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 if self.args.arg:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 try:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 return eval(u",".join(self.args.arg))
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 except SyntaxError as e:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 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
37 mess=e,
2041
456abbceee19 jp (debug/bridge): fixed unicode handling of arguments
Goffi <goffi@goffi.org>
parents: 2038
diff changeset
38 text=e.text.decode('utf-8'),
2038
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 offset=u" "*(e.offset-1)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 ), error=True)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.host.quit(C.EXIT_BAD_ARG)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 else:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 return []
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
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 class Method(base.CommandBase, BridgeCommon):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def __init__(self, host):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 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
50 BridgeCommon.__init__(self)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.need_loop=True
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def add_parser_options(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 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
55 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
56
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def method_cb(self, ret):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.disp(unicode(ret))
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.host.quit()
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def method_eb(self, failure):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 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
63 self.host.quit(C.EXIT_ERROR)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def start(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 method = getattr(self.host.bridge, self.args.method)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 args = self.evalArgs()
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 try:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 method(*self.args.arg, profile=self.profile, callback=self.method_cb, errback=self.method_eb)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 except TypeError:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 # maybe the method doesn't need a profile ?
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 try:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 method(*args, callback=self.method_cb, errback=self.method_eb)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 except TypeError:
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.method_eb(_(u"bad arguments"))
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 class Signal(base.CommandBase, BridgeCommon):
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 def __init__(self, host):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 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
82 BridgeCommon.__init__(self)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def add_parser_options(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 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
86 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
87
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def start(self):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 args = self.evalArgs()
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 json_args = json.dumps(args)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 # XXX: we use self.args.profile and not self.profile
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 # 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
93 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
94
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 class Bridge(base.CommandBase):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 subcommands = (Method, Signal)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def __init__(self, host):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 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
101
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 class Debug(base.CommandBase):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 subcommands = (Bridge,)
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def __init__(self, host):
3a5badbb443d jp: added jp/debug commands:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 super(Debug, self).__init__(host, 'debug', use_profile=False, help=_('debugging tools'))