Mercurial > libervia-backend
comparison sat/plugins/plugin_misc_upload.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 378188abe941 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
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 from sat.core.i18n import _, D_ | 20 from sat.core.i18n import _, D_ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core.log import getLogger | 22 from sat.core.log import getLogger |
23 | |
23 log = getLogger(__name__) | 24 log = getLogger(__name__) |
24 from sat.core import exceptions | 25 from sat.core import exceptions |
25 from sat.tools import xml_tools | 26 from sat.tools import xml_tools |
26 from twisted.internet import defer | 27 from twisted.internet import defer |
27 from twisted.words.protocols.jabber import jid | 28 from twisted.words.protocols.jabber import jid |
33 C.PI_NAME: "File Upload", | 34 C.PI_NAME: "File Upload", |
34 C.PI_IMPORT_NAME: "UPLOAD", | 35 C.PI_IMPORT_NAME: "UPLOAD", |
35 C.PI_TYPE: C.PLUG_TYPE_MISC, | 36 C.PI_TYPE: C.PLUG_TYPE_MISC, |
36 C.PI_MAIN: "UploadPlugin", | 37 C.PI_MAIN: "UploadPlugin", |
37 C.PI_HANDLER: "no", | 38 C.PI_HANDLER: "no", |
38 C.PI_DESCRIPTION: _("""File upload management""") | 39 C.PI_DESCRIPTION: _("""File upload management"""), |
39 } | 40 } |
40 | 41 |
41 | 42 |
42 UPLOADING = D_(u'Please select a file to upload') | 43 UPLOADING = D_(u"Please select a file to upload") |
43 UPLOADING_TITLE = D_(u'File upload') | 44 UPLOADING_TITLE = D_(u"File upload") |
44 BOOL_OPTIONS = ('ignore_tls_errors',) | 45 BOOL_OPTIONS = ("ignore_tls_errors",) |
45 | 46 |
46 | 47 |
47 class UploadPlugin(object): | 48 class UploadPlugin(object): |
48 # TODO: plugin unload | 49 # TODO: plugin unload |
49 | 50 |
50 def __init__(self, host): | 51 def __init__(self, host): |
51 log.info(_("plugin Upload initialization")) | 52 log.info(_("plugin Upload initialization")) |
52 self.host = host | 53 self.host = host |
53 host.bridge.addMethod("fileUpload", ".plugin", in_sign='sssa{ss}s', out_sign='a{ss}', method=self._fileUpload, async=True) | 54 host.bridge.addMethod( |
55 "fileUpload", | |
56 ".plugin", | |
57 in_sign="sssa{ss}s", | |
58 out_sign="a{ss}", | |
59 method=self._fileUpload, | |
60 async=True, | |
61 ) | |
54 self._upload_callbacks = [] | 62 self._upload_callbacks = [] |
55 | 63 |
56 def _fileUpload(self, filepath, filename, upload_jid_s='', options=None, profile=C.PROF_KEY_NONE): | 64 def _fileUpload( |
65 self, filepath, filename, upload_jid_s="", options=None, profile=C.PROF_KEY_NONE | |
66 ): | |
57 client = self.host.getClient(profile) | 67 client = self.host.getClient(profile) |
58 upload_jid = jid.JID(upload_jid_s) if upload_jid_s else None | 68 upload_jid = jid.JID(upload_jid_s) if upload_jid_s else None |
59 if options is None: | 69 if options is None: |
60 options = {} | 70 options = {} |
61 # we convert values that are well-known booleans | 71 # we convert values that are well-known booleans |
63 try: | 73 try: |
64 options[bool_option] = C.bool(options[bool_option]) | 74 options[bool_option] = C.bool(options[bool_option]) |
65 except KeyError: | 75 except KeyError: |
66 pass | 76 pass |
67 | 77 |
68 return self.fileUpload(client, filepath, filename or None, upload_jid, options or None) | 78 return self.fileUpload( |
79 client, filepath, filename or None, upload_jid, options or None | |
80 ) | |
69 | 81 |
70 def fileUpload(self, client, filepath, filename, upload_jid, options): | 82 def fileUpload(self, client, filepath, filename, upload_jid, options): |
71 """Send a file using best available method | 83 """Send a file using best available method |
72 | 84 |
73 parameters are the same as for [upload] | 85 parameters are the same as for [upload] |
74 @return (dict): action dictionary, with progress id in case of success, else xmlui message | 86 @return (dict): action dictionary, with progress id in case of success, else xmlui message |
75 """ | 87 """ |
88 | |
76 def uploadCb(data): | 89 def uploadCb(data): |
77 progress_id, dummy = data | 90 progress_id, dummy = data |
78 return {'progress': progress_id} | 91 return {"progress": progress_id} |
79 | 92 |
80 def uploadEb(fail): | 93 def uploadEb(fail): |
81 msg = unicode(fail) | 94 msg = unicode(fail) |
82 log.warning(msg) | 95 log.warning(msg) |
83 return {'xmlui': xml_tools.note(u"Can't upload file", msg, C.XMLUI_DATA_LVL_WARNING).toXml()} | 96 return { |
97 "xmlui": xml_tools.note( | |
98 u"Can't upload file", msg, C.XMLUI_DATA_LVL_WARNING | |
99 ).toXml() | |
100 } | |
84 | 101 |
85 d = self.upload(client, filepath, filename, upload_jid, options) | 102 d = self.upload(client, filepath, filename, upload_jid, options) |
86 d.addCallback(uploadCb) | 103 d.addCallback(uploadCb) |
87 d.addErrback(uploadEb) | 104 d.addErrback(uploadEb) |
88 return d | 105 return d |
109 raise exceptions.DataError(u"The given path doesn't link to a file") | 126 raise exceptions.DataError(u"The given path doesn't link to a file") |
110 for method_name, available_cb, upload_cb, priority in self._upload_callbacks: | 127 for method_name, available_cb, upload_cb, priority in self._upload_callbacks: |
111 try: | 128 try: |
112 upload_jid = yield available_cb(upload_jid, client.profile) | 129 upload_jid = yield available_cb(upload_jid, client.profile) |
113 except exceptions.NotFound: | 130 except exceptions.NotFound: |
114 continue # no entity managing this extension found | 131 continue # no entity managing this extension found |
115 | 132 |
116 log.info(u"{name} method will be used to upload the file".format(name=method_name)) | 133 log.info( |
117 progress_id_d, download_d = yield upload_cb(filepath, filename, upload_jid, options, client.profile) | 134 u"{name} method will be used to upload the file".format(name=method_name) |
135 ) | |
136 progress_id_d, download_d = yield upload_cb( | |
137 filepath, filename, upload_jid, options, client.profile | |
138 ) | |
118 progress_id = yield progress_id_d | 139 progress_id = yield progress_id_d |
119 defer.returnValue((progress_id, download_d)) | 140 defer.returnValue((progress_id, download_d)) |
120 | 141 |
121 raise exceptions.NotFound(u"Can't find any method to upload a file") | 142 raise exceptions.NotFound(u"Can't find any method to upload a file") |
122 | 143 |
135 @param priority(int): pririoty of this method, the higher available will be used | 156 @param priority(int): pririoty of this method, the higher available will be used |
136 """ | 157 """ |
137 assert method_name | 158 assert method_name |
138 for data in self._upload_callbacks: | 159 for data in self._upload_callbacks: |
139 if method_name == data[0]: | 160 if method_name == data[0]: |
140 raise exceptions.ConflictError(u'A method with this name is already registered') | 161 raise exceptions.ConflictError( |
162 u"A method with this name is already registered" | |
163 ) | |
141 self._upload_callbacks.append((method_name, available_cb, upload_cb, priority)) | 164 self._upload_callbacks.append((method_name, available_cb, upload_cb, priority)) |
142 self._upload_callbacks.sort(key=lambda data: data[3], reverse=True) | 165 self._upload_callbacks.sort(key=lambda data: data[3], reverse=True) |
143 | 166 |
144 def unregister(self, method_name): | 167 def unregister(self, method_name): |
145 for idx, data in enumerate(self._upload_callbacks): | 168 for idx, data in enumerate(self._upload_callbacks): |