Skip to content

Commit

Permalink
automatically set directionality if pre-defined in
Browse files Browse the repository at this point in the history
languages.py - now for batch mode
extracted Ebook class from within Ebooks class
Added typing hints on Ebooks class
  • Loading branch information
seidnerj committed Dec 7, 2024
1 parent 07accfd commit 1ca5f50
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 73 deletions.
74 changes: 42 additions & 32 deletions batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .lib.translation import get_engine_class
from .lib.conversion import extra_formats
from .lib.encodings import encoding_list
from .lib.ebook import Ebooks, Ebook
from .engines.custom import CustomTranslate
from .components import (
Footer, AlertMessage, SourceLang, TargetLang, InputFormat,
Expand All @@ -24,7 +25,7 @@


class BatchTranslation(QDialog):
def __init__(self, parent, worker, ebooks):
def __init__(self, parent, worker, ebooks: Ebooks):
QDialog.__init__(self, parent)

self.gui = parent
Expand Down Expand Up @@ -82,40 +83,10 @@ def layout_translate(self):

translation_engine = get_engine_class()
for row, ebook in enumerate(self.ebooks):
ebook: Ebook
ebook_title = QTableWidgetItem(ebook.title)
table.setItem(row, 0, ebook_title)

if ebook.input_format in extra_formats.keys():
input_encoding = QComboBox()
input_encoding.wheelEvent = lambda event: None
table.setCellWidget(row, 1, self._cell_widget(input_encoding))
input_encoding.addItems(encoding_list)
input_encoding.currentTextChanged.connect(
lambda encoding, row=row: self.ebooks[row]
.set_encoding(encoding))
input_encoding.currentTextChanged.connect(
lambda encoding: input_encoding.setToolTip(encoding))
# Target directionality
target_direction = QTableWidgetItem(_('Default'))
target_direction.setTextAlignment(Qt.AlignCenter)
table.setItem(row, 6, target_direction)
else:
input_encoding = QTableWidgetItem(_('Default'))
input_encoding.setTextAlignment(Qt.AlignCenter)
table.setItem(row, 1, input_encoding)
# Target directionality
direction_list = QComboBox()
direction_list.wheelEvent = lambda event: None
direction_list.addItem(_('Auto'), 'auto')
direction_list.addItem(_('Left to Right'), 'ltr')
direction_list.addItem(_('Right to Left'), 'rtl')
direction_list.currentIndexChanged.connect(
lambda index, row=row: self.ebooks[row]
.set_target_direction(direction_list.itemData(index)))
direction_list.currentTextChanged.connect(
lambda direction: direction_list.setToolTip(direction))
table.setCellWidget(row, 6, self._cell_widget(direction_list))

input_fmt = InputFormat(ebook.files.keys())
table.setCellWidget(row, 2, self._cell_widget(input_fmt))

Expand Down Expand Up @@ -175,6 +146,45 @@ def change_target_lang(lang, row=row):
translation_engine.lang_codes.get('target'),
translation_engine.config.get('target_lang'))

if ebook.input_format in extra_formats.keys():
input_encoding = QComboBox()
input_encoding.wheelEvent = lambda event: None
table.setCellWidget(row, 1, self._cell_widget(input_encoding))
input_encoding.addItems(encoding_list)
input_encoding.currentTextChanged.connect(
lambda encoding, row=row: self.ebooks[row]
.set_encoding(encoding))
input_encoding.currentTextChanged.connect(
lambda encoding: input_encoding.setToolTip(encoding))
# Target directionality
target_direction = QTableWidgetItem(_('Default'))
target_direction.setTextAlignment(Qt.AlignCenter)
table.setItem(row, 6, target_direction)
else:
input_encoding = QTableWidgetItem(_('Default'))
input_encoding.setTextAlignment(Qt.AlignCenter)
table.setItem(row, 1, input_encoding)
# Target directionality
direction_list = QComboBox()
direction_list.wheelEvent = lambda event: None
direction_list.addItem(_('Auto'), 'auto')
direction_list.addItem(_('Left to Right'), 'ltr')
direction_list.addItem(_('Right to Left'), 'rtl')
direction_list.currentIndexChanged.connect(
lambda index, row=row: self.ebooks[row]
.set_target_direction(direction_list.itemData(index)))
direction_list.currentTextChanged.connect(
lambda direction: direction_list.setToolTip(direction))

engine_target_lange_codes = translation_engine.lang_codes.get('target')
if engine_target_lange_codes is not None and ebook.target_lang in engine_target_lange_codes:
target_lang_code = engine_target_lange_codes[ebook.target_lang]
direction = translation_engine.lang_codes_directionality.get(target_lang_code, 'auto')
index = direction_list.findData(direction)
direction_list.setCurrentIndex(index)

table.setCellWidget(row, 6, self._cell_widget(direction_list))

table.resizeRowsToContents()
table.resizeColumnsToContents()

Expand Down
87 changes: 46 additions & 41 deletions lib/ebook.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
class Ebooks:
class Ebook:
def __init__(self, id, title, files, input_format, source_lang,
extra_formats=[]):
self.id = id
self.files = files
self.input_format = input_format
self.source_lang = source_lang
self.extra_formats = extra_formats
from typing import Iterator, Iterable

self.output_format = None
self.target_lang = None
self.lang_code = None

self.title = title
self.custom_title = None
self.encoding = 'utf-8'
self.target_direction = 'auto'
class Ebook:
def __init__(self, id, title, files, input_format, source_lang,
extra_formats=[]):
self.id = id
self.files = files
self.input_format = input_format
self.source_lang = source_lang
self.extra_formats = extra_formats

def set_input_format(self, format):
self.input_format = format
self.output_format = None
self.target_lang = None
self.lang_code = None

def set_output_format(self, format):
self.output_format = format
self.title = title
self.custom_title = None
self.encoding = 'utf-8'
self.target_direction = 'auto'

def set_source_lang(self, lang):
self.source_lang = lang
def set_input_format(self, format):
self.input_format = format

def set_target_lang(self, lang):
self.target_lang = lang
def set_output_format(self, format):
self.output_format = format

def set_lang_code(self, code):
self.lang_code = code
def set_source_lang(self, lang):
self.source_lang = lang

def set_custom_title(self, title):
self.custom_title = title
def set_target_lang(self, lang):
self.target_lang = lang

def set_encoding(self, encoding):
self.encoding = encoding
def set_lang_code(self, code):
self.lang_code = code

def set_target_direction(self, direction):
self.target_direction = direction
def set_custom_title(self, title):
self.custom_title = title

def get_input_path(self):
return self.files.get(self.input_format)
def set_encoding(self, encoding):
self.encoding = encoding

def is_extra_format(self):
return self.input_format in self.extra_formats
def set_target_direction(self, direction):
self.target_direction = direction

def get_input_path(self):
return self.files.get(self.input_format)

def is_extra_format(self):
return self.input_format in self.extra_formats


class Ebooks(Iterable):

def __init__(self):
self.ebooks = []
self.ebooks: list[Ebook] = []

def add(self, *args):
self.ebooks.append(self.Ebook(*args))
self.ebooks.append(Ebook(*args))

def first(self):
def first(self) -> Ebook:
return self.ebooks.pop(0)

def clear(self):
Expand All @@ -62,9 +67,9 @@ def clear(self):
def __len__(self):
return len(self.ebooks)

def __iter__(self):
def __iter__(self) -> Iterator[Ebook]:
for ebook in self.ebooks:
yield ebook

def __getitem__(self, index):
def __getitem__(self, index) -> Ebook:
return self.ebooks[index]

0 comments on commit 1ca5f50

Please sign in to comment.