Skip to content

Commit

Permalink
feat: Compatible with lower versions of Calibre and added row number …
Browse files Browse the repository at this point in the history
…to logging.
  • Loading branch information
bookfere committed Apr 11, 2024
1 parent f876702 commit ce522c9
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 29 deletions.
5 changes: 2 additions & 3 deletions advanced.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import time
import traceback
from types import MethodType

from calibre.constants import __version__

from .lib.utils import uid
from .lib.utils import uid, traceback_error
from .lib.config import get_config
from .lib.encodings import encoding_list
from .lib.cache import Paragraph, get_cache
Expand Down Expand Up @@ -107,7 +106,7 @@ def prepare_ebook_data(self):
except Exception:
self.progress_message.emit(
_('Failed to extract ebook content'))
self.progress_detail.emit('\n' + traceback.format_exc())
self.progress_detail.emit('\n' + traceback_error())
self.progress.emit(100)
self.clean_cache(cache)
return
Expand Down
5 changes: 2 additions & 3 deletions components/engine.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import time
import uuid
import traceback
from types import GeneratorType

from ..lib.utils import sorted_mixed_keys
from ..lib.utils import sorted_mixed_keys, traceback_error
from ..lib.config import get_config
from ..engines.custom import (
create_engine_template, load_engine_data, CustomTranslate)
Expand Down Expand Up @@ -79,7 +78,7 @@ def translate_text(self, text):
self.complete.emit()
except Exception:
self.clear.emit()
self.result.emit(traceback.format_exc(chain=False).strip())
self.result.emit(traceback_error())

@pyqtSlot()
def check_usage(self):
Expand Down
5 changes: 3 additions & 2 deletions engines/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import ssl
import os.path
import traceback

from mechanize import Browser, Request, HTTPError
from calibre import get_proxies
from calibre.utils.localization import lang_as_iso639_1

from ..lib.utils import traceback_error


load_translations()

Expand Down Expand Up @@ -201,7 +202,7 @@ def get_result(self, url, data=None, headers={}, method='GET',
except Exception as e:
if silence:
return None
error = [traceback.format_exc(chain=False).strip()]
error = [traceback_error()]
if isinstance(e, HTTPError):
error.append(e.read().decode('utf-8'))
elif result:
Expand Down
5 changes: 3 additions & 2 deletions engines/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import time
import json
import os.path
import traceback
from subprocess import Popen, PIPE

from ..lib.utils import traceback_error

from .base import Base
from .languages import google, gemini

Expand Down Expand Up @@ -88,7 +89,7 @@ def _run_command(self, command, silence=False):
if silence:
return None
raise Exception(
message.format(command, '\n\n%s' % traceback.format_exc()))
message.format(command, '\n\n%s' % traceback_error()))
if process.wait() != 0:
if silence:
return None
Expand Down
5 changes: 3 additions & 2 deletions lib/async_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sys
import asyncio
import traceback
import concurrent.futures

from ..lib.utils import traceback_error

from .exception import TranslationCanceled


Expand Down Expand Up @@ -50,7 +51,7 @@ async def translation_worker(self):
self.done_queue.task_done()
break
except Exception:
paragraph.error = traceback.format_exc(chain=False).strip()
paragraph.error = traceback_error()
self.done_queue.put_nowait(paragraph)
self.queue.task_done()

Expand Down
5 changes: 3 additions & 2 deletions lib/thread_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import time
import traceback
from threading import Thread

from ..lib.utils import traceback_error

from .exception import TranslationCanceled


Expand Down Expand Up @@ -46,7 +47,7 @@ def translation_thread(self):
self.done_queue.task_done()
break
except Exception:
paragraph.error = traceback.format_exc(chain=False).strip()
paragraph.error = traceback_error()
self.done_queue.put(paragraph)
self.queue.task_done()

Expand Down
33 changes: 20 additions & 13 deletions lib/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..engines import GoogleFreeTranslate
from ..engines.custom import CustomTranslate

from .utils import sep, trim, dummy
from .utils import sep, trim, dummy, traceback_error
from .config import get_config
from .exception import (
TranslationFailed, TranslationCanceled, NoAvailableApiKey)
Expand Down Expand Up @@ -121,7 +121,7 @@ def need_stop(self):
return self.translator.max_error_count > 0 and \
self.abort_count >= self.translator.max_error_count

def _translate_text(self, text, retry=0, interval=0):
def _translate_text(self, row, text, retry=0, interval=0):
"""Translation engine service error code documentation:
* https://cloud.google.com/apis/design/errors
* https://www.deepl.com/docs-api/api-access/error-handling/
Expand Down Expand Up @@ -155,11 +155,14 @@ def _translate_text(self, text, retry=0, interval=0):
interval += 5
# Logging any errors that occur during translation.
logged_text = text[:200] + '...' if len(text) > 200 else text
error_message = '{0}\n{2}\n{1}\n{3}\n{1}\n{4}'.format(
sep(), sep('┈'), _('Original: {}').format(logged_text),
error_messages = [
sep(), _('Original: {}').format(logged_text), sep('┈'),
_('Status: Failed {} times / Sleeping for {} seconds')
.format(retry, interval), _('Error: {}').format(str(e)))
self.log(error_message, True)
.format(retry, interval), sep('┈'), _('Error: {}')
.format(traceback_error())]
if row >= 0:
error_messages.insert(1, _('row: {}').format(row))
self.log('\n'.join(error_messages), True)
time.sleep(interval)
return self._translate_text(text, retry, interval)

Expand All @@ -171,7 +174,7 @@ def translate_paragraph(self, paragraph):
return
self.streaming('')
self.streaming(_('Translating...'))
translation = self._translate_text(paragraph.original)
translation = self._translate_text(paragraph.row, paragraph.original)
# Process streaming text
if isinstance(translation, GeneratorType):
if self.total == 1:
Expand Down Expand Up @@ -203,21 +206,25 @@ def process_translation(self, paragraph):
self.streaming(paragraph)
self.callback(paragraph)

row = paragraph.row
original = paragraph.original.strip()
if paragraph.error is None:
self.log(sep())
if row >= 0:
self.log(_('Row: {}').format(row))
self.log(_('Original: {}').format(original))
self.log(sep('┈'))
message = _('Translation: {}')
if paragraph.is_cache:
message = _('Translation (Cached): {}')
self.log(message.format(paragraph.translation.strip()))
else:
self.log(sep(), True)
self.log(_('Original: {}').format(original), True)
self.log(sep('┈'), True)
self.log(_('Error: {}').format(paragraph.error.strip()), True)
# paragraph.error = None
# else:
# self.log(sep(), True)
# self.log(_('Row: {}').format(row))
# self.log(_('Original: {}').format(original), True)
# self.log(sep('┈'), True)
# self.log(_('Error: {}').format(paragraph.error.strip()), True)
# # paragraph.error = None

def handle(self, paragraphs=[]):
start_time = time.time()
Expand Down
10 changes: 8 additions & 2 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import codecs
import socket
import hashlib
import traceback
from subprocess import Popen

from ..lib.cssselect import GenericTranslator, SelectorError

from .encodings import encoding_list


ns = {'x': 'http://www.w3.org/1999/xhtml'}
is_test = 'unittest' in sys.modules
Expand Down Expand Up @@ -129,5 +128,12 @@ def open_file(path, encoding='utf-8'):
return file.read().decode(encoding)


def traceback_error():
try:
return traceback.format_exc(chain=False).strip()
except Exception:
return traceback.format_exc().strip()


def dummy(*args, **kwargs):
pass

0 comments on commit ce522c9

Please sign in to comment.