From 45c7219afda150b3dc7ac442b5fd756691aa33dd Mon Sep 17 00:00:00 2001 From: Giuseppe Lo Presti Date: Mon, 28 Jun 2021 10:09:05 +0200 Subject: [PATCH] Linting --- src/bridge/__init__.py | 1 - src/core/cs3iface.py | 2 +- src/core/discovery.py | 18 ++++++++++-------- src/core/ioplocks.py | 8 ++++---- src/core/wopi.py | 14 +++++++------- src/wopiserver.py | 16 ++++++++++------ 6 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/bridge/__init__.py b/src/bridge/__init__.py index 641cd772..a4d2187a 100644 --- a/src/bridge/__init__.py +++ b/src/bridge/__init__.py @@ -8,7 +8,6 @@ import sys import time import traceback -import socket import logging import threading import atexit diff --git a/src/core/cs3iface.py b/src/core/cs3iface.py index f7c835e8..ed017f49 100644 --- a/src/core/cs3iface.py +++ b/src/core/cs3iface.py @@ -78,7 +78,7 @@ def stat(endpoint, fileid, userid, versioninv=0): ctx['log'].debug('msg="Stat result" data="%s"' % statInfo) if statInfo.info.type == cs3spr.RESOURCE_TYPE_CONTAINER: raise IOError('Is a directory') - elif statInfo.info.type not in (cs3spr.RESOURCE_TYPE_FILE, cs3spr.RESOURCE_TYPE_SYMLINK): + if statInfo.info.type not in (cs3spr.RESOURCE_TYPE_FILE, cs3spr.RESOURCE_TYPE_SYMLINK): ctx['log'].warning('msg="Stat: unexpected type" type="%d"' % statInfo.info.type) raise IOError('Unexpected type %d' % statInfo.info.type) # we base64-encode the inode so it can be used in a WOPISrc diff --git a/src/core/discovery.py b/src/core/discovery.py index bca4dae1..6efa39d9 100644 --- a/src/core/discovery.py +++ b/src/core/discovery.py @@ -8,9 +8,7 @@ from xml.etree import ElementTree as ET import http.client -import json import requests -import flask import bridge # convenience references to global entities @@ -18,15 +16,15 @@ log = None def registerapp(appname, appurl, appinturl, apikey=None): - '''Registers the given app in the internal endpoints list''' - '''For the time being, this is highly customized to keep backwards-compatibility. To be reviewed''' + '''Registers the given app in the internal endpoints list + For the time being, this is highly customized to keep backwards-compatibility. To be reviewed''' if not appinturl: appinturl = appurl try: discReq = requests.get(appurl + '/hosting/discovery', verify=False) except requests.exceptions.ConnectionError as e: log.error('msg="iopRegisterApp: failed to probe application" appurl="%s" response="%s"' % (appurl, e)) - return 'Error connecting to the provided app URL', http.client.NOT_FOUND + return if discReq.status_code == http.client.OK: discXml = ET.fromstring(discReq.content) @@ -61,7 +59,7 @@ def registerapp(appname, appurl, appinturl, apikey=None): srv.endpoints['.docx']['edit']) return - elif discReq.status_code == http.client.NOT_FOUND: + if discReq.status_code == http.client.NOT_FOUND: # try and scrape the app homepage to see if a bridge-supported app is found try: discReq = requests.get(appurl, verify=False).content.decode() @@ -96,7 +94,6 @@ def registerapp(appname, appurl, appinturl, apikey=None): def initappsregistry(): '''Initializes the CERNBox Office-like Apps Registry''' - # TODO to be deprecated in favour of a /wopi/iop/registerapp endpoint oos = srv.config.get('general', 'oosurl', fallback=None) if oos: registerapp('MSOffice', oos, oos) @@ -109,4 +106,9 @@ def initappsregistry(): with open('/var/run/secrets/codimd_apikey') as f: apikey = f.readline().strip('\n') registerapp('CodiMD', codimd, codimdint, apikey) - + etherpad = srv.config.get('general', 'etherpadurl', fallback=None) + etherpadint = srv.config.get('general', 'etherpadinturl', fallback=None) + if etherpad: + with open('/var/run/secrets/etherpad_apikey') as f: + apikey = f.readline().strip('\n') + registerapp('CodiMD', etherpad, etherpadint, apikey) diff --git a/src/core/ioplocks.py b/src/core/ioplocks.py index b0bcd118..4ab2614d 100644 --- a/src/core/ioplocks.py +++ b/src/core/ioplocks.py @@ -14,7 +14,7 @@ log = None -def lock(filename, userid, endpoint, isquery): +def ioplock(filename, userid, endpoint, isquery): '''Lock or query a given filename, see below for the specs of these APIs''' log.info('msg="cboxLock: start processing" filename="%s" request="%s" userid="%s"' % (filename, "query" if isquery else "create", userid)) @@ -22,7 +22,7 @@ def lock(filename, userid, endpoint, isquery): # first make sure the file itself exists try: filestat = st.statx(endpoint, filename, userid, versioninv=1) - except IOError as e: + except IOError: log.warning('msg="cboxLock: target not found or not a file" filename="%s"' % filename) return 'File not found or file is a directory', http.client.NOT_FOUND @@ -40,7 +40,7 @@ def lock(filename, userid, endpoint, isquery): log.info('msg="cboxLock: found existing Microsoft Office lock" filename="%s" lockmtime="%ld"' % (filename, mslockstat['mtime'])) return 'Previous lock exists', http.client.CONFLICT - except IOError as e: + except IOError: pass if isquery: @@ -194,7 +194,7 @@ def createLock(filestat, filename, userid, endpoint): return 'Error relocking file', http.client.INTERNAL_SERVER_ERROR -def unlock(filename, userid, endpoint): +def iopunlock(filename, userid, endpoint): '''Unlock a given filename. Used for OnlyOffice as they do not use WOPI (see cboxLock).''' log.info('msg="cboxUnlock: start processing" filename="%s"' % filename) try: diff --git a/src/core/wopi.py b/src/core/wopi.py index 1dec1317..e0e19ff0 100644 --- a/src/core/wopi.py +++ b/src/core/wopi.py @@ -10,10 +10,10 @@ import configparser import json import http.client -import jwt -import flask from urllib.parse import quote_plus as url_quote_plus from urllib.parse import unquote as url_unquote +import jwt +import flask import core.wopiutils as utils # convenience references to global entities @@ -35,7 +35,6 @@ def checkFileInfo(fileid): statInfo = st.statx(acctok['endpoint'], acctok['filename'], acctok['userid']) # compute some entities for the response wopiSrc = 'WOPISrc=%s&access_token=%s' % (utils.generateWopiSrc(fileid), flask.request.args['access_token']) - fExt = os.path.splitext(acctok['filename'])[1] # populate metadata for this file filemd = {} filemd['BaseFileName'] = filemd['BreadcrumbDocName'] = os.path.basename(acctok['filename']) @@ -154,7 +153,7 @@ def unlock(fileid, reqheaders, acctok, force=False): return 'OK', http.client.OK -def lock(fileid, reqheaders, acctok): +def setLock(fileid, reqheaders, acctok): '''Implements the Lock, RefreshLock, and UnlockAndRelock WOPI calls''' # cf. http://wopi.readthedocs.io/projects/wopirest/en/latest/files/Lock.html op = reqheaders['X-WOPI-Override'] @@ -363,7 +362,7 @@ def renameFile(fileid, reqheaders, acctok): return resp -def createNewFile(fileid, acctok): +def _createNewFile(fileid, acctok): '''Implements the editnew action as part of the PutFile WOPI call.''' log.info('msg="PutFile" user="%s" filename="%s" fileid="%s" action="editnew" token="%s"' % (acctok['userid'], acctok['filename'], fileid, flask.request.args['access_token'][-20:])) @@ -387,6 +386,7 @@ def createNewFile(fileid, acctok): def putFile(fileid): + '''Implements the PutFile WOPI call''' srv.refreshconfig() try: acctok = jwt.decode(flask.request.args['access_token'], srv.wopisecret, algorithms=['HS256']) @@ -394,14 +394,14 @@ def putFile(fileid): raise jwt.exceptions.ExpiredSignatureError if 'X-WOPI-Lock' not in flask.request.headers: # no lock given: assume we are in creation mode (cf. editnew WOPI action) - return createNewFile(fileid, acctok) + return _createNewFile(fileid, acctok) # otherwise, check that the caller holds the current lock on the file lock = flask.request.headers['X-WOPI-Lock'] retrievedLock = utils.retrieveWopiLock(fileid, 'PUTFILE', lock, acctok) if retrievedLock is None: return utils.makeConflictResponse('PUTFILE', retrievedLock, lock, '', acctok['filename'], \ 'Cannot overwrite unlocked file') - elif not utils.compareWopiLocks(retrievedLock, lock): + if not utils.compareWopiLocks(retrievedLock, lock): return utils.makeConflictResponse('PUTFILE', retrievedLock, lock, '', acctok['filename'], \ 'Cannot overwrite file locked by another application') # OK, we can save the file now diff --git a/src/wopiserver.py b/src/wopiserver.py index d0f2384c..6946cccd 100755 --- a/src/wopiserver.py +++ b/src/wopiserver.py @@ -372,13 +372,13 @@ def iopGetOpenFiles(): # @Wopi.app.route("/wopi/files/", methods=['GET']) def wopiCheckFileInfo(fileid): - '''Implements the CheckFileInfo WOPI call''' + '''The CheckFileInfo WOPI call''' return core.wopi.checkFileInfo(fileid) @Wopi.app.route("/wopi/files//contents", methods=['GET']) def wopiGetFile(fileid): - '''Implements the GetFile WOPI call''' + '''The GetFile WOPI call''' return core.wopi.getFile(fileid) @@ -396,7 +396,7 @@ def wopiFilesPost(fileid): # protect this call if the WOPI client does not have privileges return 'Attempting to perform a write operation using a read-only token', http.client.UNAUTHORIZED if op in ('LOCK', 'REFRESH_LOCK'): - return core.wopi.lock(fileid, headers, acctok) + return core.wopi.setLock(fileid, headers, acctok) if op == 'UNLOCK': return core.wopi.unlock(fileid, headers, acctok) if op == 'GET_LOCK': @@ -419,7 +419,7 @@ def wopiFilesPost(fileid): @Wopi.app.route("/wopi/files//contents", methods=['POST']) def wopiPutFile(fileid): - '''Implements the PutFile WOPI call''' + '''The PutFile WOPI call''' return core.wopi.putFile(fileid) @@ -454,7 +454,7 @@ def cboxLock(): filename = req.args['filename'] userid = req.args['userid'] if 'userid' in req.args else '0:0' endpoint = req.args['endpoint'] if 'endpoint' in req.args else 'default' - return core.ioplocks.lock(filename, userid, endpoint, req.method == 'GET') + return core.ioplocks.ioplock(filename, userid, endpoint, req.method == 'GET') @Wopi.app.route("/wopi/cbox/unlock", methods=['POST']) @@ -481,7 +481,7 @@ def cboxUnlock(): filename = req.args['filename'] userid = req.args['userid'] if 'userid' in req.args else '0:0' endpoint = req.args['endpoint'] if 'endpoint' in req.args else 'default' - return core.ioplocks.unlock(filename, userid, endpoint) + return core.ioplocks.iopunlock(filename, userid, endpoint) # @@ -489,6 +489,7 @@ def cboxUnlock(): # @Wopi.app.route("/wopi/bridge/open", methods=["GET"]) def bridgeOpen(): + '''The WOPI bridge open call''' try: wopisrc = url_unquote(flask.request.args['WOPISrc']) acctok = flask.request.args['access_token'] @@ -503,18 +504,21 @@ def bridgeOpen(): @Wopi.app.route("/wopi/bridge/", methods=["POST"]) @Wopi.metrics.do_not_track() def bridgeSave(docid): + '''The WOPI bridge save call''' return bridge.appsave(docid) @Wopi.app.route("/wopi/bridge/save", methods=["GET"]) @Wopi.metrics.do_not_track() def bridgeSave_deprecated(): + '''The WOPI bridge save call (deprecated)''' docid = flask.request.args.get('id') return bridge.appsave(docid) @Wopi.app.route("/wopi/bridge/list", methods=["GET"]) def bridgeList(): + '''The WOPI bridge list call''' return bridge.applist()