Skip to content

Commit

Permalink
Make texts in Background thread translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
OzzieIsaacs committed Apr 24, 2022
1 parent 8421a17 commit 1e723df
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 76 deletions.
6 changes: 3 additions & 3 deletions cps/about.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@


def collect_stats():
_VERSIONS['ebook converter'] = _(converter.get_calibre_version())
_VERSIONS['unrar'] = _(converter.get_unrar_version())
_VERSIONS['kepubify'] = _(converter.get_kepubify_version())
_VERSIONS['ebook converter'] = converter.get_calibre_version()
_VERSIONS['unrar'] = converter.get_unrar_version()
_VERSIONS['kepubify'] = converter.get_kepubify_version()
return _VERSIONS


Expand Down
9 changes: 5 additions & 4 deletions cps/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

import os
import re
from flask_babel import gettext as _

from flask_babel import lazy_gettext as N_

from . import config, logger
from .subproc_wrapper import process_wait


log = logger.create()

# _() necessary to make babel aware of string for translation
_NOT_INSTALLED = _('not installed')
_EXECUTION_ERROR = _('Execution permissions missing')
# strings getting translated when used
_NOT_INSTALLED = N_('not installed')
_EXECUTION_ERROR = N_('Execution permissions missing')


def _get_command_version(path, pattern, argument=None):
Expand Down
7 changes: 4 additions & 3 deletions cps/editbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import json
from shutil import copyfile
from uuid import uuid4
from markupsafe import escape
from markupsafe import escape # dependency of flask
from functools import wraps

try:
Expand All @@ -35,6 +35,7 @@

from flask import Blueprint, request, flash, redirect, url_for, abort, Markup, Response
from flask_babel import gettext as _
from flask_babel import lazy_gettext as N_
from flask_login import current_user, login_required
from sqlalchemy.exc import OperationalError, IntegrityError
# from sqlite3 import OperationalError as sqliteOperationalError
Expand Down Expand Up @@ -681,7 +682,7 @@ def upload_single_file(file_request, book, book_id):

# Queue uploader info
link = '<a href="{}">{}</a>'.format(url_for('web.show_book', book_id=book.id), escape(book.title))
upload_text = _(u"File format %(ext)s added to %(book)s", ext=file_ext.upper(), book=link)
upload_text = N_(u"File format %(ext)s added to %(book)s", ext=file_ext.upper(), book=link)
WorkerThread.add(current_user.name, TaskUpload(upload_text, escape(book.title)))

return uploader.process(
Expand Down Expand Up @@ -1134,7 +1135,7 @@ def upload():
if error:
flash(error, category="error")
link = '<a href="{}">{}</a>'.format(url_for('web.show_book', book_id=book_id), escape(title))
upload_text = _(u"File %(file)s uploaded", file=link)
upload_text = N_(u"File %(file)s uploaded", file=link)
WorkerThread.add(current_user.name, TaskUpload(upload_text, escape(title)))
helper.add_book_to_thumbnail_cache(book_id)

Expand Down
12 changes: 7 additions & 5 deletions cps/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from babel.units import format_unit
from flask import send_from_directory, make_response, redirect, abort, url_for
from flask_babel import gettext as _
from flask_babel import lazy_gettext as N_
from flask_login import current_user
from sqlalchemy.sql.expression import true, false, and_, or_, text, func
from sqlalchemy.exc import InvalidRequestError, OperationalError
Expand All @@ -53,7 +54,7 @@

from . import calibre_db, cli
from .tasks.convert import TaskConvert
from . import logger, config, get_locale, db, ub, kobo_sync_status, fs
from . import logger, config, get_locale, db, ub, fs
from . import gdriveutils as gd
from .constants import STATIC_DIR as _STATIC_DIR, CACHE_TYPE_THUMBNAILS, THUMBNAIL_TYPE_COVER, THUMBNAIL_TYPE_SERIES
from .subproc_wrapper import process_wait
Expand Down Expand Up @@ -111,9 +112,10 @@ def convert_book_format(book_id, calibrepath, old_book_format, new_book_format,
return None


# Texts are not lazy translated as they are supposed to get send out as is
def send_test_mail(kindle_mail, user_name):
WorkerThread.add(user_name, TaskEmail(_(u'Calibre-Web test e-mail'), None, None,
config.get_mail_settings(), kindle_mail, _(u"Test e-mail"),
config.get_mail_settings(), kindle_mail, N_(u"Test e-mail"),
_(u'This e-mail has been sent via Calibre-Web.')))
return

Expand All @@ -135,7 +137,7 @@ def send_registration_mail(e_mail, user_name, default_password, resend=False):
attachment=None,
settings=config.get_mail_settings(),
recipient=e_mail,
taskMessage=_(u"Registration e-mail for user: %(name)s", name=user_name),
task_message=N_(u"Registration e-mail for user: %(name)s", name=user_name),
text=txt
))
return
Expand Down Expand Up @@ -219,7 +221,7 @@ def send_mail(book_id, book_format, convert, kindle_mail, calibrepath, user_id):
if entry.format.upper() == book_format.upper():
converted_file_name = entry.name + '.' + book_format.lower()
link = '<a href="{}">{}</a>'.format(url_for('web.show_book', book_id=book_id), escape(book.title))
email_text = _(u"%(book)s send to Kindle", book=link)
email_text = N_(u"%(book)s send to Kindle", book=link)
WorkerThread.add(user_id, TaskEmail(_(u"Send to Kindle"), book.path, converted_file_name,
config.get_mail_settings(), kindle_mail,
email_text, _(u'This e-mail has been sent via Calibre-Web.')))
Expand Down Expand Up @@ -1012,7 +1014,7 @@ def render_task_status(tasklist):
else:
ret['status'] = _(u'Unknown Status')

