comparison sat_frontends/jp/cmd_debug.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
26 26
27 __commands__ = ["Debug"] 27 __commands__ = ["Debug"]
28 28
29 29
30 class BridgeCommon(object): 30 class BridgeCommon(object):
31
32 def evalArgs(self): 31 def evalArgs(self):
33 if self.args.arg: 32 if self.args.arg:
34 try: 33 try:
35 return eval(u'[{}]'.format(u",".join(self.args.arg))) 34 return eval(u"[{}]".format(u",".join(self.args.arg)))
36 except SyntaxError as e: 35 except SyntaxError as e:
37 self.disp(u"Can't evaluate arguments: {mess}\n{text}\n{offset}^".format( 36 self.disp(
38 mess=e, 37 u"Can't evaluate arguments: {mess}\n{text}\n{offset}^".format(
39 text=e.text.decode('utf-8'), 38 mess=e, text=e.text.decode("utf-8"), offset=u" " * (e.offset - 1)
40 offset=u" "*(e.offset-1) 39 ),
41 ), error=True) 40 error=True,
41 )
42 self.host.quit(C.EXIT_BAD_ARG) 42 self.host.quit(C.EXIT_BAD_ARG)
43 else: 43 else:
44 return [] 44 return []
45 45
46 46
47 class Method(base.CommandBase, BridgeCommon): 47 class Method(base.CommandBase, BridgeCommon):
48
49 def __init__(self, host): 48 def __init__(self, host):
50 base.CommandBase.__init__(self, host, 'method', help=_(u'call a bridge method')) 49 base.CommandBase.__init__(self, host, "method", help=_(u"call a bridge method"))
51 BridgeCommon.__init__(self) 50 BridgeCommon.__init__(self)
52 self.need_loop=True 51 self.need_loop = True
53 52
54 def add_parser_options(self): 53 def add_parser_options(self):
55 self.parser.add_argument("method", type=str, help=_(u"name of the method to execute")) 54 self.parser.add_argument(
56 self.parser.add_argument("arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the method")) 55 "method", type=str, help=_(u"name of the method to execute")
56 )
57 self.parser.add_argument(
58 "arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the method")
59 )
57 60
58 def method_cb(self, ret=None): 61 def method_cb(self, ret=None):
59 if ret is not None: 62 if ret is not None:
60 self.disp(unicode(ret)) 63 self.disp(unicode(ret))
61 self.host.quit() 64 self.host.quit()
62 65
63 def method_eb(self, failure): 66 def method_eb(self, failure):
64 self.disp(_(u"Error while executing {}: {}".format(self.args.method, failure)), error=True) 67 self.disp(
68 _(u"Error while executing {}: {}".format(self.args.method, failure)),
69 error=True,
70 )
65 self.host.quit(C.EXIT_ERROR) 71 self.host.quit(C.EXIT_ERROR)
66 72
67 def start(self): 73 def start(self):
68 method = getattr(self.host.bridge, self.args.method) 74 method = getattr(self.host.bridge, self.args.method)
69 args = self.evalArgs() 75 args = self.evalArgs()
70 try: 76 try:
71 method(*args, profile=self.profile, callback=self.method_cb, errback=self.method_eb) 77 method(
78 *args,
79 profile=self.profile,
80 callback=self.method_cb,
81 errback=self.method_eb
82 )
72 except TypeError: 83 except TypeError:
73 # maybe the method doesn't need a profile ? 84 # maybe the method doesn't need a profile ?
74 try: 85 try:
75 method(*args, callback=self.method_cb, errback=self.method_eb) 86 method(*args, callback=self.method_cb, errback=self.method_eb)
76 except TypeError: 87 except TypeError:
77 self.method_eb(_(u"bad arguments")) 88 self.method_eb(_(u"bad arguments"))
78 89
79 90
80 class Signal(base.CommandBase, BridgeCommon): 91 class Signal(base.CommandBase, BridgeCommon):
81
82 def __init__(self, host): 92 def __init__(self, host):
83 base.CommandBase.__init__(self, host, 'signal', help=_(u'send a fake signal from backend')) 93 base.CommandBase.__init__(
94 self, host, "signal", help=_(u"send a fake signal from backend")
95 )
84 BridgeCommon.__init__(self) 96 BridgeCommon.__init__(self)
85 97
86 def add_parser_options(self): 98 def add_parser_options(self):
87 self.parser.add_argument("signal", type=str, help=_(u"name of the signal to send")) 99 self.parser.add_argument(
88 self.parser.add_argument("arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the signal")) 100 "signal", type=str, help=_(u"name of the signal to send")
101 )
102 self.parser.add_argument(
103 "arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the signal")
104 )
89 105
90 def start(self): 106 def start(self):
91 args = self.evalArgs() 107 args = self.evalArgs()
92 json_args = json.dumps(args) 108 json_args = json.dumps(args)
93 # XXX: we use self.args.profile and not self.profile 109 # XXX: we use self.args.profile and not self.profile
97 113
98 class Bridge(base.CommandBase): 114 class Bridge(base.CommandBase):
99 subcommands = (Method, Signal) 115 subcommands = (Method, Signal)
100 116
101 def __init__(self, host): 117 def __init__(self, host):
102 super(Bridge, self).__init__(host, 'bridge', use_profile=False, help=_('bridge s(t)imulation')) 118 super(Bridge, self).__init__(
119 host, "bridge", use_profile=False, help=_("bridge s(t)imulation")
120 )
103 121
104 122
105 class Monitor(base.CommandBase): 123 class Monitor(base.CommandBase):
106
107 def __init__(self, host): 124 def __init__(self, host):
108 super(Monitor, self).__init__(host, 125 super(Monitor, self).__init__(
109 'monitor', 126 host,
110 use_verbose=True, 127 "monitor",
111 use_profile=False, 128 use_verbose=True,
112 use_output=C.OUTPUT_XML, 129 use_profile=False,
113 help=_('monitor XML stream')) 130 use_output=C.OUTPUT_XML,
131 help=_("monitor XML stream"),
132 )
114 self.need_loop = True 133 self.need_loop = True
115 134
116 def add_parser_options(self): 135 def add_parser_options(self):
117 self.parser.add_argument("-d", "--direction", choices=('in', 'out', 'both'), default='both', help=_(u"stream direction filter")) 136 self.parser.add_argument(
137 "-d",
138 "--direction",
139 choices=("in", "out", "both"),
140 default="both",
141 help=_(u"stream direction filter"),
142 )
118 143
119 def printXML(self, direction, xml_data, profile): 144 def printXML(self, direction, xml_data, profile):
120 if self.args.direction == 'in' and direction != 'IN': 145 if self.args.direction == "in" and direction != "IN":
121 return 146 return
122 if self.args.direction == 'out' and direction != 'OUT': 147 if self.args.direction == "out" and direction != "OUT":
123 return 148 return
124 verbosity = self.host.verbosity 149 verbosity = self.host.verbosity
125 if not xml_data.strip(): 150 if not xml_data.strip():
126 if verbosity <= 2: 151 if verbosity <= 2:
127 return 152 return
128 whiteping = True 153 whiteping = True
129 else: 154 else:
130 whiteping = False 155 whiteping = False
131 156
132 if verbosity: 157 if verbosity:
133 profile_disp = u' ({})'.format(profile) if verbosity>1 else u'' 158 profile_disp = u" ({})".format(profile) if verbosity > 1 else u""
134 if direction == 'IN': 159 if direction == "IN":
135 self.disp(A.color(A.BOLD, A.FG_YELLOW, '<<<===== IN ====', A.FG_WHITE, profile_disp)) 160 self.disp(
161 A.color(
162 A.BOLD, A.FG_YELLOW, "<<<===== IN ====", A.FG_WHITE, profile_disp
163 )
164 )
136 else: 165 else:
137 self.disp(A.color(A.BOLD, A.FG_CYAN, '==== OUT ====>>>', A.FG_WHITE, profile_disp)) 166 self.disp(
167 A.color(
168 A.BOLD, A.FG_CYAN, "==== OUT ====>>>", A.FG_WHITE, profile_disp
169 )
170 )
138 if whiteping: 171 if whiteping:
139 self.disp('[WHITESPACE PING]') 172 self.disp("[WHITESPACE PING]")
140 else: 173 else:
141 try: 174 try:
142 self.output(xml_data) 175 self.output(xml_data)
143 except Exception: 176 except Exception:
144 # initial stream is not valid XML, 177 #  initial stream is not valid XML,
145 # in this case we print directly to data 178 # in this case we print directly to data
146 # FIXME: we should test directly lxml.etree.XMLSyntaxError 179 #  FIXME: we should test directly lxml.etree.XMLSyntaxError
147 # but importing lxml directly here is not clean 180 # but importing lxml directly here is not clean
148 # should be wrapped in a custom Exception 181 # should be wrapped in a custom Exception
149 self.disp(xml_data) 182 self.disp(xml_data)
150 self.disp(u'') 183 self.disp(u"")
151 184
152 def start(self): 185 def start(self):
153 self.host.bridge.register_signal('xmlLog', self.printXML, 'plugin') 186 self.host.bridge.register_signal("xmlLog", self.printXML, "plugin")
154 187
155 188
156 class Debug(base.CommandBase): 189 class Debug(base.CommandBase):
157 subcommands = (Bridge, Monitor) 190 subcommands = (Bridge, Monitor)
158 191
159 def __init__(self, host): 192 def __init__(self, host):
160 super(Debug, self).__init__(host, 'debug', use_profile=False, help=_('debugging tools')) 193 super(Debug, self).__init__(
194 host, "debug", use_profile=False, help=_("debugging tools")
195 )