comparison sat_frontends/jp/cmd_forums.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_frontends.jp import common 24 from sat_frontends.jp import common
25 from sat.tools.common.ansi import ANSI as A 25 from sat.tools.common.ansi import ANSI as A
26 from functools import partial 26 from functools import partial
27 import codecs 27 import codecs
28 import json 28 import json
29 29
30 __commands__ = ["Forums"] 30 __commands__ = ["Forums"]
31 31
32 FORUMS_TMP_DIR = u"forums" 32 FORUMS_TMP_DIR = "forums"
33 33
34 34
35 class Edit(base.CommandBase, common.BaseEdit): 35 class Edit(base.CommandBase, common.BaseEdit):
36 use_items = False 36 use_items = False
37 37
41 host, 41 host,
42 "edit", 42 "edit",
43 use_pubsub=True, 43 use_pubsub=True,
44 use_draft=True, 44 use_draft=True,
45 use_verbose=True, 45 use_verbose=True,
46 help=_(u"edit forums"), 46 help=_("edit forums"),
47 ) 47 )
48 common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR) 48 common.BaseEdit.__init__(self, self.host, FORUMS_TMP_DIR)
49 self.need_loop = True 49 self.need_loop = True
50 50
51 def add_parser_options(self): 51 def add_parser_options(self):
52 self.parser.add_argument( 52 self.parser.add_argument(
53 "-k", 53 "-k",
54 "--key", 54 "--key",
55 type=base.unicode_decoder, 55 default="",
56 default=u"", 56 help=_("forum key (DEFAULT: default forums)"),
57 help=_(u"forum key (DEFAULT: default forums)"),
58 ) 57 )
59 58
60 def getTmpSuff(self): 59 def getTmpSuff(self):
61 """return suffix used for content file""" 60 """return suffix used for content file"""
62 return u"json" 61 return "json"
63 62
64 def forumsSetCb(self): 63 def forumsSetCb(self):
65 self.disp(_(u"forums have been edited"), 1) 64 self.disp(_("forums have been edited"), 1)
66 self.host.quit() 65 self.host.quit()
67 66
68 def publish(self, forums_raw): 67 def publish(self, forums_raw):
69 self.host.bridge.forumsSet( 68 self.host.bridge.forumsSet(
70 forums_raw, 69 forums_raw,
73 self.args.key, 72 self.args.key,
74 self.profile, 73 self.profile,
75 callback=self.forumsSetCb, 74 callback=self.forumsSetCb,
76 errback=partial( 75 errback=partial(
77 self.errback, 76 self.errback,
78 msg=_(u"can't set forums: {}"), 77 msg=_("can't set forums: {}"),
79 exit_code=C.EXIT_BRIDGE_ERRBACK, 78 exit_code=C.EXIT_BRIDGE_ERRBACK,
80 ), 79 ),
81 ) 80 )
82 81
83 def forumsGetCb(self, forums_json): 82 def forumsGetCb(self, forums_json):
92 content_file_obj.seek(0) 91 content_file_obj.seek(0)
93 self.runEditor("forums_editor_args", content_file_path, content_file_obj) 92 self.runEditor("forums_editor_args", content_file_path, content_file_obj)
94 93
95 def forumsGetEb(self, failure_): 94 def forumsGetEb(self, failure_):
96 # FIXME: error handling with bridge is broken, need to be properly fixed 95 # FIXME: error handling with bridge is broken, need to be properly fixed
97 if failure_.condition == u"item-not-found": 96 if failure_.condition == "item-not-found":
98 self.forumsGetCb(u"") 97 self.forumsGetCb("")
99 else: 98 else:
100 self.errback( 99 self.errback(
101 failure_, 100 failure_,
102 msg=_(u"can't get forums structure: {}"), 101 msg=_("can't get forums structure: {}"),
103 exit_code=C.EXIT_BRIDGE_ERRBACK, 102 exit_code=C.EXIT_BRIDGE_ERRBACK,
104 ) 103 )
105 104
106 def start(self): 105 def start(self):
107 self.host.bridge.forumsGet( 106 self.host.bridge.forumsGet(
123 "get", 122 "get",
124 use_output=C.OUTPUT_COMPLEX, 123 use_output=C.OUTPUT_COMPLEX,
125 extra_outputs=extra_outputs, 124 extra_outputs=extra_outputs,
126 use_pubsub=True, 125 use_pubsub=True,
127 use_verbose=True, 126 use_verbose=True,
128 help=_(u"get forums structure"), 127 help=_("get forums structure"),
129 ) 128 )
130 self.need_loop = True 129 self.need_loop = True
131 130
132 def add_parser_options(self): 131 def add_parser_options(self):
133 self.parser.add_argument( 132 self.parser.add_argument(
134 "-k", 133 "-k",
135 "--key", 134 "--key",
136 type=base.unicode_decoder, 135 default="",
137 default=u"", 136 help=_("forum key (DEFAULT: default forums)"),
138 help=_(u"forum key (DEFAULT: default forums)"),
139 ) 137 )
140 138
141 def default_output(self, forums, level=0): 139 def default_output(self, forums, level=0):
142 for forum in forums: 140 for forum in forums:
143 keys = list(forum.keys()) 141 keys = list(forum.keys())
144 keys.sort() 142 keys.sort()
145 try: 143 try:
146 keys.remove(u"title") 144 keys.remove("title")
147 except ValueError: 145 except ValueError:
148 pass 146 pass
149 else: 147 else:
150 keys.insert(0, u"title") 148 keys.insert(0, "title")
151 try: 149 try:
152 keys.remove(u"sub-forums") 150 keys.remove("sub-forums")
153 except ValueError: 151 except ValueError:
154 pass 152 pass
155 else: 153 else:
156 keys.append(u"sub-forums") 154 keys.append("sub-forums")
157 155
158 for key in keys: 156 for key in keys:
159 value = forum[key] 157 value = forum[key]
160 if key == "sub-forums": 158 if key == "sub-forums":
161 self.default_output(value, level + 1) 159 self.default_output(value, level + 1)
162 else: 160 else:
163 if self.host.verbosity < 1 and key != u"title": 161 if self.host.verbosity < 1 and key != "title":
164 continue 162 continue
165 head_color = C.A_LEVEL_COLORS[level % len(C.A_LEVEL_COLORS)] 163 head_color = C.A_LEVEL_COLORS[level % len(C.A_LEVEL_COLORS)]
166 self.disp( 164 self.disp(
167 A.color(level * 4 * u" ", head_color, key, A.RESET, u": ", value) 165 A.color(level * 4 * " ", head_color, key, A.RESET, ": ", value)
168 ) 166 )
169 167
170 def forumsGetCb(self, forums_raw): 168 def forumsGetCb(self, forums_raw):
171 if not forums_raw: 169 if not forums_raw:
172 self.disp(_(u"no schema found"), 1) 170 self.disp(_("no schema found"), 1)
173 self.host.quit(1) 171 self.host.quit(1)
174 forums = json.loads(forums_raw) 172 forums = json.loads(forums_raw)
175 self.output(forums) 173 self.output(forums)
176 self.host.quit() 174 self.host.quit()
177 175
182 self.args.key, 180 self.args.key,
183 self.profile, 181 self.profile,
184 callback=self.forumsGetCb, 182 callback=self.forumsGetCb,
185 errback=partial( 183 errback=partial(
186 self.errback, 184 self.errback,
187 msg=_(u"can't get forums: {}"), 185 msg=_("can't get forums: {}"),
188 exit_code=C.EXIT_BRIDGE_ERRBACK, 186 exit_code=C.EXIT_BRIDGE_ERRBACK,
189 ), 187 ),
190 ) 188 )
191 189
192 190
193 class Forums(base.CommandBase): 191 class Forums(base.CommandBase):
194 subcommands = (Get, Edit) 192 subcommands = (Get, Edit)
195 193
196 def __init__(self, host): 194 def __init__(self, host):
197 super(Forums, self).__init__( 195 super(Forums, self).__init__(
198 host, "forums", use_profile=False, help=_(u"Forums structure edition") 196 host, "forums", use_profile=False, help=_("Forums structure edition")
199 ) 197 )