comparison sat/plugins/plugin_misc_file.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 d1464548055a
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT plugin for file tansfer 4 # SAT plugin for file tansfer
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
43 This plugin manage the various ways of sending a file, and choose the best one.""" 43 This plugin manage the various ways of sending a file, and choose the best one."""
44 ), 44 ),
45 } 45 }
46 46
47 47
48 SENDING = D_(u"Please select a file to send to {peer}") 48 SENDING = D_("Please select a file to send to {peer}")
49 SENDING_TITLE = D_(u"File sending") 49 SENDING_TITLE = D_("File sending")
50 CONFIRM = D_( 50 CONFIRM = D_(
51 u'{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?' 51 '{peer} wants to send the file "{name}" to you:\n{desc}\n\nThe file has a size of {size_human}\n\nDo you accept ?'
52 ) 52 )
53 CONFIRM_TITLE = D_(u"Confirm file transfer") 53 CONFIRM_TITLE = D_("Confirm file transfer")
54 CONFIRM_OVERWRITE = D_(u"File {} already exists, are you sure you want to overwrite ?") 54 CONFIRM_OVERWRITE = D_("File {} already exists, are you sure you want to overwrite ?")
55 CONFIRM_OVERWRITE_TITLE = D_(u"File exists") 55 CONFIRM_OVERWRITE_TITLE = D_("File exists")
56 SECURITY_LIMIT = 30 56 SECURITY_LIMIT = 30
57 57
58 PROGRESS_ID_KEY = "progress_id" 58 PROGRESS_ID_KEY = "progress_id"
59 59
60 60
68 "fileSend", 68 "fileSend",
69 ".plugin", 69 ".plugin",
70 in_sign="ssssa{ss}s", 70 in_sign="ssssa{ss}s",
71 out_sign="a{ss}", 71 out_sign="a{ss}",
72 method=self._fileSend, 72 method=self._fileSend,
73 async=True, 73 async_=True,
74 ) 74 )
75 self._file_callbacks = [] 75 self._file_callbacks = []
76 host.importMenu( 76 host.importMenu(
77 (D_("Action"), D_("send file")), 77 (D_("Action"), D_("send file")),
78 self._fileSendMenu, 78 self._fileSendMenu,
107 @param file_desc(unicode, None): description of the file 107 @param file_desc(unicode, None): description of the file
108 @param profile: %(doc_profile)s 108 @param profile: %(doc_profile)s
109 @return (dict): action dictionary, with progress id in case of success, else xmlui message 109 @return (dict): action dictionary, with progress id in case of success, else xmlui message
110 """ 110 """
111 if not os.path.isfile(filepath): 111 if not os.path.isfile(filepath):
112 raise exceptions.DataError(u"The given path doesn't link to a file") 112 raise exceptions.DataError("The given path doesn't link to a file")
113 if not filename: 113 if not filename:
114 filename = os.path.basename(filepath) or "_" 114 filename = os.path.basename(filepath) or "_"
115 for namespace, callback, priority, method_name in self._file_callbacks: 115 for namespace, callback, priority, method_name in self._file_callbacks:
116 has_feature = yield self.host.hasFeature(client, namespace, peer_jid) 116 has_feature = yield self.host.hasFeature(client, namespace, peer_jid)
117 if has_feature: 117 if has_feature:
118 log.info( 118 log.info(
119 u"{name} method will be used to send the file".format( 119 "{name} method will be used to send the file".format(
120 name=method_name 120 name=method_name
121 ) 121 )
122 ) 122 )
123 progress_id = yield callback( 123 progress_id = yield callback(
124 client, peer_jid, filepath, filename, file_desc, extra 124 client, peer_jid, filepath, filename, file_desc, extra
125 ) 125 )
126 defer.returnValue({"progress": progress_id}) 126 defer.returnValue({"progress": progress_id})
127 msg = u"Can't find any method to send file to {jid}".format(jid=peer_jid.full()) 127 msg = "Can't find any method to send file to {jid}".format(jid=peer_jid.full())
128 log.warning(msg) 128 log.warning(msg)
129 defer.returnValue( 129 defer.returnValue(
130 { 130 {
131 "xmlui": xml_tools.note( 131 "xmlui": xml_tools.note(
132 u"Can't transfer file", msg, C.XMLUI_DATA_LVL_WARNING 132 "Can't transfer file", msg, C.XMLUI_DATA_LVL_WARNING
133 ).toXml() 133 ).toXml()
134 } 134 }
135 ) 135 )
136 136
137 def _onFileChoosed(self, client, peer_jid, data): 137 def _onFileChoosed(self, client, peer_jid, data):
179 @param method_name(unicode): short name for the method, namespace will be used if None 179 @param method_name(unicode): short name for the method, namespace will be used if None
180 """ 180 """
181 for data in self._file_callbacks: 181 for data in self._file_callbacks:
182 if namespace == data[0]: 182 if namespace == data[0]:
183 raise exceptions.ConflictError( 183 raise exceptions.ConflictError(
184 u"A method with this namespace is already registered" 184 "A method with this namespace is already registered"
185 ) 185 )
186 self._file_callbacks.append( 186 self._file_callbacks.append(
187 (namespace, callback, priority, method_name or namespace) 187 (namespace, callback, priority, method_name or namespace)
188 ) 188 )
189 self._file_callbacks.sort(key=lambda data: data[2], reverse=True) 189 self._file_callbacks.sort(key=lambda data: data[2], reverse=True)
191 def unregister(self, namespace): 191 def unregister(self, namespace):
192 for idx, data in enumerate(self._file_callbacks): 192 for idx, data in enumerate(self._file_callbacks):
193 if data[0] == namespace: 193 if data[0] == namespace:
194 del [idx] 194 del [idx]
195 return 195 return
196 raise exceptions.NotFound(u"The namespace to unregister doesn't exist") 196 raise exceptions.NotFound("The namespace to unregister doesn't exist")
197 197
198 # Dialogs with user 198 # Dialogs with user
199 # the overwrite check is done here 199 # the overwrite check is done here
200 200
201 def openFileWrite(self, client, file_path, transfer_data, file_data, stream_object): 201 def openFileWrite(self, client, file_path, transfer_data, file_data, stream_object):
239 """ 239 """
240 if data.get("cancelled", False): 240 if data.get("cancelled", False):
241 return False 241 return False
242 path = data["path"] 242 path = data["path"]
243 file_data["file_path"] = file_path = os.path.join(path, file_data["name"]) 243 file_data["file_path"] = file_path = os.path.join(path, file_data["name"])
244 log.debug(u"destination file path set to {}".format(file_path)) 244 log.debug("destination file path set to {}".format(file_path))
245 245
246 # we manage case where file already exists 246 # we manage case where file already exists
247 if os.path.exists(file_path): 247 if os.path.exists(file_path):
248 248
249 def check_overwrite(overwrite): 249 def check_overwrite(overwrite):
307 return ret_value 307 return ret_value
308 filename = file_data["name"] 308 filename = file_data["name"]
309 assert filename and not "/" in filename 309 assert filename and not "/" in filename
310 assert PROGRESS_ID_KEY in file_data 310 assert PROGRESS_ID_KEY in file_data
311 # human readable size 311 # human readable size
312 file_data["size_human"] = u"{:.6n} Mio".format( 312 file_data["size_human"] = "{:.6n} Mio".format(
313 float(file_data["size"]) / (1024 ** 2) 313 float(file_data["size"]) / (1024 ** 2)
314 ) 314 )
315 d = xml_tools.deferDialog( 315 d = xml_tools.deferDialog(
316 self.host, 316 self.host,
317 _(CONFIRM).format(peer=peer_jid.full(), **file_data), 317 _(CONFIRM).format(peer=peer_jid.full(), **file_data),