ret['taskMessage'] = "{}: {}".format(_(task.name), task.message) if task.message else _(task.name)
ret['taskMessage'] = "{}: {}".format(task.name, task.message) if task.message else task.name
ret['progress'] = "{} %".format(int(task.progress * 100))
ret['user'] = escape(user) # prevent xss

Expand Down
39 changes: 19 additions & 20 deletions cps/tasks/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

import os
import re

from glob import glob
from shutil import copyfile
from markupsafe import escape

from sqlalchemy.exc import SQLAlchemyError
from flask_babel import lazy_gettext as N_

from cps.services.worker import CalibreTask
from cps import db
Expand All @@ -41,10 +41,10 @@


class TaskConvert(CalibreTask):
def __init__(self, file_path, bookid, taskMessage, settings, kindle_mail, user=None):
super(TaskConvert, self).__init__(taskMessage)
def __init__(self, file_path, book_id, task_message, settings, kindle_mail, user=None):
super(TaskConvert, self).__init__(task_message)
self.file_path = file_path
self.bookid = bookid
self.book_id = book_id
self.title = ""
self.settings = settings
self.kindle_mail = kindle_mail
Expand All @@ -56,9 +56,9 @@ def run(self, worker_thread):
self.worker_thread = worker_thread
if config.config_use_google_drive:
worker_db = db.CalibreDB(expire_on_commit=False)
cur_book = worker_db.get_book(self.bookid)
cur_book = worker_db.get_book(self.book_id)
self.title = cur_book.title
data = worker_db.get_book_format(self.bookid, self.settings['old_book_format'])
data = worker_db.get_book_format(self.book_id, self.settings['old_book_format'])
df = gdriveutils.getFileFromEbooksFolder(cur_book.path,
data.name + "." + self.settings['old_book_format'].lower())
if df:
Expand Down Expand Up @@ -89,7 +89,7 @@ def run(self, worker_thread):
# if we're sending to kindle after converting, create a one-off task and run it immediately
# todo: figure out how to incorporate this into the progress
try:
EmailText = _(u"%(book)s send to Kindle", book=escape(self.title))
EmailText = N_(u"%(book)s send to Kindle", book=escape(self.title))
worker_thread.add(self.user, TaskEmail(self.settings['subject'],
self.results["path"],
filename,
Expand All @@ -106,15 +106,15 @@ def _convert_ebook_format(self):
error_message = None
local_db = db.CalibreDB(expire_on_commit=False)
file_path = self.file_path
book_id = self.bookid
book_id = self.book_id
format_old_ext = u'.' + self.settings['old_book_format'].lower()
format_new_ext = u'.' + self.settings['new_book_format'].lower()

# check to see if destination format already exists - or if book is in database
# if it does - mark the conversion task as complete and return a success
# this will allow send to kindle workflow to continue to work
if os.path.isfile(file_path + format_new_ext) or\
local_db.get_book_format(self.bookid, self.settings['new_book_format']):
local_db.get_book_format(self.book_id, self.settings['new_book_format']):
log.info("Book id %d already converted to %s", book_id, format_new_ext)
cur_book = local_db.get_book(book_id)
self.title = cur_book.title
Expand All @@ -133,7 +133,7 @@ def _convert_ebook_format(self):
local_db.session.rollback()
log.error("Database error: %s", e)
local_db.session.close()
self._handleError(error_message)
self._handleError(N_("Database error: %(error)s.", error=e))
return
self._handleSuccess()
local_db.session.close()
Expand All @@ -150,8 +150,7 @@ def _convert_ebook_format(self):
else:
# check if calibre converter-executable is existing
if not os.path.exists(config.config_converterpath):
# ToDo Text is not translated
self._handleError(_(u"Calibre ebook-convert %(tool)s not found", tool=config.config_converterpath))
self._handleError(N_(u"Calibre ebook-convert %(tool)s not found", tool=config.config_converterpath))
return
check, error_message = self._convert_calibre(file_path, format_old_ext, format_new_ext)

Expand Down Expand Up @@ -184,11 +183,11 @@ def _convert_ebook_format(self):
self._handleSuccess()
return os.path.basename(file_path + format_new_ext)
else:
error_message = _('%(format)s format not found on disk', format=format_new_ext.upper())
error_message = N_('%(format)s format not found on disk', format=format_new_ext.upper())
local_db.session.close()
log.info("ebook converter failed with error while converting book")
if not error_message:
error_message = _('Ebook converter failed with unknown error')
error_message = N_('Ebook converter failed with unknown error')
self._handleError(error_message)
return

Expand All @@ -198,7 +197,7 @@ def _convert_kepubify(self, file_path, format_old_ext, format_new_ext):
try:
p = process_open(command, quotes)
except OSError as e:
return 1, _(u"Kepubify-converter failed: %(error)s", error=e)
return 1, N_(u"Kepubify-converter failed: %(error)s", error=e)
self.progress = 0.01
while True:
nextline = p.stdout.readlines()
Expand All @@ -219,7 +218,7 @@ def _convert_kepubify(self, file_path, format_old_ext, format_new_ext):
copyfile(converted_file[0], (file_path + format_new_ext))
os.unlink(converted_file[0])
else:
return 1, _(u"Converted file not found or more than one file in folder %(folder)s",
return 1, N_(u"Converted file not found or more than one file in folder %(folder)s",
folder=os.path.dirname(file_path))
return check, None

Expand All @@ -243,7 +242,7 @@ def _convert_calibre(self, file_path, format_old_ext, format_new_ext):

p = process_open(command, quotes, newlines=False)
except OSError as e:
return 1, _(u"Ebook-converter failed: %(error)s", error=e)
return 1, N_(u"Ebook-converter failed: %(error)s", error=e)

while p.poll() is None:
nextline = p.stdout.readline()
Expand All @@ -266,15 +265,15 @@ def _convert_calibre(self, file_path, format_old_ext, format_new_ext):
ele = ele.decode('utf-8', errors="ignore").strip('\n')
log.debug(ele)
if not ele.startswith('Traceback') and not ele.startswith(' File'):
error_message = _("Calibre failed with error: %(error)s", error=ele)
error_message = N_("Calibre failed with error: %(error)s", error=ele)
return check, error_message

@property
def name(self):
return "Convert"
return N_("Convert")

def __str__(self):
return "Convert {} {}".format(self.bookid, self.kindle_mail)
return "Convert {} {}".format(self.book_id, self.kindle_mail)

@property
def is_cancellable(self):
Expand Down
11 changes: 4 additions & 7 deletions cps/tasks/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from __future__ import division, print_function, unicode_literals
from urllib.request import urlopen

from flask_babel import lazy_gettext as N_

from cps import config, logger
from cps.services.worker import CalibreTask

try:
from urllib.request import urlopen
except ImportError as e:
from urllib2 import urlopen


class TaskReconnectDatabase(CalibreTask):
def __init__(self, task_message=u'Reconnecting Calibre database'):
def __init__(self, task_message=N_('Reconnecting Calibre database')):
super(TaskReconnectDatabase, self).__init__(task_message)
self.log = logger.create()
self.listen_address = config.get_config_ipaddress()
Expand Down
17 changes: 8 additions & 9 deletions cps/tasks/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
from email.message import EmailMessage
from email.utils import parseaddr


from email import encoders
from email.utils import formatdate, make_msgid
from flask_babel import lazy_gettext as N_
from email.utils import formatdate
from email.generator import Generator

from cps.services.worker import CalibreTask
Expand Down Expand Up @@ -111,13 +110,13 @@ def __init__(self, *args, **kwargs):


class TaskEmail(CalibreTask):
def __init__(self, subject, filepath, attachment, settings, recipient, taskMessage, text, internal=False):
super(TaskEmail, self).__init__(taskMessage)
def __init__(self, subject, filepath, attachment, settings, recipient, task_message, text, internal=False):
super(TaskEmail, self).__init__(task_message)
self.subject = subject
self.attachment = attachment
self.settings = settings
self.filepath = filepath
self.recipent = recipient
self.recipient = recipient
self.text = text
self.asyncSMTP = None
self.results = dict()
Expand All @@ -139,7 +138,7 @@ def prepare_message(self):
message = EmailMessage()
# message = MIMEMultipart()
message['From'] = self.settings["mail_from"]
message['To'] = self.recipent
message['To'] = self.recipient
message['Subject'] = self.subject
message['Date'] = formatdate(localtime=True)
message['Message-Id'] = "{}@{}".format(uuid.uuid4(), self.get_msgid_domain()) # f"<{uuid.uuid4()}@{get_msgid_domain(from_)}>" # make_msgid('calibre-web')
Expand Down Expand Up @@ -212,7 +211,7 @@ def send_standard_email(self, msg):
gen = Generator(fp, mangle_from_=False)
gen.flatten(msg)

self.asyncSMTP.sendmail(self.settings["mail_from"], self.recipent, fp.getvalue())
self.asyncSMTP.sendmail(self.settings["mail_from"], self.recipient, fp.getvalue())
self.asyncSMTP.quit()
self._handleSuccess()
log.debug("E-mail send successfully")
Expand Down Expand Up @@ -264,7 +263,7 @@ def _get_attachment(cls, book_path, filename):

@property
def name(self):
return "E-mail"
return N_("E-mail")

@property
def is_cancellable(self):
Expand Down
Loading

0 comments on commit 1e723df

Please sign in to comment.