Skip to content

Commit

Permalink
Terminate translation whenever an error occurs in batch mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
bookfere committed Jul 21, 2023
1 parent eabead6 commit 4e4e437
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
1 change: 0 additions & 1 deletion advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from types import MethodType

from calibre.constants import __version__
from calibre.gui2.dialogs.message_box import JobError

from .lib.utils import uid
from .lib.config import get_config
Expand Down
2 changes: 1 addition & 1 deletion batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ def translate_ebooks(self, ebooks):

ebooks = ebooks if isinstance(ebooks, list) else [ebooks]
for ebook in self.ebooks:
self.worker.translate_ebook(ebook)
self.worker.translate_ebook(ebook, is_batch=True)
self.ebooks.clear()
self.done(0)
11 changes: 6 additions & 5 deletions lib/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from tempfile import gettempdir

from calibre.gui2 import Dispatcher
from calibre.constants import __version__, DEBUG
from calibre.constants import DEBUG, __version__
from calibre.ebooks.conversion.plumber import Plumber
from calibre.ptempfile import PersistentTemporaryFile
from calibre.ebooks.metadata.meta import get_metadata, set_metadata
Expand Down Expand Up @@ -36,8 +36,8 @@ def convert(self, oeb, output_path, input_plugin, opts, log):


def convert_book(ebook_title, input_path, output_path, source_lang,
target_lang, cache_only, notification):
""" The following parameters need attention:
target_lang, cache_only, is_batch, notification):
"""The following parameters need attention:
:cache_only: Only use the translation which exists in the cache.
:notification: It is automatically added by arbitrary_n.
"""
Expand All @@ -64,6 +64,7 @@ def convert_book(ebook_title, input_path, output_path, source_lang,

translation = get_translation(
translator, lambda text, error=False: log.info(text))
translation.set_batch(is_batch)
translation.set_callback(cache.update_paragraph)

info = '{0}\n| Diagnosis Information\n{0}'.format(sep())
Expand Down Expand Up @@ -117,7 +118,7 @@ def __init__(self, gui, icon):
self.api = self.db.new_api
self.working_jobs = self.gui.bookfere_ebook_translator.jobs

def translate_ebook(self, ebook, cache_only=False):
def translate_ebook(self, ebook, cache_only=False, is_batch=False):
input_path = ebook.get_input_path()
if not self.config.get('to_library'):
output_path = os.path.join(
Expand All @@ -134,7 +135,7 @@ def translate_ebook(self, ebook, cache_only=False):
'calibre_plugins.ebook_translator.lib.conversion',
'convert_book',
(ebook.title, input_path, output_path, ebook.source_lang,
ebook.target_lang, cache_only)),
ebook.target_lang, cache_only, is_batch)),
description=(_('[{} > {}] Translating "{}"').format(
ebook.source_lang, ebook.target_lang, ebook.title)))
self.working_jobs[job] = (ebook, output_path)
Expand Down
4 changes: 2 additions & 2 deletions lib/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def add_translation(self, translation, placeholder, position=None,
else:
self.element.addnext(new_element)
if position == 'only':
if get_name(new_element) =='a':
new_element.set('href', self.element.get('href'))
if get_name(new_element) == 'a':
new_element.set('href', self.element.get('href'))
self.delete()
return new_element

Expand Down
14 changes: 9 additions & 5 deletions lib/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import json
from types import GeneratorType

from calibre import prepare_string_for_xml as xml_escape

from ..engines import builtin_engines
from ..engines import GoogleFreeTranslate
from ..engines.custom import CustomTranslate
Expand Down Expand Up @@ -83,9 +81,9 @@ class Translation:
def __init__(self, translator, glossary):
self.translator = translator
self.glossary = glossary
self.abort_count = 0

self.fresh = False
self.batch = False
self.progress = dummy
self.log = dummy
self.streaming = dummy
Expand All @@ -94,10 +92,14 @@ def __init__(self, translator, glossary):

self.total = 0
self.progress_bar = ProgressBar()
self.abort_count = 0

def set_fresh(self, fresh):
self.fresh = fresh

def set_batch(self, batch):
self.batch = batch

def set_progress(self, progress):
self.progress = progress

Expand Down Expand Up @@ -136,7 +138,7 @@ def _translate_text(self, text, retry=0, interval=5):
except Exception as e:
if self.cancel_request() or self.need_stop():
raise TranslationCanceled(_('Translation canceled.'))
# Try to retreive a available API key.
# Try to retrieve a available API key.
if self.translator.need_change_api_key(str(e).lower()):
if not self.translator.change_api_key():
raise NoAvailableApiKey(_('No available API key.'))
Expand Down Expand Up @@ -239,10 +241,12 @@ def handle(self, paragraphs=[]):
self.translator.request_interval)
handler.handle()

message = _('Translation completed.')
self.log(sep())
if self.batch and self.abort_count > 0:
raise Exception(_('Translation failed.'))
consuming = round((time.time() - start_time) / 60, 2)
self.log('Time consuming: %s minutes' % consuming)
message = _('Translation completed.')
self.log(message)
self.progress(1, message)

Expand Down

0 comments on commit 4e4e437

Please sign in to comment.