Skip to content

Commit

Permalink
More code quality cleanups, don't need pygettext
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoguen committed Mar 10, 2024
1 parent 0c64e5b commit 9074fa3
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 851 deletions.
17 changes: 8 additions & 9 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ def modify_epub(
log.debug("Found meta node with name=cover")

if cover_id:
log.info("Found cover image ID '{0}'".format(cover_id))
log.info(f"Found cover image ID '{cover_id}'")

cover_node_list: ElementBase = opf.xpath(
'./opf:manifest/opf:item[@id="{0}"]'.format(cover_id),
f'./opf:manifest/opf:item[@id="{v}"]',
namespaces=OPF_NAMESPACES,
)
if len(cover_node_list) > 0:
Expand Down Expand Up @@ -192,7 +192,7 @@ def modify_epub(
nohyphen_css.name, name="kte-css/no-hyphens.css"
)
)
container.add_content_file_reference("kte-css/{0}".format(css_path))
container.add_content_file_reference(f"kte-css/{css_path}")
os.unlink(nohyphen_css.name)
elif opts.get("hyphenate", False) and int(opts.get("hyphen_min_chars", 6)) > 0:
if metadata and metadata.language == NULL_VALUES["language"]:
Expand Down Expand Up @@ -220,7 +220,7 @@ def modify_epub(
hyphen_css.name, name="kte-css/hyphenation.css"
)
)
container.add_content_file_reference("kte-css/{0}".format(css_path))
container.add_content_file_reference(f"kte-css/{css_path}")
os.unlink(hyphen_css.name)

# Now smarten punctuation
Expand All @@ -230,9 +230,8 @@ def modify_epub(
if opts.get("extended_kepub_features", True):
if metadata is not None:
log.info(
"Adding extended Kobo features to {0} by {1}".format(
metadata.title, " and ".join(metadata.authors)
)
"Adding extended Kobo features to {metadata.title} by "
+ " and ".join(metadata.authors)
)

# Add the Kobo span and div tags
Expand Down Expand Up @@ -265,12 +264,12 @@ def modify_epub(
stylehacks_css.name, name="kte-css/stylehacks.css"
)
)
container.add_content_file_reference("kte-css/{0}".format(css_path))
container.add_content_file_reference(f"kte-css/{css_path}")
os.unlink(filename)
container.commit(filename)

_modify_time = time.time() - _modify_start
log.info("modify_epub took {0:f} seconds".format(_modify_time))
log.info(f"modify_epub took {_modify_time:0.2f} seconds")


def intValueChanged(widget, singular, plural, *args, **kwargs):
Expand Down
2 changes: 0 additions & 2 deletions container.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@
class InvalidEpub(ValueError):
"""Designates an invalid ePub file."""

pass


class ParseError(ValueError):
"""Designates an error parsing an ePub inner file."""
Expand Down
4 changes: 2 additions & 2 deletions conversion/input_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ def load_conversion_widgets(self):
"""Add our configuration to the input processing."""
super(OutputOptions, self).load_conversion_widgets()
self.conversion_widgets.append(PluginWidget)
self.conversion_widgets = sorted(
self.conversion_widgets = sorted( # skipcq: PYL-W0201
self.conversion_widgets, key=lambda x: x.TITLE
) # skipcq: PYL-W0201
)
44 changes: 23 additions & 21 deletions conversion/output_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def setupUi(self, Form): # noqa: N802, N803
self.opt_kepub_hyphenate.setText(_("Hyphenate Files")) # noqa: F821
self.gridLayout.addWidget(self.opt_kepub_hyphenate, rows, 0, 1, 1)

self.opt_kepub_disable_hyphenation = QtWidgets.QCheckBox(
self.opt_kepub_disable_hyphenation = QtWidgets.QCheckBox( # skipcq: PYL-W0201
Form
) # skipcq: PYL-W0201
)
self.opt_kepub_disable_hyphenation.setObjectName(
"opt_kepub_disable_hyphenation" # noqa: F821
)
Expand All @@ -88,9 +88,9 @@ def setupUi(self, Form): # noqa: N802, N803

rows += 1

self.opt_kepub_hyphenate_chars_label = QtWidgets.QLabel(
self.opt_kepub_hyphenate_chars_label = QtWidgets.QLabel( # skipcq: PYL-W0201
_("Minimum word length to hyphenate") + ":" # noqa: F821
) # skipcq: PYL-W0201
)
self.gridLayout.addWidget(self.opt_kepub_hyphenate_chars_label, rows, 0, 1, 1)

