1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-24 18:47:23 +00:00
Code cosmetics
This commit is contained in:
OzzieIsaacs 2017-09-17 07:15:08 +02:00
parent b6f06658a2
commit b6b73d39c2
5 changed files with 42 additions and 80 deletions

View File

@ -20,7 +20,7 @@ def init_cache_busting(app):
app.logger.debug('Computing cache-busting values...') app.logger.debug('Computing cache-busting values...')
# compute file hashes # compute file hashes
for dirpath, dirnames, filenames in os.walk(static_folder): for dirpath, __, filenames in os.walk(static_folder):
for filename in filenames: for filename in filenames:
# compute version component # compute version component
rooted_filename = os.path.join(dirpath, filename) rooted_filename = os.path.join(dirpath, filename)

View File

@ -73,7 +73,8 @@ if not os.path.exists(dbpath):
migrate() migrate()
def getDrive(gauth=None): def getDrive(drive=None, gauth=None):
if not drive:
if not gauth: if not gauth:
gauth = GoogleAuth(settings_file='settings.yaml') gauth = GoogleAuth(settings_file='settings.yaml')
# Try to load saved client credentials # Try to load saved client credentials
@ -86,13 +87,13 @@ def getDrive(gauth=None):
gauth.Authorize() gauth.Authorize()
# Save the current credentials to a file # Save the current credentials to a file
return GoogleDrive(gauth) return GoogleDrive(gauth)
if drive.auth.access_token_expired:
drive.auth.Refresh()
return drive
def getEbooksFolder(drive=None): def getEbooksFolder(drive=None):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
ebooksFolder = "title = '%s' and 'root' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed = false" % config.config_google_drive_folder ebooksFolder = "title = '%s' and 'root' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed = false" % config.config_google_drive_folder
fileList = drive.ListFile({'q': ebooksFolder}).GetList() fileList = drive.ListFile({'q': ebooksFolder}).GetList()
@ -113,20 +114,14 @@ def getEbooksFolderId(drive=None):
def getFolderInFolder(parentId, folderName, drive=None): def getFolderInFolder(parentId, folderName, drive=None):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
folder = "title = '%s' and '%s' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed = false" % (folderName.replace("'", "\\'"), parentId) folder = "title = '%s' and '%s' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed = false" % (folderName.replace("'", "\\'"), parentId)
fileList = drive.ListFile({'q': folder}).GetList() fileList = drive.ListFile({'q': folder}).GetList()
return fileList[0] return fileList[0]
def getFile(pathId, fileName, drive=None): def getFile(pathId, fileName, drive=None):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
metaDataFile = "'%s' in parents and trashed = false and title = '%s'" % (pathId, fileName.replace("'", "\\'")) metaDataFile = "'%s' in parents and trashed = false and title = '%s'" % (pathId, fileName.replace("'", "\\'"))
fileList = drive.ListFile({'q': metaDataFile}).GetList() fileList = drive.ListFile({'q': metaDataFile}).GetList()
@ -134,10 +129,7 @@ def getFile(pathId, fileName, drive=None):
def getFolderId(path, drive=None): def getFolderId(path, drive=None):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
currentFolderId = getEbooksFolderId(drive) currentFolderId = getEbooksFolderId(drive)
sqlCheckPath = path if path[-1] == '/' else path + '/' sqlCheckPath = path if path[-1] == '/' else path + '/'
storedPathName = session.query(GdriveId).filter(GdriveId.path == sqlCheckPath).first() storedPathName = session.query(GdriveId).filter(GdriveId.path == sqlCheckPath).first()
@ -168,10 +160,7 @@ def getFolderId(path, drive=None):
def getFileFromEbooksFolder(drive, path, fileName): def getFileFromEbooksFolder(drive, path, fileName):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
if path: if path:
# sqlCheckPath=path if path[-1] =='/' else path + '/' # sqlCheckPath=path if path[-1] =='/' else path + '/'
folderId = getFolderId(path, drive) folderId = getFolderId(path, drive)
@ -182,10 +171,7 @@ def getFileFromEbooksFolder(drive, path, fileName):
def copyDriveFileRemote(drive, origin_file_id, copy_title): def copyDriveFileRemote(drive, origin_file_id, copy_title):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
copied_file = {'title': copy_title} copied_file = {'title': copy_title}
try: try:
file_data = drive.auth.service.files().copy( file_data = drive.auth.service.files().copy(
@ -197,19 +183,13 @@ def copyDriveFileRemote(drive, origin_file_id, copy_title):
def downloadFile(drive, path, filename, output): def downloadFile(drive, path, filename, output):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
f = getFileFromEbooksFolder(drive, path, filename) f = getFileFromEbooksFolder(drive, path, filename)
f.GetContentFile(output) f.GetContentFile(output)
def backupCalibreDbAndOptionalDownload(drive, f=None): def backupCalibreDbAndOptionalDownload(drive, f=None):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
metaDataFile = "'%s' in parents and title = 'metadata.db' and trashed = false" % getEbooksFolderId() metaDataFile = "'%s' in parents and title = 'metadata.db' and trashed = false" % getEbooksFolderId()
fileList = drive.ListFile({'q': metaDataFile}).GetList() fileList = drive.ListFile({'q': metaDataFile}).GetList()
@ -221,12 +201,9 @@ def backupCalibreDbAndOptionalDownload(drive, f=None):
def copyToDrive(drive, uploadFile, createRoot, replaceFiles, def copyToDrive(drive, uploadFile, createRoot, replaceFiles,
ignoreFiles=[], ignoreFiles= ignoreFiles or [],
parent=None, prevDir=''): parent=None, prevDir=''):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
isInitial = not bool(parent) isInitial = not bool(parent)
if not parent: if not parent:
parent = getEbooksFolder(drive) parent = getEbooksFolder(drive)
@ -254,10 +231,7 @@ def copyToDrive(drive, uploadFile, createRoot, replaceFiles,
def uploadFileToEbooksFolder(drive, destFile, f): def uploadFileToEbooksFolder(drive, destFile, f):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
parent = getEbooksFolder(drive) parent = getEbooksFolder(drive)
splitDir = destFile.split('/') splitDir = destFile.split('/')
for i, x in enumerate(splitDir): for i, x in enumerate(splitDir):
@ -281,10 +255,7 @@ def uploadFileToEbooksFolder(drive, destFile, f):
def watchChange(drive, channel_id, channel_type, channel_address, def watchChange(drive, channel_id, channel_type, channel_address,
channel_token=None, expiration=None): channel_token=None, expiration=None):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
# Watch for all changes to a user's Drive. # Watch for all changes to a user's Drive.
# Args: # Args:
# service: Drive API service instance. # service: Drive API service instance.
@ -327,10 +298,7 @@ def watchFile(drive, file_id, channel_id, channel_type, channel_address,
Raises: Raises:
apiclient.errors.HttpError: if http request to create channel fails. apiclient.errors.HttpError: if http request to create channel fails.
""" """
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
body = { body = {
'id': channel_id, 'id': channel_id,
@ -353,10 +321,7 @@ def stopChannel(drive, channel_id, resource_id):
Raises: Raises:
apiclient.errors.HttpError: if http request to create channel fails. apiclient.errors.HttpError: if http request to create channel fails.
""" """
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
# service=drive.auth.service # service=drive.auth.service
body = { body = {
'id': channel_id, 'id': channel_id,
@ -366,10 +331,7 @@ def stopChannel(drive, channel_id, resource_id):
def getChangeById (drive, change_id): def getChangeById (drive, change_id):
if not drive: drive = getDrive(drive)
drive = getDrive()
if drive.auth.access_token_expired:
drive.auth.Refresh()
# Print a single Change resource information. # Print a single Change resource information.
# #
# Args: # Args:

View File

@ -158,7 +158,7 @@ class Gauth:
@Singleton @Singleton
class Gdrive: class Gdrive:
def __init__(self): def __init__(self):
self.drive = gdriveutils.getDrive(Gauth.Instance().auth) self.drive = gdriveutils.getDrive(gauth=Gauth.Instance().auth)
class ReverseProxied(object): class ReverseProxied(object):
@ -794,7 +794,7 @@ def feed_category(book_id):
off = request.args.get("offset") off = request.args.get("offset")
if not off: if not off:
off = 0 off = 0
entries, random, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1), entries, __, pagination = fill_indexpage((int(off) / (int(config.config_books_per_page)) + 1),
db.Books, db.Books.tags.any(db.Tags.id == book_id), db.Books.timestamp.desc()) db.Books, db.Books.tags.any(db.Tags.id == book_id), db.Books.timestamp.desc())
xml = render_title_template('feed.xml', entries=entries, pagination=pagination) xml = render_title_template('feed.xml', entries=entries, pagination=pagination)
response = make_response(xml) response = make_response(xml)
@ -1129,7 +1129,7 @@ def author_list():
@app.route("/author/<int:book_id>/<int:page>'") @app.route("/author/<int:book_id>/<int:page>'")
@login_required_if_no_ano @login_required_if_no_ano
def author(book_id, page): def author(book_id, page):
entries, random, pagination = fill_indexpage(page, db.Books, db.Books.authors.any(db.Authors.id == book_id), entries, __, pagination = fill_indexpage(page, db.Books, db.Books.authors.any(db.Authors.id == book_id),
db.Books.timestamp.desc()) db.Books.timestamp.desc())
if entries is None: if entries is None:
flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error") flash(_(u"Error opening eBook. File does not exist or file is not accessible:"), category="error")
@ -2172,7 +2172,7 @@ def delete_shelf(shelf_id):
if current_user.role_admin(): if current_user.role_admin():
deleted = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).delete() deleted = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).delete()
else: else:
if not cur_shelf.is_public and not cur_shelf.user_id == int(current_user.id) \ if (not cur_shelf.is_public and cur_shelf.user_id == int(current_user.id)) \
or (cur_shelf.is_public and current_user.role_edit_shelfs()): or (cur_shelf.is_public and current_user.role_edit_shelfs()):
deleted = ub.session.query(ub.Shelf).filter(ub.or_(ub.and_(ub.Shelf.user_id == int(current_user.id), deleted = ub.session.query(ub.Shelf).filter(ub.or_(ub.and_(ub.Shelf.user_id == int(current_user.id),
ub.Shelf.id == shelf_id), ub.Shelf.id == shelf_id),