diff --git a/cps/book_formats.py b/cps/book_formats.py index 90c9c5a3..1e0a08bd 100644 --- a/cps/book_formats.py +++ b/cps/book_formats.py @@ -127,11 +127,11 @@ def get_versions(): else: IVersion = _(u'not installed') if use_pdf_meta: - PVersion=PyPdfVersion + PVersion='v'+PyPdfVersion else: PVersion=_(u'not installed') if lxmlversion: - XVersion = '.'.join(map(str, lxmlversion)) + XVersion = 'v'+'.'.join(map(str, lxmlversion)) else: XVersion = _(u'not installed') - return {'ImageVersion': IVersion, 'PyPdfVersion': PVersion, 'LxmlVersion':XVersion} + return {'Image Magick': IVersion, 'PyPdf': PVersion, 'lxml':XVersion} diff --git a/cps/converter.py b/cps/converter.py new file mode 100644 index 00000000..ff4f252c --- /dev/null +++ b/cps/converter.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import os +import sys +import subprocess +import ub +import db +import re +import web +from flask_babel import gettext as _ + + +RET_FAIL = 0 +RET_SUCCESS = 1 + + +def versionKindle(): + versions = _(u'not installed') + if os.path.exists(ub.config.config_converterpath): + try: + p = subprocess.Popen(ub.config.config_converterpath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p.wait() + for lines in p.stdout.readlines(): + if isinstance(lines, bytes): + lines = lines.decode('utf-8') + if re.search('Amazon kindlegen\(', lines): + versions = lines + except Exception: + versions = _(u'Excecution permissions missing') + return {'kindlegen' : versions} + + +def versionCalibre(): + versions = _(u'not installed') + if os.path.exists(ub.config.config_converterpath): + try: + p = subprocess.Popen(ub.config.config_converterpath + ' --version', stdout=subprocess.PIPE, stderr=subprocess.PIPE) + p.wait() + for lines in p.stdout.readlines(): + if isinstance(lines, bytes): + lines = lines.decode('utf-8') + if re.search('.*\(calibre', lines): + versions = lines + except Exception: + versions = _(u'Excecution permissions missing') + return {'Calibre converter' : versions} + + +def convert_kindlegen(file_path, book): + error_message = None + # vendorpath = os.path.join(os.path.normpath(os.path.dirname(os.path.realpath(__file__)) + + # os.sep + "../vendor" + os.sep)) + #if sys.platform == "win32": + # kindlegen = (os.path.join(vendorpath, u"kindlegen.exe")).encode(sys.getfilesystemencoding()) + #else: + # kindlegen = (os.path.join(vendorpath, u"kindlegen")).encode(sys.getfilesystemencoding()) + if not os.path.exists(ub.config.config_converterpath): + error_message = _(u"kindlegen binary %(kindlepath)s not found", kindlepath=ub.config.config_converterpath) + web.app.logger.error("convert_kindlegen: " + error_message) + return error_message, RET_FAIL + try: + p = subprocess.Popen((ub.config.config_converterpath + " \"" + file_path + u".epub\"").encode(sys.getfilesystemencoding()), + stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + except Exception as e: + error_message = _(u"kindlegen failed, no execution permissions") + web.app.logger.error("convert_kindlegen: " + error_message) + return error_message, RET_FAIL + # Poll process for new output until finished + while True: + nextline = p.stdout.readline() + if nextline == '' and p.poll() is not None: + break + if nextline != "\r\n": + # Format of error message (kindlegen translates its output texts): + # Error(prcgen):E23006: Language not recognized in metadata.The dc:Language field is mandatory.Aborting. + conv_error = re.search(".*\(.*\):(E\d+):\s(.*)", nextline) + # If error occoures, log in every case + if conv_error: + error_message = _(u"Kindlegen failed with Error %(error)s. Message: %(message)s", + error=conv_error.group(1), message=conv_error.group(2).decode('utf-8')) + web.app.logger.info("convert_kindlegen: " + error_message) + web.app.logger.info(nextline.strip('\r\n')) + else: + web.app.logger.debug(nextline.strip('\r\n')) + + check = p.returncode + if not check or check < 2: + book.data.append(db.Data( + name=book.data[0].name, + book_format="MOBI", + book=book.id, + uncompressed_size=os.path.getsize(file_path + ".mobi") + )) + db.session.commit() + return file_path + ".mobi", RET_SUCCESS + else: + web.app.logger.info("convert_kindlegen: kindlegen failed with error while converting book") + if not error_message: + error_message = 'kindlegen failed, no excecution permissions' + return error_message, RET_FAIL + + +def convert_calibre(file_path, book): + error_message = None + if not os.path.exists(ub.config.config_converterpath): + error_message = _(u"Ebook-convert binary %(converterpath)s not found", converterpath=ub.config.config_converterpath) + web.app.logger.error("convert_calibre: " + error_message) + return error_message, RET_FAIL + try: + command = ("\""+ub.config.config_converterpath + "\" " + ub.config.config_calibre + + " \"" + file_path + u".epub\" \"" + file_path + u".mobi\"").encode(sys.getfilesystemencoding()) + p = subprocess.Popen(command,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + except Exception as e: + error_message = _(u"Ebook-convert failed, no execution permissions") + web.app.logger.error("convert_calibre: " + error_message) + return error_message, RET_FAIL + # Poll process for new output until finished + while True: + nextline = p.stdout.readline() + if nextline == '' and p.poll() is not None: + break + web.app.logger.debug(nextline.strip('\r\n').decode(sys.getfilesystemencoding())) + + check = p.returncode + if check == 0 : + book.data.append(db.Data( + name=book.data[0].name, + book_format="MOBI", + book=book.id, + uncompressed_size=os.path.getsize(file_path + ".mobi") + )) + db.session.commit() + return file_path + ".mobi", RET_SUCCESS + else: + web.app.logger.info("convert_calibre: Ebook-convert failed with error while converting book") + if not error_message: + error_message = 'Ebook-convert failed, no excecution permissions' + return error_message, RET_FAIL + + +def versioncheck(): + if ub.config.config_ebookconverter == 1: + return versionKindle() + elif ub.config.config_ebookconverter == 2: + return versionCalibre() + else: + return {'ebook_converter':''} + + +def convert_mobi(file_path, book): + if ub.config.config_ebookconverter == 2: + return convert_calibre(file_path, book) + else: + return convert_kindlegen(file_path, book) diff --git a/cps/helper.py b/cps/helper.py index 95dca99f..6c5c81c4 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -14,6 +14,7 @@ import traceback import re import unicodedata from io import BytesIO +import converter try: from StringIO import StringIO @@ -57,17 +58,6 @@ RET_FAIL = 0 def make_mobi(book_id, calibrepath): - error_message = None - vendorpath = os.path.join(os.path.normpath(os.path.dirname(os.path.realpath(__file__)) + - os.sep + "../vendor" + os.sep)) - if sys.platform == "win32": - kindlegen = (os.path.join(vendorpath, u"kindlegen.exe")).encode(sys.getfilesystemencoding()) - else: - kindlegen = (os.path.join(vendorpath, u"kindlegen")).encode(sys.getfilesystemencoding()) - if not os.path.exists(kindlegen): - error_message = _(u"kindlegen binary %(kindlepath)s not found", kindlepath=kindlegen) - app.logger.error("make_mobi: " + error_message) - return error_message, RET_FAIL book = db.session.query(db.Books).filter(db.Books.id == book_id).first() data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == 'EPUB').first() if not data: @@ -77,45 +67,7 @@ def make_mobi(book_id, calibrepath): file_path = os.path.join(calibrepath, book.path, data.name) if os.path.exists(file_path + u".epub"): - try: - p = subprocess.Popen((kindlegen + " \"" + file_path + u".epub\"").encode(sys.getfilesystemencoding()), - stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) - except Exception: - error_message = _(u"kindlegen failed, no execution permissions") - app.logger.error("make_mobi: " + error_message) - return error_message, RET_FAIL - # Poll process for new output until finished - while True: - nextline = p.stdout.readline() - if nextline == '' and p.poll() is not None: - break - if nextline != "\r\n": - # Format of error message (kindlegen translates its output texts): - # Error(prcgen):E23006: Language not recognized in metadata.The dc:Language field is mandatory.Aborting. - conv_error = re.search(".*\(.*\):(E\d+):\s(.*)", nextline) - # If error occoures, log in every case - if conv_error: - error_message = _(u"Kindlegen failed with Error %(error)s. Message: %(message)s", - error=conv_error.group(1), message=conv_error.group(2).decode('utf-8')) - app.logger.info("make_mobi: " + error_message) - app.logger.info(nextline.strip('\r\n')) - app.logger.debug(nextline.strip('\r\n')) - - check = p.returncode - if not check or check < 2: - book.data.append(db.Data( - name=book.data[0].name, - book_format="MOBI", - book=book.id, - uncompressed_size=os.path.getsize(file_path + ".mobi") - )) - db.session.commit() - return file_path + ".mobi", RET_SUCCESS - else: - app.logger.info("make_mobi: kindlegen failed with error while converting book") - if not error_message: - error_message = 'kindlegen failed, no excecution permissions' - return error_message, RET_FAIL + return converter.convert_mobi(file_path, book) else: error_message = "make_mobi: epub not found: %s.epub" % file_path return error_message, RET_FAIL diff --git a/cps/server.py b/cps/server.py index 0842c064..fbfffb45 100644 --- a/cps/server.py +++ b/cps/server.py @@ -92,9 +92,9 @@ class server: def getNameVersion(self): if gevent_present: - return {'gevent':geventVersion} + return {'Gevent':'v'+geventVersion} else: - return {'tornado':tornadoVersion} + return {'Tornado':'v'+tornadoVersion} # Start Instance of Server diff --git a/cps/templates/config_edit.html b/cps/templates/config_edit.html index 02238ebc..051531ec 100644 --- a/cps/templates/config_edit.html +++ b/cps/templates/config_edit.html @@ -159,6 +159,38 @@ +