Mercurial > libervia-backend
annotate frontends/src/jp/cmd_debug.py @ 2112:877d0a2d0d86
jp (constants): added exit code for error on bridge method call
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 05 Jan 2017 20:37:01 +0100 |
parents | 741db5abf077 |
children | 8b37a62336c3 |
rev | line source |
---|---|
2038 | 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: | |
2068
741db5abf077
jp (debug/bridge/method,signal): fixed argument parsing
Goffi <goffi@goffi.org>
parents:
2053
diff
changeset
|
34 return eval(u'[{}]'.format(u",".join(self.args.arg))) |
2038 | 35 except SyntaxError as e: |
36 self.disp(u"Can't evaluate arguments: {mess}\n{text}\n{offset}^".format( | |
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 | 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")) | |
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 | 56 |
2053
a3c2866841f7
jp (debug): method callback now handles methods without return value
Goffi <goffi@goffi.org>
parents:
2041
diff
changeset
|
57 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
|
58 if ret is not None: |
a3c2866841f7
jp (debug): method callback now handles methods without return value
Goffi <goffi@goffi.org>
parents:
2041
diff
changeset
|
59 self.disp(unicode(ret)) |
2038 | 60 self.host.quit() |
61 | |
62 def method_eb(self, failure): | |
63 self.disp(_(u"Error while executing {}: {}".format(self.args.method, failure)), error=True) | |
64 self.host.quit(C.EXIT_ERROR) | |
65 | |
66 def start(self): | |
67 method = getattr(self.host.bridge, self.args.method) | |
68 args = self.evalArgs() | |
69 try: | |
2068
741db5abf077
jp (debug/bridge/method,signal): fixed argument parsing
Goffi <goffi@goffi.org>
parents:
2053
diff
changeset
|
70 method(*args, profile=self.profile, callback=self.method_cb, errback=self.method_eb) |
2038 | 71 except TypeError: |
72 # maybe the method doesn't need a profile ? | |
73 try: | |
74 method(*args, callback=self.method_cb, errback=self.method_eb) | |
75 except TypeError: | |
76 self.method_eb(_(u"bad arguments")) | |
77 | |
78 | |
79 class Signal(base.CommandBase, BridgeCommon): | |
80 | |
81 def __init__(self, host): | |
82 base.CommandBase.__init__(self, host, 'signal', help=_(u'send a fake signal from backend')) | |
83 BridgeCommon.__init__(self) | |
84 | |
85 def add_parser_options(self): | |
86 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
|
87 self.parser.add_argument("arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the signal")) |
2038 | 88 |
89 def start(self): | |
90 args = self.evalArgs() | |
91 json_args = json.dumps(args) | |
92 # XXX: we use self.args.profile and not self.profile | |
93 # because we want the raw profile_key (so plugin handle C.PROF_KEY_NONE) | |
94 self.host.bridge.debugFakeSignal(self.args.signal, json_args, self.args.profile) | |
95 | |
96 | |
97 class Bridge(base.CommandBase): | |
98 subcommands = (Method, Signal) | |
99 | |
100 def __init__(self, host): | |
101 super(Bridge, self).__init__(host, 'bridge', use_profile=False, help=_('bridge s(t)imulation')) | |
102 | |
103 | |
104 class Debug(base.CommandBase): | |
105 subcommands = (Bridge,) | |
106 | |
107 def __init__(self, host): | |
108 super(Debug, self).__init__(host, 'debug', use_profile=False, help=_('debugging tools')) |