Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_debug.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 003b8b4b56a7 |
children | fee60f17ebac |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 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/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 | 20 |
21 import base | 21 from . import base |
22 from sat.core.i18n import _ | 22 from sat.core.i18n import _ |
23 from sat_frontends.jp.constants import Const as C | 23 from sat_frontends.jp.constants import Const as C |
24 from sat.tools.common.ansi import ANSI as A | 24 from sat.tools.common.ansi import ANSI as A |
25 import json | 25 import json |
26 | 26 |
29 | 29 |
30 class BridgeCommon(object): | 30 class BridgeCommon(object): |
31 def evalArgs(self): | 31 def evalArgs(self): |
32 if self.args.arg: | 32 if self.args.arg: |
33 try: | 33 try: |
34 return eval(u"[{}]".format(u",".join(self.args.arg))) | 34 return eval("[{}]".format(",".join(self.args.arg))) |
35 except SyntaxError as e: | 35 except SyntaxError as e: |
36 self.disp( | 36 self.disp( |
37 u"Can't evaluate arguments: {mess}\n{text}\n{offset}^".format( | 37 "Can't evaluate arguments: {mess}\n{text}\n{offset}^".format( |
38 mess=e, text=e.text.decode("utf-8"), offset=u" " * (e.offset - 1) | 38 mess=e, text=e.text, offset=" " * (e.offset - 1) |
39 ), | 39 ), |
40 error=True, | 40 error=True, |
41 ) | 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 def __init__(self, host): | 48 def __init__(self, host): |
49 base.CommandBase.__init__(self, host, "method", help=_(u"call a bridge method")) | 49 base.CommandBase.__init__(self, host, "method", help=_("call a bridge method")) |
50 BridgeCommon.__init__(self) | 50 BridgeCommon.__init__(self) |
51 self.need_loop = True | 51 self.need_loop = True |
52 | 52 |
53 def add_parser_options(self): | 53 def add_parser_options(self): |
54 self.parser.add_argument( | 54 self.parser.add_argument( |
55 "method", type=str, help=_(u"name of the method to execute") | 55 "method", type=str, help=_("name of the method to execute") |
56 ) | 56 ) |
57 self.parser.add_argument( | 57 self.parser.add_argument( |
58 "arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the method") | 58 "arg", nargs="*", help=_("argument of the method") |
59 ) | 59 ) |
60 | 60 |
61 def method_cb(self, ret=None): | 61 def method_cb(self, ret=None): |
62 if ret is not None: | 62 if ret is not None: |
63 self.disp(unicode(ret)) | 63 self.disp(str(ret)) |
64 self.host.quit() | 64 self.host.quit() |
65 | 65 |
66 def method_eb(self, failure): | 66 def method_eb(self, failure): |
67 self.disp( | 67 self.disp( |
68 _(u"Error while executing {}: {}".format(self.args.method, failure)), | 68 _("Error while executing {}: {}".format(self.args.method, failure)), |
69 error=True, | 69 error=True, |
70 ) | 70 ) |
71 self.host.quit(C.EXIT_ERROR) | 71 self.host.quit(C.EXIT_ERROR) |
72 | 72 |
73 def start(self): | 73 def start(self): |
83 except TypeError: | 83 except TypeError: |
84 # maybe the method doesn't need a profile ? | 84 # maybe the method doesn't need a profile ? |
85 try: | 85 try: |
86 method(*args, callback=self.method_cb, errback=self.method_eb) | 86 method(*args, callback=self.method_cb, errback=self.method_eb) |
87 except TypeError: | 87 except TypeError: |
88 self.method_eb(_(u"bad arguments")) | 88 self.method_eb(_("bad arguments")) |
89 | 89 |
90 | 90 |
91 class Signal(base.CommandBase, BridgeCommon): | 91 class Signal(base.CommandBase, BridgeCommon): |
92 def __init__(self, host): | 92 def __init__(self, host): |
93 base.CommandBase.__init__( | 93 base.CommandBase.__init__( |
94 self, host, "signal", help=_(u"send a fake signal from backend") | 94 self, host, "signal", help=_("send a fake signal from backend") |
95 ) | 95 ) |
96 BridgeCommon.__init__(self) | 96 BridgeCommon.__init__(self) |
97 | 97 |
98 def add_parser_options(self): | 98 def add_parser_options(self): |
99 self.parser.add_argument( | 99 self.parser.add_argument( |
100 "signal", type=str, help=_(u"name of the signal to send") | 100 "signal", type=str, help=_("name of the signal to send") |
101 ) | 101 ) |
102 self.parser.add_argument( | 102 self.parser.add_argument( |
103 "arg", type=base.unicode_decoder, nargs="*", help=_(u"argument of the signal") | 103 "arg", nargs="*", help=_("argument of the signal") |
104 ) | 104 ) |
105 | 105 |
106 def start(self): | 106 def start(self): |
107 args = self.evalArgs() | 107 args = self.evalArgs() |
108 json_args = json.dumps(args) | 108 json_args = json.dumps(args) |
136 self.parser.add_argument( | 136 self.parser.add_argument( |
137 "-d", | 137 "-d", |
138 "--direction", | 138 "--direction", |
139 choices=("in", "out", "both"), | 139 choices=("in", "out", "both"), |
140 default="both", | 140 default="both", |
141 help=_(u"stream direction filter"), | 141 help=_("stream direction filter"), |
142 ) | 142 ) |
143 | 143 |
144 def printXML(self, direction, xml_data, profile): | 144 def printXML(self, direction, xml_data, profile): |
145 if self.args.direction == "in" and direction != "IN": | 145 if self.args.direction == "in" and direction != "IN": |
146 return | 146 return |
153 whiteping = True | 153 whiteping = True |
154 else: | 154 else: |
155 whiteping = False | 155 whiteping = False |
156 | 156 |
157 if verbosity: | 157 if verbosity: |
158 profile_disp = u" ({})".format(profile) if verbosity > 1 else u"" | 158 profile_disp = " ({})".format(profile) if verbosity > 1 else "" |
159 if direction == "IN": | 159 if direction == "IN": |
160 self.disp( | 160 self.disp( |
161 A.color( | 161 A.color( |
162 A.BOLD, A.FG_YELLOW, "<<<===== IN ====", A.FG_WHITE, profile_disp | 162 A.BOLD, A.FG_YELLOW, "<<<===== IN ====", A.FG_WHITE, profile_disp |
163 ) | 163 ) |
178 # in this case we print directly to data | 178 # in this case we print directly to data |
179 # FIXME: we should test directly lxml.etree.XMLSyntaxError | 179 # FIXME: we should test directly lxml.etree.XMLSyntaxError |
180 # but importing lxml directly here is not clean | 180 # but importing lxml directly here is not clean |
181 # should be wrapped in a custom Exception | 181 # should be wrapped in a custom Exception |
182 self.disp(xml_data) | 182 self.disp(xml_data) |
183 self.disp(u"") | 183 self.disp("") |
184 | 184 |
185 def start(self): | 185 def start(self): |
186 self.host.bridge.register_signal("xmlLog", self.printXML, "plugin") | 186 self.host.bridge.register_signal("xmlLog", self.printXML, "plugin") |
187 | 187 |
188 | 188 |