self.opt_kepub_hyphenate_chars = QtWidgets.QSpinBox(Form) # skipcq: PYL-W0201
Expand All @@ -109,16 +109,16 @@ def setupUi(self, Form): # noqa: N802, N803

rows += 1

self.opt_kepub_hyphenate_chars_before_label = QtWidgets.QLabel(
_("Minimum characters before hyphens") + ":" # noqa: F821
) # skipcq: PYL-W0201
self.opt_kepub_hyphenate_chars_before_label = ( # skipcq: PYL-W0201
QtWidgets.QLabel(_("Minimum characters before hyphens") + ":") # noqa: F821
)
self.gridLayout.addWidget(
self.opt_kepub_hyphenate_chars_before_label, rows, 0, 1, 1
)

self.opt_kepub_hyphenate_chars_before = QtWidgets.QSpinBox(
self.opt_kepub_hyphenate_chars_before = QtWidgets.QSpinBox( # skipcq: PYL-W0201
Form
) # skipcq: PYL-W0201
)
self.opt_kepub_hyphenate_chars_before_label.setBuddy(
self.opt_kepub_hyphenate_chars_before
)
Expand All @@ -138,16 +138,16 @@ def setupUi(self, Form): # noqa: N802, N803

rows += 1

self.opt_kepub_hyphenate_chars_after_label = QtWidgets.QLabel(
_("Minimum characters after hyphens") + ":" # noqa: F821
) # skipcq: PYL-W0201
self.opt_kepub_hyphenate_chars_after_label = ( # skipcq: PYL-W0201
QtWidgets.QLabel(_("Minimum characters after hyphens") + ":") # noqa: F821
)
self.gridLayout.addWidget(
self.opt_kepub_hyphenate_chars_after_label, rows, 0, 1, 1
)

self.opt_kepub_hyphenate_chars_after = QtWidgets.QSpinBox(
self.opt_kepub_hyphenate_chars_after = QtWidgets.QSpinBox( # skipcq: PYL-W0201
Form
) # skipcq: PYL-W0201
)
self.opt_kepub_hyphenate_chars_after_label.setBuddy(
self.opt_kepub_hyphenate_chars_after
)
Expand All @@ -167,16 +167,18 @@ def setupUi(self, Form): # noqa: N802, N803

rows += 1

self.opt_kepub_hyphenate_limit_lines_label = QtWidgets.QLabel(
_("Maximum consecutive hyphenated lines") + ":" # noqa: F821
) # skipcq: PYL-W0201
self.opt_kepub_hyphenate_limit_lines_label = ( # skipcq: PYL-W0201
QtWidgets.QLabel(
_("Maximum consecutive hyphenated lines") + ":" # noqa: F821
)
)
self.gridLayout.addWidget(
self.opt_kepub_hyphenate_limit_lines_label, rows, 0, 1, 1
)

self.opt_kepub_hyphenate_limit_lines = QtWidgets.QSpinBox(
self.opt_kepub_hyphenate_limit_lines = QtWidgets.QSpinBox( # skipcq: PYL-W0201
Form
) # skipcq: PYL-W0201
)
self.opt_kepub_hyphenate_limit_lines_label.setBuddy(
self.opt_kepub_hyphenate_limit_lines
)
Expand Down Expand Up @@ -223,6 +225,6 @@ def load_conversion_widgets(self):
"""Add our configuration to the output process."""
super(OutputOptions, self).load_conversion_widgets()
self.conversion_widgets.append(PluginWidget)
self.conversion_widgets = sorted(
self.conversion_widgets = sorted( # skipcq: PYL-W0201
self.conversion_widgets, key=lambda x: x.TITLE
) # skipcq: PYL-W0201
)
45 changes: 21 additions & 24 deletions device/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ def __init__(self, name, author, message, fname=None, lineno=None):
ValueError.__init__(
self,
_( # noqa: F821
"Failed to parse '{book}' by '{author}' with error: '{error}' "
+ "(file: {filename}, line: {lineno})"
).format(
book=name, author=author, error=message, filename=fname, lineno=lineno
f"Failed to parse '{name}' by '{author}' with error: '{message}' "
+ f"(file: {fname}, line: {lineno})"
),
)

