Skip to content

Commit

Permalink
Bugfix logging with gdrive
Browse files Browse the repository at this point in the history
Update optional-requirements.txt
  • Loading branch information
OzzieIsaacs committed Mar 28, 2022
1 parent 036cd7b commit 73d359a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
29 changes: 15 additions & 14 deletions cps/gdriveutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from sqlalchemy.orm import declarative_base
except ImportError:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.exc import OperationalError, InvalidRequestError
from sqlalchemy.exc import OperationalError, InvalidRequestError, IntegrityError
from sqlalchemy.sql.expression import text

#try:
Expand Down Expand Up @@ -81,7 +81,7 @@
if not logger.is_debug_enabled():
logger.get('googleapiclient.discovery').setLevel(logger.logging.ERROR)
else:
log.debug("Cannot import pydrive, httplib2, using gdrive will not work: %s", importError)
log.debug("Cannot import pydrive, httplib2, using gdrive will not work: {}".format(importError))


class Singleton:
Expand Down Expand Up @@ -213,7 +213,7 @@ def getDrive(drive=None, gauth=None):
try:
gauth.Refresh()
except RefreshError as e:
log.error("Google Drive error: %s", e)
log.error("Google Drive error: {}".format(e))
except Exception as ex:
log.error_or_exception(ex)
else:
Expand All @@ -225,7 +225,7 @@ def getDrive(drive=None, gauth=None):
try:
drive.auth.Refresh()
except RefreshError as e:
log.error("Google Drive error: %s", e)
log.error("Google Drive error: {}".format(e))
return drive

def listRootFolders():
Expand All @@ -234,7 +234,7 @@ def listRootFolders():
folder = "'root' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed = false"
fileList = drive.ListFile({'q': folder}).GetList()
except (ServerNotFoundError, ssl.SSLError, RefreshError) as e:
log.info("GDrive Error %s" % e)
log.info("GDrive Error {}".format(e))
fileList = []
return fileList

Expand Down Expand Up @@ -272,7 +272,7 @@ def getEbooksFolderId(drive=None):
try:
session.commit()
except OperationalError as ex:
log.error_or_exception('Database error: %s', ex)
log.error_or_exception('Database error: {}'.format(ex))
session.rollback()
return gDriveId.gdrive_id

Expand All @@ -288,6 +288,7 @@ def getFile(pathId, fileName, drive):

def getFolderId(path, drive):
# drive = getDrive(drive)
currentFolderId = None
try:
currentFolderId = getEbooksFolderId(drive)
sqlCheckPath = path if path[-1] == '/' else path + '/'
Expand Down Expand Up @@ -320,8 +321,8 @@ def getFolderId(path, drive):
session.commit()
else:
currentFolderId = storedPathName.gdrive_id
except OperationalError as ex:
log.error_or_exception('Database error: %s', ex)
except (OperationalError, IntegrityError) as ex:
log.error_or_exception('Database error: {}'.format(ex))
session.rollback()
except ApiRequestError as ex:
log.error('{} {}'.format(ex.error['message'], path))
Expand Down Expand Up @@ -545,7 +546,7 @@ def deleteDatabaseOnChange():
session.commit()
except (OperationalError, InvalidRequestError) as ex:
session.rollback()
log.error_or_exception('Database error: %s', ex)
log.error_or_exception('Database error: {}'.format(ex))


def updateGdriveCalibreFromLocal():
Expand All @@ -563,7 +564,7 @@ def updateDatabaseOnEdit(ID,newPath):
try:
session.commit()
except OperationalError as ex:
log.error_or_exception('Database error: %s', ex)
log.error_or_exception('Database error: {}'.format(ex))
session.rollback()


Expand All @@ -573,7 +574,7 @@ def deleteDatabaseEntry(ID):
try:
session.commit()
except OperationalError as ex:
log.error_or_exception('Database error: %s', ex)
log.error_or_exception('Database error: {}'.format(ex))
session.rollback()


Expand All @@ -594,7 +595,7 @@ def get_cover_via_gdrive(cover_path):
try:
session.commit()
except OperationalError as ex:
log.error_or_exception('Database error: %s', ex)
log.error_or_exception('Database error: {}'.format(ex))
session.rollback()
return df.metadata.get('webContentLink')
else:
Expand All @@ -616,15 +617,15 @@ def do_gdrive_download(df, headers, convert_encoding=False):

def stream(convert_encoding):
for byte in s:
headers = {"Range": 'bytes=%s-%s' % (byte[0], byte[1])}
headers = {"Range": 'bytes={}-{}'.format(byte[0], byte[1])}
resp, content = df.auth.Get_Http_Object().request(download_url, headers=headers)
if resp.status == 206:
if convert_encoding:
result = chardet.detect(content)
content = content.decode(result['encoding']).encode('utf-8')
yield content
else:
log.warning('An error occurred: %s', resp)
log.warning('An error occurred: {}'.format(resp))
return
return Response(stream_with_context(stream(convert_encoding)), headers=headers)

Expand Down
2 changes: 1 addition & 1 deletion cps/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ def get_book_cover_internal(book, use_generic_cover_on_failure):
if path:
return redirect(path)
else:
log.error('%s/cover.jpg not found on Google Drive', book.path)
log.error('{}/cover.jpg not found on Google Drive'.format(book.path))
return get_cover_on_failure(use_generic_cover_on_failure)
except Exception as ex:
log.error_or_exception(ex)
Expand Down
4 changes: 2 additions & 2 deletions optional-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# GDrive Integration
google-api-python-client>=1.7.11,<2.42.0
google-api-python-client>=1.7.11,<2.43.0
gevent>20.6.0,<22.0.0
greenlet>=0.4.17,<1.2.0
httplib2>=0.9.2,<0.21.0
Expand All @@ -13,7 +13,7 @@ rsa>=3.4.2,<4.9.0

# Gmail
google-auth-oauthlib>=0.4.3,<0.6.0
google-api-python-client>=1.7.11,<2.42.0
google-api-python-client>=1.7.11,<2.43.0

# goodreads
goodreads>=0.3.2,<0.4.0
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ install_requires =

[options.extras_require]
gdrive =
google-api-python-client>=1.7.11,<2.37.0
google-api-python-client>=1.7.11,<2.43.0
gevent>20.6.0,<22.0.0
greenlet>=0.4.17,<1.2.0
httplib2>=0.9.2,<0.21.0
Expand All @@ -72,7 +72,7 @@ gdrive =
rsa>=3.4.2,<4.9.0
gmail =
google-auth-oauthlib>=0.4.3,<0.5.0
google-api-python-client>=1.7.11,<2.37.0
google-api-python-client>=1.7.11,<2.43.0
goodreads =
goodreads>=0.3.2,<0.4.0
python-Levenshtein>=0.12.0,<0.13.0
Expand Down

0 comments on commit 73d359a

Please sign in to comment.