Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed Jun 28, 2021
1 parent db84401 commit 45c7219
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
1 change: 0 additions & 1 deletion src/bridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import sys
import time
import traceback
import socket
import logging
import threading
import atexit
Expand Down
2 changes: 1 addition & 1 deletion src/core/cs3iface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions src/core/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@

from xml.etree import ElementTree as ET
import http.client
import json
import requests
import flask
import bridge

# convenience references to global entities
srv = None
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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
8 changes: 4 additions & 4 deletions src/core/ioplocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
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))

# 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

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions src/core/wopi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'])
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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:]))
Expand All @@ -387,21 +386,22 @@ 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'])
if acctok['exp'] < time.time():
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
Expand Down
16 changes: 10 additions & 6 deletions src/wopiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,13 @@ def iopGetOpenFiles():
#
@Wopi.app.route("/wopi/files/<fileid>", methods=['GET'])
def wopiCheckFileInfo(fileid):
'''Implements the CheckFileInfo WOPI call'''
'''The CheckFileInfo WOPI call'''
return core.wopi.checkFileInfo(fileid)


@Wopi.app.route("/wopi/files/<fileid>/contents", methods=['GET'])
def wopiGetFile(fileid):
'''Implements the GetFile WOPI call'''
'''The GetFile WOPI call'''
return core.wopi.getFile(fileid)


Expand All @@ -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':
Expand All @@ -419,7 +419,7 @@ def wopiFilesPost(fileid):

@Wopi.app.route("/wopi/files/<fileid>/contents", methods=['POST'])
def wopiPutFile(fileid):
'''Implements the PutFile WOPI call'''
'''The PutFile WOPI call'''
return core.wopi.putFile(fileid)


Expand Down Expand Up @@ -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'])
Expand All @@ -481,14 +481,15 @@ 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)


#
# Bridge functionality
#
@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']
Expand All @@ -503,18 +504,21 @@ def bridgeOpen():
@Wopi.app.route("/wopi/bridge/<docid>", 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()


Expand Down

0 comments on commit 45c7219

Please sign in to comment.