comparison sat/plugins/plugin_misc_upload.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 3cac3d050046
children e75024e41f81
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
39 C.PI_HANDLER: "no", 39 C.PI_HANDLER: "no",
40 C.PI_DESCRIPTION: _("""File upload management"""), 40 C.PI_DESCRIPTION: _("""File upload management"""),
41 } 41 }
42 42
43 43
44 UPLOADING = D_(u"Please select a file to upload") 44 UPLOADING = D_("Please select a file to upload")
45 UPLOADING_TITLE = D_(u"File upload") 45 UPLOADING_TITLE = D_("File upload")
46 BOOL_OPTIONS = ("ignore_tls_errors",) 46 BOOL_OPTIONS = ("ignore_tls_errors",)
47 47
48 48
49 class UploadPlugin(object): 49 class UploadPlugin(object):
50 # TODO: plugin unload 50 # TODO: plugin unload
56 "fileUpload", 56 "fileUpload",
57 ".plugin", 57 ".plugin",
58 in_sign="sssa{ss}s", 58 in_sign="sssa{ss}s",
59 out_sign="a{ss}", 59 out_sign="a{ss}",
60 method=self._fileUpload, 60 method=self._fileUpload,
61 async=True, 61 async_=True,
62 ) 62 )
63 self._upload_callbacks = [] 63 self._upload_callbacks = []
64 64
65 def _fileUpload( 65 def _fileUpload(
66 self, filepath, filename, upload_jid_s="", options=None, profile=C.PROF_KEY_NONE 66 self, filepath, filename, upload_jid_s="", options=None, profile=C.PROF_KEY_NONE
95 def uploadEb(fail): 95 def uploadEb(fail):
96 if (isinstance(fail.value, jabber_error.StanzaError) 96 if (isinstance(fail.value, jabber_error.StanzaError)
97 and fail.value.condition == 'not-acceptable'): 97 and fail.value.condition == 'not-acceptable'):
98 reason = fail.value.text 98 reason = fail.value.text
99 else: 99 else:
100 reason = unicode(fail.value) 100 reason = str(fail.value)
101 msg = D_(u"Can't upload file: {reason}").format(reason=reason) 101 msg = D_("Can't upload file: {reason}").format(reason=reason)
102 log.warning(msg) 102 log.warning(msg)
103 return { 103 return {
104 "xmlui": xml_tools.note( 104 "xmlui": xml_tools.note(
105 msg, D_(u"Can't upload file"), C.XMLUI_DATA_LVL_WARNING 105 msg, D_("Can't upload file"), C.XMLUI_DATA_LVL_WARNING
106 ).toXml() 106 ).toXml()
107 } 107 }
108 108
109 d = self.upload(client, filepath, filename, upload_jid, options) 109 d = self.upload(client, filepath, filename, upload_jid, options)
110 d.addCallback(uploadCb) 110 d.addCallback(uploadCb)
128 download URL when upload is finished 128 download URL when upload is finished
129 """ 129 """
130 if options is None: 130 if options is None:
131 options = {} 131 options = {}
132 if not os.path.isfile(filepath): 132 if not os.path.isfile(filepath):
133 raise exceptions.DataError(u"The given path doesn't link to a file") 133 raise exceptions.DataError("The given path doesn't link to a file")
134 for method_name, available_cb, upload_cb, priority in self._upload_callbacks: 134 for method_name, available_cb, upload_cb, priority in self._upload_callbacks:
135 try: 135 try:
136 upload_jid = yield available_cb(upload_jid, client.profile) 136 upload_jid = yield available_cb(upload_jid, client.profile)
137 except exceptions.NotFound: 137 except exceptions.NotFound:
138 continue # no entity managing this extension found 138 continue # no entity managing this extension found
139 139
140 log.info( 140 log.info(
141 u"{name} method will be used to upload the file".format(name=method_name) 141 "{name} method will be used to upload the file".format(name=method_name)
142 ) 142 )
143 progress_id_d, download_d = yield upload_cb( 143 progress_id_d, download_d = yield upload_cb(
144 filepath, filename, upload_jid, options, client.profile 144 filepath, filename, upload_jid, options, client.profile
145 ) 145 )
146 progress_id = yield progress_id_d 146 progress_id = yield progress_id_d
147 defer.returnValue((progress_id, download_d)) 147 defer.returnValue((progress_id, download_d))
148 148
149 raise exceptions.NotFound(u"Can't find any method to upload a file") 149 raise exceptions.NotFound("Can't find any method to upload a file")
150 150
151 def register(self, method_name, available_cb, upload_cb, priority=0): 151 def register(self, method_name, available_cb, upload_cb, priority=0):
152 """Register a fileUploading method 152 """Register a fileUploading method
153 153
154 @param method_name(unicode): short name for the method, must be unique 154 @param method_name(unicode): short name for the method, must be unique
165 """ 165 """
166 assert method_name 166 assert method_name
167 for data in self._upload_callbacks: 167 for data in self._upload_callbacks:
168 if method_name == data[0]: 168 if method_name == data[0]:
169 raise exceptions.ConflictError( 169 raise exceptions.ConflictError(
170 u"A method with this name is already registered" 170 "A method with this name is already registered"
171 ) 171 )
172 self._upload_callbacks.append((method_name, available_cb, upload_cb, priority)) 172 self._upload_callbacks.append((method_name, available_cb, upload_cb, priority))
173 self._upload_callbacks.sort(key=lambda data: data[3], reverse=True) 173 self._upload_callbacks.sort(key=lambda data: data[3], reverse=True)
174 174
175 def unregister(self, method_name): 175 def unregister(self, method_name):
176 for idx, data in enumerate(self._upload_callbacks): 176 for idx, data in enumerate(self._upload_callbacks):
177 if data[0] == method_name: 177 if data[0] == method_name:
178 del [idx] 178 del [idx]
179 return 179 return
180 raise exceptions.NotFound(u"The name to unregister doesn't exist") 180 raise exceptions.NotFound("The name to unregister doesn't exist")