Skip to content

Commit 4e4e437

Browse files
committed
Terminate translation whenever an error occurs in batch mode.
1 parent eabead6 commit 4e4e437

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

advanced.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from types import MethodType
33

44
from calibre.constants import __version__
5-
from calibre.gui2.dialogs.message_box import JobError
65

76
from .lib.utils import uid
87
from .lib.config import get_config

batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,6 @@ def translate_ebooks(self, ebooks):
144144

145145
ebooks = ebooks if isinstance(ebooks, list) else [ebooks]
146146
for ebook in self.ebooks:
147-
self.worker.translate_ebook(ebook)
147+
self.worker.translate_ebook(ebook, is_batch=True)
148148
self.ebooks.clear()
149149
self.done(0)

lib/conversion.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from tempfile import gettempdir
55

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

3737

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

6565
translation = get_translation(
6666
translator, lambda text, error=False: log.info(text))
67+
translation.set_batch(is_batch)
6768
translation.set_callback(cache.update_paragraph)
6869

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

120-
def translate_ebook(self, ebook, cache_only=False):
121+
def translate_ebook(self, ebook, cache_only=False, is_batch=False):
121122
input_path = ebook.get_input_path()
122123
if not self.config.get('to_library'):
123124
output_path = os.path.join(
@@ -134,7 +135,7 @@ def translate_ebook(self, ebook, cache_only=False):
134135
'calibre_plugins.ebook_translator.lib.conversion',
135136
'convert_book',
136137
(ebook.title, input_path, output_path, ebook.source_lang,
137-
ebook.target_lang, cache_only)),
138+
ebook.target_lang, cache_only, is_batch)),
138139
description=(_('[{} > {}] Translating "{}"').format(
139140
ebook.source_lang, ebook.target_lang, ebook.title)))
140141
self.working_jobs[job] = (ebook, output_path)

lib/element.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def add_translation(self, translation, placeholder, position=None,
105105
else:
106106
self.element.addnext(new_element)
107107
if position == 'only':
108-
if get_name(new_element) =='a':
109-
new_element.set('href', self.element.get('href'))
108+
if get_name(new_element) == 'a':
109+
new_element.set('href', self.element.get('href'))
110110
self.delete()
111111
return new_element
112112

lib/translation.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import json
55
from types import GeneratorType
66

7-
from calibre import prepare_string_for_xml as xml_escape
8-
97
from ..engines import builtin_engines
108
from ..engines import GoogleFreeTranslate
119
from ..engines.custom import CustomTranslate
@@ -83,9 +81,9 @@ class Translation:
8381
def __init__(self, translator, glossary):
8482
self.translator = translator
8583
self.glossary = glossary
86-
self.abort_count = 0
8784

8885
self.fresh = False
86+
self.batch = False
8987
self.progress = dummy
9088
self.log = dummy
9189
self.streaming = dummy
@@ -94,10 +92,14 @@ def __init__(self, translator, glossary):
9492

9593
self.total = 0
9694
self.progress_bar = ProgressBar()
95+
self.abort_count = 0
9796

9897
def set_fresh(self, fresh):
9998
self.fresh = fresh
10099

100+
def set_batch(self, batch):
101+
self.batch = batch
102+
101103
def set_progress(self, progress):
102104
self.progress = progress
103105

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

242-
message = _('Translation completed.')
243244
self.log(sep())
245+
if self.batch and self.abort_count > 0:
246+
raise Exception(_('Translation failed.'))
244247
consuming = round((time.time() - start_time) / 60, 2)
245248
self.log('Time consuming: %s minutes' % consuming)
249+
message = _('Translation completed.')
246250
self.log(message)
247251
self.progress(1, message)
248252

0 commit comments

Comments
 (0)