From a334ef28e7de72692d763249a7a0108ee2e9b9f4 Mon Sep 17 00:00:00 2001 From: Daniel Pavel Date: Sun, 14 Jul 2019 20:38:07 +0300 Subject: [PATCH] about page: build the versions dictionary only once --- cps/about.py | 67 ++++++++++++++++++++---------------------------- cps/converter.py | 52 +++++++++++++++++-------------------- cps/server.py | 5 ++-- 3 files changed, 53 insertions(+), 71 deletions(-) diff --git a/cps/about.py b/cps/about.py index 42ffe559..451b6411 100644 --- a/cps/about.py +++ b/cps/about.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web) @@ -23,56 +22,46 @@ from __future__ import division, print_function, unicode_literals import sys -import requests +import sqlite3 +from collections import OrderedDict -from flask import Blueprint -from flask import __version__ as flaskVersion +import babel, pytz, requests, sqlalchemy +import werkzeug, flask, flask_login, flask_principal, jinja2 from flask_babel import gettext as _ -from flask_principal import __version__ as flask_principalVersion -from flask_login import login_required -try: - from flask_login import __version__ as flask_loginVersion -except ImportError: - from flask_login.__about__ import __version__ as flask_loginVersion -from werkzeug import __version__ as werkzeugVersion -from babel import __version__ as babelVersion -from jinja2 import __version__ as jinja2Version -from pytz import __version__ as pytzVersion -from sqlalchemy import __version__ as sqlalchemyVersion - -from . import db, converter, uploader -from .isoLanguages import __version__ as iso639Version -from .server import VERSION as serverVersion +from . import db, converter, uploader, server, isoLanguages from .web import render_title_template -about = Blueprint('about', __name__) +about = flask.Blueprint('about', __name__) + + +_VERSIONS = OrderedDict( + Python=sys.version, + WebServer=server.VERSION, + Flask=flask.__version__, + Flask_Login=flask_login.__version__, + Flask_Principal=flask_principal.__version__, + Werkzeug=werkzeug.__version__, + Babel=babel.__version__, + Jinja2=jinja2.__version__, + Requests=requests.__version__, + SqlAlchemy=sqlalchemy.__version__, + pySqlite=sqlite3.version, + SQLite=sqlite3.sqlite_version, + iso639=isoLanguages.__version__, + pytz=pytz.__version__, +) +_VERSIONS.update(uploader.get_versions()) @about.route("/stats") -@login_required +@flask_login.login_required def stats(): counter = db.session.query(db.Books).count() authors = db.session.query(db.Authors).count() categorys = db.session.query(db.Tags).count() series = db.session.query(db.Series).count() - versions = uploader.get_versions() - versions['Babel'] = 'v' + babelVersion - versions['Sqlalchemy'] = 'v' + sqlalchemyVersion - versions['Werkzeug'] = 'v' + werkzeugVersion - versions['Jinja2'] = 'v' + jinja2Version - versions['Flask'] = 'v' + flaskVersion - versions['Flask Login'] = 'v' + flask_loginVersion - versions['Flask Principal'] = 'v' + flask_principalVersion - versions['Iso 639'] = 'v' + iso639Version - versions['pytz'] = 'v' + pytzVersion - - versions['Requests'] = 'v' + requests.__version__ - versions['pySqlite'] = 'v' + db.session.bind.dialect.dbapi.version - versions['Sqlite'] = 'v' + db.session.bind.dialect.dbapi.sqlite_version - versions.update(converter.versioncheck()) - versions.update(serverVersion) - versions['Python'] = sys.version - return render_title_template('stats.html', bookcounter=counter, authorcounter=authors, versions=versions, + _VERSIONS['ebook converter'] = _(converter.get_version()) + return render_title_template('stats.html', bookcounter=counter, authorcounter=authors, versions=_VERSIONS, categorycounter=categorys, seriecounter=series, title=_(u"Statistics"), page="stat") diff --git a/cps/converter.py b/cps/converter.py index 6dc44383..32bc273f 100644 --- a/cps/converter.py +++ b/cps/converter.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web) @@ -21,41 +20,36 @@ from __future__ import division, print_function, unicode_literals import os import re -from flask_babel import gettext as _ - -from . import config +from . import config, logger from .subproc_wrapper import process_wait -def versionKindle(): - versions = _(u'not installed') - if os.path.exists(config.config_converterpath): - try: - for lines in process_wait(config.config_converterpath): - if re.search('Amazon kindlegen\(', lines): - versions = lines - except Exception: - versions = _(u'Excecution permissions missing') - return {'kindlegen' : versions} +log = logger.create() +_NOT_CONFIGURED = 'not configured' +_NOT_INSTALLED = 'not installed' +_EXECUTION_ERROR = 'Execution permissions missing' -def versionCalibre(): - versions = _(u'not installed') - if os.path.exists(config.config_converterpath): + +def _get_command_version(path, pattern, argument=None): + if os.path.exists(path): + command = [path] + if argument: + command.append(argument) try: - for lines in process_wait([config.config_converterpath, '--version']): - if re.search('ebook-convert.*\(calibre', lines): - versions = lines - except Exception: - versions = _(u'Excecution permissions missing') - return {'Calibre converter' : versions} + for line in process_wait(command): + if re.search(pattern, line): + return line + except Exception as ex: + log.warning("%s: %s", path, ex) + return _EXECUTION_ERROR + return _NOT_INSTALLED -def versioncheck(): +def get_version(): + version = None if config.config_ebookconverter == 1: - return versionKindle() + version = _get_command_version(config.config_converterpath, r'Amazon kindlegen\(') elif config.config_ebookconverter == 2: - return versionCalibre() - else: - return {'ebook_converter':_(u'not configured')} - + version = _get_command_version(config.config_converterpath, r'ebook-convert.*\(calibre', '--version') + return version or _NOT_CONFIGURED diff --git a/cps/server.py b/cps/server.py index ada9d156..fbb14b84 100644 --- a/cps/server.py +++ b/cps/server.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python # -*- coding: utf-8 -*- # This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web) @@ -28,14 +27,14 @@ try: from gevent.pywsgi import WSGIServer from gevent.pool import Pool from gevent import __version__ as _version - VERSION = {'Gevent': 'v' + _version} + VERSION = 'Gevent ' + _version _GEVENT = True except ImportError: from tornado.wsgi import WSGIContainer from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado import version as _version - VERSION = {'Tornado': 'v' + _version} + VERSION = 'Tornado ' + _version _GEVENT = False from . import logger