From b6b73d39c2e637afd5382ead82b57a0d43d682a9 Mon Sep 17 00:00:00 2001 From: OzzieIsaacs Date: Sun, 17 Sep 2017 07:15:08 +0200 Subject: [PATCH] Fix #329 Code cosmetics --- cps/cache_buster.py | 2 +- cps/gdriveutils.py | 102 ++++++++++++-------------------------- cps/static/js/get_meta.js | 4 +- cps/static/js/main.js | 6 +-- cps/web.py | 8 +-- 5 files changed, 42 insertions(+), 80 deletions(-) diff --git a/cps/cache_buster.py b/cps/cache_buster.py index 2e2b9869..0b0b15fb 100644 --- a/cps/cache_buster.py +++ b/cps/cache_buster.py @@ -20,7 +20,7 @@ def init_cache_busting(app): app.logger.debug('Computing cache-busting values...') # compute file hashes - for dirpath, dirnames, filenames in os.walk(static_folder): + for dirpath, __, filenames in os.walk(static_folder): for filename in filenames: # compute version component rooted_filename = os.path.join(dirpath, filename) diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 550426ca..efd11acb 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -73,26 +73,27 @@ if not os.path.exists(dbpath): migrate() -def getDrive(gauth=None): - if not gauth: - gauth = GoogleAuth(settings_file='settings.yaml') - # Try to load saved client credentials - gauth.LoadCredentialsFile("gdrive_credentials") - if gauth.access_token_expired: - # Refresh them if expired - gauth.Refresh() - else: - # Initialize the saved creds - gauth.Authorize() - # Save the current credentials to a file - return GoogleDrive(gauth) - - -def getEbooksFolder(drive=None): +def getDrive(drive=None, gauth=None): if not drive: - drive = getDrive() + if not gauth: + gauth = GoogleAuth(settings_file='settings.yaml') + # Try to load saved client credentials + gauth.LoadCredentialsFile("gdrive_credentials") + if gauth.access_token_expired: + # Refresh them if expired + gauth.Refresh() + else: + # Initialize the saved creds + gauth.Authorize() + # Save the current credentials to a file + return GoogleDrive(gauth) if drive.auth.access_token_expired: drive.auth.Refresh() + return drive + + +def getEbooksFolder(drive=None): + drive = getDrive(drive) 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() @@ -113,20 +114,14 @@ def getEbooksFolderId(drive=None): def getFolderInFolder(parentId, folderName, drive=None): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) 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() return fileList[0] def getFile(pathId, fileName, drive=None): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) metaDataFile = "'%s' in parents and trashed = false and title = '%s'" % (pathId, fileName.replace("'", "\\'")) fileList = drive.ListFile({'q': metaDataFile}).GetList() @@ -134,10 +129,7 @@ def getFile(pathId, fileName, drive=None): def getFolderId(path, drive=None): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) currentFolderId = getEbooksFolderId(drive) sqlCheckPath = path if path[-1] == '/' else path + '/' storedPathName = session.query(GdriveId).filter(GdriveId.path == sqlCheckPath).first() @@ -168,10 +160,7 @@ def getFolderId(path, drive=None): def getFileFromEbooksFolder(drive, path, fileName): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) if path: # sqlCheckPath=path if path[-1] =='/' else path + '/' folderId = getFolderId(path, drive) @@ -182,10 +171,7 @@ def getFileFromEbooksFolder(drive, path, fileName): def copyDriveFileRemote(drive, origin_file_id, copy_title): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) copied_file = {'title': copy_title} try: 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): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) f = getFileFromEbooksFolder(drive, path, filename) f.GetContentFile(output) def backupCalibreDbAndOptionalDownload(drive, f=None): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) metaDataFile = "'%s' in parents and title = 'metadata.db' and trashed = false" % getEbooksFolderId() fileList = drive.ListFile({'q': metaDataFile}).GetList() @@ -221,12 +201,9 @@ def backupCalibreDbAndOptionalDownload(drive, f=None): def copyToDrive(drive, uploadFile, createRoot, replaceFiles, - ignoreFiles=[], + ignoreFiles= ignoreFiles or [], parent=None, prevDir=''): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) isInitial = not bool(parent) if not parent: parent = getEbooksFolder(drive) @@ -254,10 +231,7 @@ def copyToDrive(drive, uploadFile, createRoot, replaceFiles, def uploadFileToEbooksFolder(drive, destFile, f): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) parent = getEbooksFolder(drive) splitDir = destFile.split('/') for i, x in enumerate(splitDir): @@ -281,10 +255,7 @@ def uploadFileToEbooksFolder(drive, destFile, f): def watchChange(drive, channel_id, channel_type, channel_address, channel_token=None, expiration=None): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) # Watch for all changes to a user's Drive. # Args: # service: Drive API service instance. @@ -327,10 +298,7 @@ def watchFile(drive, file_id, channel_id, channel_type, channel_address, Raises: apiclient.errors.HttpError: if http request to create channel fails. """ - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) body = { 'id': channel_id, @@ -353,10 +321,7 @@ def stopChannel(drive, channel_id, resource_id): Raises: apiclient.errors.HttpError: if http request to create channel fails. """ - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) # service=drive.auth.service body = { 'id': channel_id, @@ -366,10 +331,7 @@ def stopChannel(drive, channel_id, resource_id): def getChangeById (drive, change_id): - if not drive: - drive = getDrive() - if drive.auth.access_token_expired: - drive.auth.Refresh() + drive = getDrive(drive) # Print a single Change resource information. # # Args: diff --git a/cps/static/js/get_meta.js b/cps/static/js/get_meta.js index 8dd34cdf..0a8670dc 100644 --- a/cps/static/js/get_meta.js +++ b/cps/static/js/get_meta.js @@ -3,8 +3,8 @@ * Created by idalin * Google Books api document: https://developers.google.com/books/docs/v1/using * Douban Books api document: https://developers.douban.com/wiki/?title=book_v2 (Chinese Only) - */ - /* global _, i18nMsg, tinymce */ +*/ +/* global _, i18nMsg, tinymce */ var dbResults = []; var ggResults = []; diff --git a/cps/static/js/main.js b/cps/static/js/main.js index 267c6d97..524abdb0 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -63,13 +63,13 @@ $(function() { $(".load-more .row").infinitescroll({ debug: false, navSelector : ".pagination", - // selector for the paged navigation (it will be hidden) + // selector for the paged navigation (it will be hidden) nextSelector : ".pagination a:last", - // selector for the NEXT link (to page 2) + // selector for the NEXT link (to page 2) itemSelector : ".load-more .book", animate : true, extraScrollPx: 300 - // selector for all items you'll retrieve + // selector for all items you'll retrieve }, function(data) { $(".load-more .row").isotope( "appended", $(data), null ); }); diff --git a/cps/web.py b/cps/web.py index 4406ddba..8d2674df 100755 --- a/cps/web.py +++ b/cps/web.py @@ -158,7 +158,7 @@ class Gauth: @Singleton class Gdrive: def __init__(self): - self.drive = gdriveutils.getDrive(Gauth.Instance().auth) + self.drive = gdriveutils.getDrive(gauth=Gauth.Instance().auth) class ReverseProxied(object): @@ -794,7 +794,7 @@ def feed_category(book_id): off = request.args.get("offset") if not off: 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()) xml = render_title_template('feed.xml', entries=entries, pagination=pagination) response = make_response(xml) @@ -1129,7 +1129,7 @@ def author_list(): @app.route("/author//'") @login_required_if_no_ano 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()) if entries is None: 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(): deleted = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).delete() 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()): deleted = ub.session.query(ub.Shelf).filter(ub.or_(ub.and_(ub.Shelf.user_id == int(current_user.id), ub.Shelf.id == shelf_id),