Mercurial > libervia-backend
comparison sat_frontends/jp/output_std.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 | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
24 from sat.tools.common.ansi import ANSI as A | 24 from sat.tools.common.ansi import ANSI as A |
25 from sat.tools.common import date_utils | 25 from sat.tools.common import date_utils |
26 import json | 26 import json |
27 | 27 |
28 __outputs__ = ["Simple", "Json"] | 28 __outputs__ = ["Simple", "Json"] |
29 SIMPLE = u"simple" | 29 SIMPLE = "simple" |
30 JSON = u"json" | 30 JSON = "json" |
31 JSON_RAW = u"json_raw" | 31 JSON_RAW = "json_raw" |
32 | 32 |
33 | 33 |
34 class Simple(object): | 34 class Simple(object): |
35 """Default outputs""" | 35 """Default outputs""" |
36 | 36 |
43 host.register_output(C.OUTPUT_DICT_DICT, SIMPLE, self.dict_dict) | 43 host.register_output(C.OUTPUT_DICT_DICT, SIMPLE, self.dict_dict) |
44 host.register_output(C.OUTPUT_MESS, SIMPLE, self.messages) | 44 host.register_output(C.OUTPUT_MESS, SIMPLE, self.messages) |
45 host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print) | 45 host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print) |
46 | 46 |
47 def simple_print(self, data): | 47 def simple_print(self, data): |
48 self.host.disp(unicode(data)) | 48 self.host.disp(str(data)) |
49 | 49 |
50 def list(self, data): | 50 def list(self, data): |
51 self.host.disp(u"\n".join(data)) | 51 self.host.disp("\n".join(data)) |
52 | 52 |
53 def dict(self, data, indent=0, header_color=C.A_HEADER): | 53 def dict(self, data, indent=0, header_color=C.A_HEADER): |
54 options = self.host.parse_output_options() | 54 options = self.host.parse_output_options() |
55 self.host.check_output_options({u"no-header"}, options) | 55 self.host.check_output_options({"no-header"}, options) |
56 show_header = not u"no-header" in options | 56 show_header = not "no-header" in options |
57 for k, v in data.iteritems(): | 57 for k, v in data.items(): |
58 if show_header: | 58 if show_header: |
59 header = A.color(header_color, k) + u": " | 59 header = A.color(header_color, k) + ": " |
60 else: | 60 else: |
61 header = u"" | 61 header = "" |
62 | 62 |
63 self.host.disp( | 63 self.host.disp( |
64 ( | 64 ( |
65 u"{indent}{header}{value}".format( | 65 "{indent}{header}{value}".format( |
66 indent=indent * u" ", header=header, value=v | 66 indent=indent * " ", header=header, value=v |
67 ) | 67 ) |
68 ) | 68 ) |
69 ) | 69 ) |
70 | 70 |
71 def list_dict(self, data): | 71 def list_dict(self, data): |
72 for idx, datum in enumerate(data): | 72 for idx, datum in enumerate(data): |
73 if idx: | 73 if idx: |
74 self.host.disp(u"\n") | 74 self.host.disp("\n") |
75 self.dict(datum) | 75 self.dict(datum) |
76 | 76 |
77 def dict_dict(self, data): | 77 def dict_dict(self, data): |
78 for key, sub_dict in data.iteritems(): | 78 for key, sub_dict in data.items(): |
79 self.host.disp(A.color(C.A_HEADER, key)) | 79 self.host.disp(A.color(C.A_HEADER, key)) |
80 self.dict(sub_dict, indent=4, header_color=C.A_SUBHEADER) | 80 self.dict(sub_dict, indent=4, header_color=C.A_SUBHEADER) |
81 | 81 |
82 def messages(self, data): | 82 def messages(self, data): |
83 # TODO: handle lang, and non chat message (normal, headline) | 83 # TODO: handle lang, and non chat message (normal, headline) |
84 for mess_data in data: | 84 for mess_data in data: |
85 (uid, timestamp, from_jid, to_jid, message, subject, mess_type, | 85 (uid, timestamp, from_jid, to_jid, message, subject, mess_type, |
86 extra) = mess_data | 86 extra) = mess_data |
87 time_str = date_utils.date_fmt(timestamp, u"auto_day", | 87 time_str = date_utils.date_fmt(timestamp, "auto_day", |
88 tz_info=date_utils.TZ_LOCAL) | 88 tz_info=date_utils.TZ_LOCAL) |
89 from_jid = jid.JID(from_jid) | 89 from_jid = jid.JID(from_jid) |
90 if mess_type == C.MESS_TYPE_GROUPCHAT: | 90 if mess_type == C.MESS_TYPE_GROUPCHAT: |
91 nick = from_jid.resource | 91 nick = from_jid.resource |
92 else: | 92 else: |
94 | 94 |
95 if self.host.own_jid is not None and self.host.own_jid.bare == from_jid.bare: | 95 if self.host.own_jid is not None and self.host.own_jid.bare == from_jid.bare: |
96 nick_color = A.BOLD + A.FG_BLUE | 96 nick_color = A.BOLD + A.FG_BLUE |
97 else: | 97 else: |
98 nick_color = A.BOLD + A.FG_YELLOW | 98 nick_color = A.BOLD + A.FG_YELLOW |
99 message = message.values()[0] if message else u"" | 99 message = list(message.values())[0] if message else "" |
100 | 100 |
101 self.host.disp(A.color( | 101 self.host.disp(A.color( |
102 A.FG_CYAN, u'['+time_str+u'] ', | 102 A.FG_CYAN, '['+time_str+'] ', |
103 nick_color, nick, A.RESET, A.BOLD, u'> ', | 103 nick_color, nick, A.RESET, A.BOLD, '> ', |
104 A.RESET, message)) | 104 A.RESET, message)) |
105 | 105 |
106 | 106 |
107 class Json(object): | 107 class Json(object): |
108 """outputs in json format""" | 108 """outputs in json format""" |