Mercurial > libervia-backend
diff sat/plugins/plugin_comp_file_sharing.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 | e0429ff7f6b6 |
children | 9d0df638c8b4 |
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_file_sharing.py Wed Jul 31 11:31:22 2019 +0200 +++ b/sat/plugins/plugin_comp_file_sharing.py Tue Aug 13 19:08:41 2019 +0200 @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # SAT plugin for parrot mode (experimental) @@ -55,17 +55,17 @@ C.PI_RECOMMENDATIONS: [], C.PI_MAIN: "FileSharing", C.PI_HANDLER: C.BOOL_TRUE, - C.PI_DESCRIPTION: _(u"""Component hosting and sharing files"""), + C.PI_DESCRIPTION: _("""Component hosting and sharing files"""), } -HASH_ALGO = u"sha-256" +HASH_ALGO = "sha-256" NS_COMMENTS = "org.salut-a-toi.comments" COMMENT_NODE_PREFIX = "org.salut-a-toi.file_comments/" class FileSharing(object): def __init__(self, host): - log.info(_(u"File Sharing initialization")) + log.info(_("File Sharing initialization")) self.host = host self._f = host.plugins["FILE"] self._jf = host.plugins["XEP-0234"] @@ -99,12 +99,12 @@ on file is received, this method create hash/thumbnails if necessary move the file to the right location, and create metadata entry in database """ - name = file_data[u"name"] + name = file_data["name"] extra = {} - if file_data[u"hash_algo"] == HASH_ALGO: - log.debug(_(u"Reusing already generated hash")) - file_hash = file_data[u"hash_hasher"].hexdigest() + if file_data["hash_algo"] == HASH_ALGO: + log.debug(_("Reusing already generated hash")) + file_hash = file_data["hash_hasher"].hexdigest() else: hasher = self._h.getHasher(HASH_ALGO) with open("file_path") as f: @@ -113,7 +113,7 @@ if os.path.isfile(final_path): log.debug( - u"file [{file_hash}] already exists, we can remove temporary one".format( + "file [{file_hash}] already exists, we can remove temporary one".format( file_hash=file_hash ) ) @@ -121,16 +121,16 @@ else: os.rename(file_path, final_path) log.debug( - u"file [{file_hash}] moved to {files_path}".format( + "file [{file_hash}] moved to {files_path}".format( file_hash=file_hash, files_path=self.files_path ) ) - mime_type = file_data.get(u"mime_type") - if not mime_type or mime_type == u"application/octet-stream": + mime_type = file_data.get("mime_type") + if not mime_type or mime_type == "application/octet-stream": mime_type = mimetypes.guess_type(name)[0] - if mime_type is not None and mime_type.startswith(u"image"): + if mime_type is not None and mime_type.startswith("image"): thumbnails = extra.setdefault(C.KEY_THUMBNAILS, []) for max_thumb_size in (self._t.SIZE_SMALL, self._t.SIZE_MEDIUM): try: @@ -141,19 +141,19 @@ 60 * 60 * 24 * 31 * 6, ) except Exception as e: - log.warning(_(u"Can't create thumbnail: {reason}").format(reason=e)) + log.warning(_("Can't create thumbnail: {reason}").format(reason=e)) break - thumbnails.append({u"id": thumb_id, u"size": thumb_size}) + thumbnails.append({"id": thumb_id, "size": thumb_size}) self.host.memory.setFile( client, name=name, - version=u"", + version="", file_hash=file_hash, hash_algo=HASH_ALGO, - size=file_data[u"size"], - path=file_data.get(u"path"), - namespace=file_data.get(u"namespace"), + size=file_data["size"], + path=file_data.get("path"), + namespace=file_data.get("namespace"), mime_type=mime_type, owner=peer_jid, extra=extra, @@ -191,49 +191,49 @@ self, client, session, content_data, content_name, file_data, file_elt ): """This method retrieve a file on request, and send if after checking permissions""" - peer_jid = session[u"peer_jid"] + peer_jid = session["peer_jid"] try: found_files = yield self.host.memory.getFiles( client, peer_jid=peer_jid, - name=file_data.get(u"name"), - file_hash=file_data.get(u"file_hash"), - hash_algo=file_data.get(u"hash_algo"), - path=file_data.get(u"path"), - namespace=file_data.get(u"namespace"), + name=file_data.get("name"), + file_hash=file_data.get("file_hash"), + hash_algo=file_data.get("hash_algo"), + path=file_data.get("path"), + namespace=file_data.get("namespace"), ) except exceptions.NotFound: found_files = None except exceptions.PermissionError: log.warning( - _(u"{peer_jid} is trying to access an unauthorized file: {name}").format( - peer_jid=peer_jid, name=file_data.get(u"name") + _("{peer_jid} is trying to access an unauthorized file: {name}").format( + peer_jid=peer_jid, name=file_data.get("name") ) ) defer.returnValue(False) if not found_files: log.warning( - _(u"no matching file found ({file_data})").format(file_data=file_data) + _("no matching file found ({file_data})").format(file_data=file_data) ) defer.returnValue(False) # we only use the first found file found_file = found_files[0] - if found_file[u'type'] != C.FILE_TYPE_FILE: - raise TypeError(u"a file was expected, type is {type_}".format( - type_=found_file[u'type'])) - file_hash = found_file[u"file_hash"] + if found_file['type'] != C.FILE_TYPE_FILE: + raise TypeError("a file was expected, type is {type_}".format( + type_=found_file['type'])) + file_hash = found_file["file_hash"] file_path = os.path.join(self.files_path, file_hash) - file_data[u"hash_hasher"] = hasher = self._h.getHasher(found_file[u"hash_algo"]) - size = file_data[u"size"] = found_file[u"size"] - file_data[u"file_hash"] = file_hash - file_data[u"hash_algo"] = found_file[u"hash_algo"] + file_data["hash_hasher"] = hasher = self._h.getHasher(found_file["hash_algo"]) + size = file_data["size"] = found_file["size"] + file_data["file_hash"] = file_hash + file_data["hash_algo"] = found_file["hash_algo"] # we complete file_elt so peer can have some details on the file - if u"name" not in file_data: - file_elt.addElement(u"name", content=found_file[u"name"]) - file_elt.addElement(u"size", content=unicode(size)) + if "name" not in file_data: + file_elt.addElement("name", content=found_file["name"]) + file_elt.addElement("size", content=str(size)) content_data["stream_object"] = stream.FileStreamObject( self.host, client, @@ -268,11 +268,11 @@ comment_elt = file_elt.addElement((NS_COMMENTS, "comments"), content=comments_url) try: - count = len(extra_args[u"extra"][u"comments"]) + count = len(extra_args["extra"]["comments"]) except KeyError: count = 0 - comment_elt["count"] = unicode(count) + comment_elt["count"] = str(count) return True def _getFileComments(self, file_elt, file_data): @@ -280,7 +280,7 @@ comments_elt = next(file_elt.elements(NS_COMMENTS, "comments")) except StopIteration: return - file_data["comments_url"] = unicode(comments_elt) + file_data["comments_url"] = str(comments_elt) file_data["comments_count"] = comments_elt["count"] return True