Expand Down Expand Up @@ -250,9 +248,8 @@ def _modify_epub(self, infile, metadata, container=None):
o["kobotouchextended_currenttime"] = datetime.utcnow().ctime()
kte_data_file = self.temporary_file("_KoboTouchExtendedDriverInfo")
common.log.debug(
"KoboTouchExtended:_modify_epub:Driver data file :: {0}".format(
kte_data_file.name
)
f"KoboTouchExtended:_modify_epub:Driver data file :: "
+ kte_data_file.names
)
kte_data_file.write(json.dumps(o).encode("UTF-8"))
kte_data_file.close()
Expand All @@ -276,10 +273,9 @@ def _modify_epub(self, infile, metadata, container=None):
},
)
except Exception as e:
msg = "Failed to process {title} by {authors}: {msg}".format(
title=metadata.title,
authors=" and ".join(metadata.authors),
msg=str(e),
msg = (
f"Failed to process {metadata.title} by "
+ f"{' and '.join(metadata.authors)}: {e}"
)
common.log.exception(msg)

Expand All @@ -298,7 +294,7 @@ def _modify_epub(self, infile, metadata, container=None):
dpath = self.create_upload_path(dpath, metadata, metadata.kte_calibre_name)
common.log.info(
"KoboTouchExtended:_modify_epub:Generated KePub file copy "
+ "path: {0}".format(dpath)
+ f"path: {dpath}"
)
shutil.copy(infile, dpath)

Expand Down Expand Up @@ -403,9 +399,10 @@ def upload_books(self, files, names, on_card=None, end_session=True, metadata=No
cfg.add_section("FeatureSettings")
common.log.info(
"KoboTouchExtended:upload_books:Setting FeatureSettings."
+ "FullBookPageNumbers to {0}".format(
"true" if self.full_page_numbers else "false"
)
+ "FullBookPageNumbers to "
+ "true"
if self.full_page_numbers
else "false"
)
cfg.set(
"FeatureSettings",
Expand All @@ -426,14 +423,10 @@ def filename_callback(self, path, mi):
and path.endswith(EPUB_EXT)
and mi.uuid not in self.skip_renaming_files
):
common.log.debug(
"KoboTouchExtended:filename_callback:Path - {0}".format(path)
)
common.log.debug(f"KoboTouchExtended:filename_callback:Path - {path}")
path = path[: -len(EPUB_EXT)] + KEPUB_EXT + EPUB_EXT

common.log.debug(
"KoboTouchExtended:filename_callback:New path - {0}".format(path)
)
common.log.debug(f"KoboTouchExtended:filename_callback:New path - {path}")
else:
path = super(KOBOTOUCHEXTENDED, self).filename_callback(path, mi)

Expand All @@ -449,10 +442,14 @@ def sync_booklists(self, booklists, end_session=True):
common.log.info("KoboTouchExtended:sync_booklists:Setting ImageId fields")

select_query = (
"SELECT ContentId FROM content WHERE "
+ "ContentType = ? AND "
# DeepSource picks this up as possible SQL injection from string
# concatenation, but the concatenation here is only static SQL. All
# parameters are parameterized and passed to the SQL engine for
# safe replacement.
"SELECT ContentId FROM content WHERE " # skipcq: BAN-B608
+ "ContentType = ? AND " # skipcq: BAN-B608
+ "(ImageId IS NULL OR ImageId = '')"
) # skipcq: BAN-B608
)
update_query = "UPDATE content SET ImageId = ? WHERE ContentId = ?"
try:
db = self.device_database_connection()
Expand Down
10 changes: 6 additions & 4 deletions device/koboextended_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
pass


def wrap_msg(msg):
def wrap_msg(msg: str) -> str:
return textwrap.fill(msg.strip(), 100)


Expand Down Expand Up @@ -267,11 +267,13 @@ def __init__(self, parent, device):
self.skip_failed_checkbox = create_checkbox(
_("Silently Ignore Failed Conversions"), # noqa: F821
_( # noqa: F821
"Select this to not upload any book that fails conversion to "
+ "kepub. If this is not selected, the upload process will be "
# DeepSource picks this up as possible SQL injection, which is not
# exactly correct.
"Select this to not upload any book that fails conversion to " # skipcq: BAN-B608
+ "kepub. If this is not selected, the upload process will be " # skipcq: BAN-B608
+ "stopped at the first book that fails. If this is selected, "
+ "failed books will be silently removed from the upload queue."
), # skipcq: BAN-B608
),
device.get_pref("skip_failed"),
)

Expand Down
Loading

0 comments on commit 9074fa3

Please sign in to comment.