Skip to content

Commit

Permalink
Added a feature and fixed bugs as follows:
Browse files Browse the repository at this point in the history
1. Added the feature to choose encoding for certain plain text files. #245
2. Attempted to fix the bug causing output errors on Windows. #242, #248
3. Attempted to fix the bug preventing cache deletion on Windows. #246
  • Loading branch information
bookfere committed Mar 21, 2024
1 parent 3c98d88 commit e68dd99
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 227 deletions.
46 changes: 37 additions & 9 deletions advanced.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import time
import traceback
from types import MethodType

from calibre.constants import __version__

from .lib.utils import uid
from .lib.config import get_config
from .lib.encodings import encoding_list
from .lib.cache import Paragraph, TranslationCache, get_cache
from .lib.translation import get_engine_class, get_translator, get_translation
from .lib.element import get_element_handler, Extraction
from .lib.conversion import extract_item
from .lib.conversion import extract_item, extra_formats
from .engines.custom import CustomTranslate

from . import EbookTranslator
Expand All @@ -22,13 +24,15 @@
Qt, QObject, QDialog, QGroupBox, QWidget, QVBoxLayout, QHBoxLayout,
QPlainTextEdit, QPushButton, QSplitter, QLabel, QThread, QLineEdit,
QGridLayout, QProgressBar, pyqtSignal, pyqtSlot, QPixmap, QEvent,
QStackedWidget, QSpacerItem, QTextCursor, QTabWidget, QCheckBox)
QStackedWidget, QSpacerItem, QTextCursor, QTabWidget, QCheckBox,
QComboBox)
except ImportError:
from PyQt5.Qt import (
Qt, QObject, QDialog, QGroupBox, QWidget, QVBoxLayout, QHBoxLayout,
QPlainTextEdit, QPushButton, QSplitter, QLabel, QThread, QLineEdit,
QGridLayout, QProgressBar, pyqtSignal, pyqtSlot, QPixmap, QEvent,
QStackedWidget, QSpacerItem, QTextCursor, QTabWidget, QCheckBox)
QStackedWidget, QSpacerItem, QTextCursor, QTabWidget, QCheckBox,
QComboBox)

load_translations()

Expand Down Expand Up @@ -69,19 +73,21 @@ def clean_cache(self, cache):

@pyqtSlot()
def prepare_ebook_data(self):
self.progress_detail.emit(
'Start processing the ebook: %s' % self.ebook.title)
input_path = self.ebook.get_input_path()
element_handler = get_element_handler(
self.engine_class.placeholder, self.engine_class.separator)
merge_length = str(element_handler.get_merge_length())
encoding = ''
if self.ebook.encoding.lower() != 'utf-8':
encoding = self.ebook.encoding.lower()
cache_id = uid(
input_path + self.engine_class.name + self.ebook.target_lang
+ merge_length + TranslationCache.__version__
+ merge_length + encoding + TranslationCache.__version__
+ Extraction.__version__)
cache = get_cache(cache_id)

self.progress_detail.emit(
'Start processing the ebook: %s' % self.ebook.title)

if cache.is_fresh() or not cache.is_persistence():
cache.set_info('title', self.ebook.title)
cache.set_info('engine_name', self.engine_class.name)
Expand All @@ -93,8 +99,17 @@ def prepare_ebook_data(self):
a = time.time()
# --------------------------
self.progress_message.emit(_('Extracting ebook content...'))
elements = extract_item(
input_path, self.ebook.input_format, self.progress_detail.emit)
try:
elements = extract_item(
input_path, self.ebook.input_format, self.ebook.encoding,
self.progress_detail.emit)
except Exception:
self.progress_message.emit(
_('Failed to extract ebook content'))
self.progress_detail.emit('\n' + traceback.format_exc())
self.progress.emit(100)
self.clean_cache(cache)
return
self.progress.emit(30)
b = time.time()
self.progress_detail.emit('extracting timing: %s' % (b - a))
Expand Down Expand Up @@ -232,8 +247,21 @@ def change_target_format(format):
change_target_format(target_lang.currentText())
target_lang.currentTextChanged.connect(change_target_format)

encoding_group = QGroupBox(_('Encoding'))
encoding_layout = QVBoxLayout(encoding_group)
encoding_select = QComboBox()
encoding_select.addItems(encoding_list)
encoding_layout.addWidget(encoding_select)
encoding_group.setVisible(
self.ebook.input_format in extra_formats.keys())

def change_encoding(encoding):
self.ebook.set_encoding(encoding)
encoding_select.currentTextChanged.connect(change_encoding)

layout.addWidget(input_group)
layout.addWidget(target_group)
layout.addWidget(encoding_group)

return widget

Expand Down
2 changes: 1 addition & 1 deletion engines/microsoft.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class MicrosoftEdgeTranslate(Base):
name = 'MicrosoftEdge(Free))'
name = 'MicrosoftEdge(Free)'
alias = 'Microsoft Edge (Free)'
lang_codes = Base.load_lang_codes(microsoft)
endpoint = 'https://api-edge.cognitive.microsofttranslator.com/translate'
Expand Down
1 change: 1 addition & 0 deletions lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def get_list(cls):
merge = int(cache.get_info('merge_length') or 0) or 'N/A'
size = size_by_unit(os.path.getsize(file_path), 'MB')
names.append((title, engine, lang, merge, size, name))
cache.close()
return names

def _path(self, name):
Expand Down
Loading

0 comments on commit e68dd99

Please sign in to comment.