From 23e47ba4e60e692f6daad8a4616c29e625b895fa Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 31 Jan 2022 18:09:23 +0100 Subject: [PATCH 1/2] Fix for #2299 (scholarly requires Internet connection at startup) --- cps/about.py | 6 +++++- cps/editbooks.py | 7 ------- cps/metadata_provider/scholar.py | 9 ++++++++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cps/about.py b/cps/about.py index 7e839520..54f0eb91 100644 --- a/cps/about.py +++ b/cps/about.py @@ -58,10 +58,14 @@ try: except ImportError: greenlet_Version = None +try: + from fake_useragent.errors import FakeUserAgentError +except (ImportError): + FakeUserAgentError = BaseException try: from scholarly import scholarly scholarly_version = _(u'installed') -except ImportError: +except (ImportError, FakeUserAgentError): scholarly_version = _(u'not installed') from . import services diff --git a/cps/editbooks.py b/cps/editbooks.py index f0b8fb41..2f3b1b88 100755 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -33,13 +33,6 @@ try: except ImportError: pass -# Improve this to check if scholarly is available in a global way, like other pythonic libraries -try: - from scholarly import scholarly - have_scholar = True -except ImportError: - have_scholar = False - from flask import Blueprint, request, flash, redirect, url_for, abort, Markup, Response from flask_babel import gettext as _ from flask_login import current_user, login_required diff --git a/cps/metadata_provider/scholar.py b/cps/metadata_provider/scholar.py index df387831..2a8d3cca 100644 --- a/cps/metadata_provider/scholar.py +++ b/cps/metadata_provider/scholar.py @@ -16,7 +16,14 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from scholarly import scholarly +try: + from fake_useragent.errors import FakeUserAgentError +except (ImportError): + FakeUserAgentError = BaseException +try: + from scholarly import scholarly +except FakeUserAgentError: + raise ImportError("No module named 'scholarly'") from cps.services.Metadata import Metadata From 95e0255aa1da646c50d863a6bb450ca6f06a96b0 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 31 Jan 2022 19:19:25 +0100 Subject: [PATCH 2/2] Code cosmetics, removed scholarly from about section --- cps/about.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/cps/about.py b/cps/about.py index 54f0eb91..7ff178e3 100644 --- a/cps/about.py +++ b/cps/about.py @@ -25,8 +25,15 @@ import platform import sqlite3 from collections import OrderedDict -import babel, pytz, requests, sqlalchemy -import werkzeug, flask, flask_login, flask_principal, jinja2 +import babel +import pytz +import requests +import sqlalchemy +import werkzeug +import flask +import flask_login +import flask_principal +import jinja2 from flask_babel import gettext as _ try: from flask_wtf import __version__ as flaskwtf_version @@ -58,16 +65,6 @@ try: except ImportError: greenlet_Version = None -try: - from fake_useragent.errors import FakeUserAgentError -except (ImportError): - FakeUserAgentError = BaseException -try: - from scholarly import scholarly - scholarly_version = _(u'installed') -except (ImportError, FakeUserAgentError): - scholarly_version = _(u'not installed') - from . import services about = flask.Blueprint('about', __name__) @@ -82,8 +79,8 @@ if constants.NIGHTLY_VERSION[0] == "$Format:%H$": calibre_web_version = constants.STABLE_VERSION['version'] else: calibre_web_version = (constants.STABLE_VERSION['version'] + ' - ' - + constants.NIGHTLY_VERSION[0].replace('%','%%') + ' - ' - + constants.NIGHTLY_VERSION[1].replace('%','%%')) + + constants.NIGHTLY_VERSION[0].replace('%', '%%') + ' - ' + + constants.NIGHTLY_VERSION[1].replace('%', '%%')) if getattr(sys, 'frozen', False): calibre_web_version += " - Exe-Version" elif constants.HOME_CONFIG: @@ -91,7 +88,7 @@ elif constants.HOME_CONFIG: if not ret: _VERSIONS = OrderedDict( - Platform = '{0[0]} {0[2]} {0[3]} {0[4]} {0[5]}'.format(platform.uname()), + Platform='{0[0]} {0[2]} {0[3]} {0[4]} {0[5]}'.format(platform.uname()), Python=sys.version, Calibre_Web=calibre_web_version, WebServer=server.VERSION, @@ -109,7 +106,6 @@ if not ret: iso639=isoLanguages.__version__, pytz=pytz.__version__, Unidecode=unidecode_version, - Scholarly=scholarly_version, Flask_SimpleLDAP=u'installed' if bool(services.ldap) else None, python_LDAP=services.ldapVersion if bool(services.ldapVersion) else None, Goodreads=u'installed' if bool(services.goodreads_support) else None, @@ -121,23 +117,25 @@ if not ret: _VERSIONS.update(uploader.get_versions(True)) else: _VERSIONS = OrderedDict( - Platform = '{0[0]} {0[2]} {0[3]} {0[4]} {0[5]}'.format(platform.uname()), - Python = sys.version, + Platform='{0[0]} {0[2]} {0[3]} {0[4]} {0[5]}'.format(platform.uname()), + Python=sys.version, Calibre_Web=calibre_web_version, - Werkzeug = werkzeug.__version__, + Werkzeug=werkzeug.__version__, Jinja2=jinja2.__version__, - pySqlite = sqlite3.version, - SQLite = sqlite3.sqlite_version, + pySqlite=sqlite3.version, + SQLite=sqlite3.sqlite_version, ) _VERSIONS.update(ret) _VERSIONS.update(uploader.get_versions(False)) + def collect_stats(): _VERSIONS['ebook converter'] = _(converter.get_calibre_version()) _VERSIONS['unrar'] = _(converter.get_unrar_version()) _VERSIONS['kepubify'] = _(converter.get_kepubify_version()) return _VERSIONS + @about.route("/stats") @flask_login.login_required def stats():