comparison frontends/src/jp/cmd_debug.py @ 2038:3a5badbb443d

jp: added jp/debug commands: jp/debug hierarchy contain commands useful for developping and debugging. first commands are jp/debug/bridge: - "method" allows to call a bridge method, args are parsed as python arguments (using eval). - "signal" do the same to send a signal from backend, using the new debug plugin.
author Goffi <goffi@goffi.org>
date Sun, 21 Aug 2016 12:27:19 +0200
parents
children 456abbceee19
comparison
equal deleted inserted replaced
2037:0f485f1d627d 2038:3a5badbb443d
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
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
24 import json
25
26 __commands__ = ["Debug"]
27
28
29 class BridgeCommon(object):
30
31 def evalArgs(self):
32 if self.args.arg:
33 try:
34 return eval(u",".join(self.args.arg))
35 except SyntaxError as e:
36 self.disp(u"Can't evaluate arguments: {mess}\n{text}\n{offset}^".format(
37 mess=e,
38 text=e.text,
39 offset=u" "*(e.offset-1)
40 ), error=True)
41 self.host.quit(C.EXIT_BAD_ARG)
42 else:
43 return []
44
45
46 class Method(base.CommandBase, BridgeCommon):
47
48 def __init__(self, host):
49 base.CommandBase.__init__(self, host, 'method', help=_(u'call a bridge method'))
50 BridgeCommon.__init__(self)
51 self.need_loop=True
52
53 def add_parser_options(self):
54 self.parser.add_argument("method", type=str, help=_(u"name of the method to execute"))
55 self.parser.add_argument("arg", nargs="*", help=_(u"argument of the method"))
56
57 def method_cb(self, ret):
58 self.disp(unicode(ret))
59 self.host.quit()
60
61 def method_eb(self, failure):
62 self.disp(_(u"Error while executing {}: {}".format(self.args.method, failure)), error=True)
63 self.host.quit(C.EXIT_ERROR)
64
65 def start(self):
66 method = getattr(self.host.bridge, self.args.method)
67 args = self.evalArgs()
68 try:
69 method(*self.args.arg, profile=self.profile, callback=self.method_cb, errback=self.method_eb)
70 except TypeError:
71 # maybe the method doesn't need a profile ?
72 try:
73 method(*args, callback=self.method_cb, errback=self.method_eb)
74 except TypeError:
75 self.method_eb(_(u"bad arguments"))
76
77
78 class Signal(base.CommandBase, BridgeCommon):
79
80 def __init__(self, host):
81 base.CommandBase.__init__(self, host, 'signal', help=_(u'send a fake signal from backend'))
82 BridgeCommon.__init__(self)
83
84 def add_parser_options(self):
85 self.parser.add_argument("signal", type=str, help=_(u"name of the signal to send"))
86 self.parser.add_argument("arg", nargs="*", help=_(u"argument of the signal"))
87
88 def start(self):
89 args = self.evalArgs()
90 json_args = json.dumps(args)
91 # XXX: we use self.args.profile and not self.profile
92 # because we want the raw profile_key (so plugin handle C.PROF_KEY_NONE)
93 self.host.bridge.debugFakeSignal(self.args.signal, json_args, self.args.profile)
94
95
96 class Bridge(base.CommandBase):
97 subcommands = (Method, Signal)
98
99 def __init__(self, host):
100 super(Bridge, self).__init__(host, 'bridge', use_profile=False, help=_('bridge s(t)imulation'))
101
102
103 class Debug(base.CommandBase):
104 subcommands = (Bridge,)
105
106 def __init__(self, host):
107 super(Debug, self).__init__(host, 'debug', use_profile=False, help=_('debugging tools'))