Skip to content

Commit

Permalink
[fix] Unblocking uploads of some file types
Browse files Browse the repository at this point in the history
Fixes uploading file types not supported by the underlying ePub container implementation.

Fixes #173
  • Loading branch information
jgoguen committed Aug 7, 2024
1 parent 0ef53a0 commit 327f809
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
18 changes: 9 additions & 9 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from polyglot.io import PolyglotStringIO

# lxml isn't great, but I don't have access to defusedxml
from lxml.etree import ElementBase # skipcq: BAN-B410
from lxml.etree import _Element # skipcq: BAN-B410

if is_py3:
from typing import Dict
Expand All @@ -41,7 +41,7 @@
XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
CONFIGDIR = os.path.join(config_dir, "plugins")
REFERENCE_KEPUB = os.path.join(CONFIGDIR, "reference.kepub.epub")
PLUGIN_VERSION = (3, 6, 10)
PLUGIN_VERSION = (3, 6, 11)
PLUGIN_MINIMUM_CALIBRE_VERSION = (5, 0, 0)


Expand Down Expand Up @@ -123,26 +123,26 @@ def modify_epub(
# TODO: Refactor out cover detection logic so it can be directly used in
# metadata/writer.py
found_cover = False
opf: ElementBase = container.opf
cover_meta_node_list: List[ElementBase] = opf.xpath(
opf: _Element = container.opf
cover_meta_node_list: List[_Element] = opf.xpath(
'./opf:metadata/opf:meta[@name="cover"]', namespaces=OPF_NAMESPACES
)

if len(cover_meta_node_list) > 0:
cover_meta_node: ElementBase = cover_meta_node_list[0]
cover_meta_node: _Element = cover_meta_node_list[0]
cover_id = cover_meta_node.attrib.get("content", None)

log.debug("Found meta node with name=cover")

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

cover_node_list: ElementBase = opf.xpath(
cover_node_list: _Element = opf.xpath(
f'./opf:manifest/opf:item[@id="{cover_id}"]',
namespaces=OPF_NAMESPACES,
)
if len(cover_node_list) > 0:
cover_node: ElementBase = cover_node_list[0]
cover_node: _Element = cover_node_list[0]

log.debug("Found an item node with cover ID")

Expand All @@ -159,7 +159,7 @@ def modify_epub(
if not found_cover:
log.debug("Looking for cover image in OPF manifest")

node_list: List[ElementBase] = opf.xpath(
node_list: List[_Element] = opf.xpath(
"./opf:manifest/opf:item[(translate(@id, "
+ "'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"
+ '="cover" or starts-with(translate(@id, '
Expand All @@ -172,7 +172,7 @@ def modify_epub(
f"Found {len(node_list)} nodes, assuming the first is the right node"
)

node: ElementBase = node_list[0]
node: _Element = node_list[0]
if node.attrib.get("properties", "") != "cover-image":
log.info("Setting cover-image property")
node.set("properties", "cover-image")
Expand Down
19 changes: 7 additions & 12 deletions container.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def __add_content_file_reference_impl(self, infile: str, name: str) -> None:
self.commit_item(infile, keep_parsed=True)

@staticmethod
def fix_tail(item: etree.ElementBase) -> None:
def fix_tail(item: etree._Element) -> None:
"""Fix self-closing elements.
Designed only to work with self closing elements after item has just
Expand Down Expand Up @@ -451,7 +451,7 @@ def add_kobo_divs(self, name) -> None:
return name

@staticmethod
def __add_kobo_divs_to_body(root: etree.ElementBase) -> None:
def __add_kobo_divs_to_body(root: etree._Element) -> None:
body = root.xpath("./xhtml:body", namespaces={"xhtml": XHTML_NAMESPACE})[0]

# save node content for later
Expand Down Expand Up @@ -526,8 +526,8 @@ def add_kobo_spans(self, name: str) -> None:
self.commit_item(name, keep_parsed=True)

def _add_kobo_spans_to_node(
self, node: etree.ElementBase, name: str
) -> etree.ElementBase:
self, node: etree._Element, name: str
) -> etree._Element:
# process node only if it is not a comment or a processing instruction
if node is None or isinstance(
node, (etree._Comment, etree._ProcessingInstruction)
Expand All @@ -538,12 +538,7 @@ def _add_kobo_spans_to_node(
return node

# Special case some tags
try:
special_tag_match = re.search(r"^(?:\{[^\}]+\})?(\w+)$", node.tag)
except Exception as e:
raise Exception(
f"Exception: {e}\nNode class: {type(node)}\nNode tag class: {type(node.tag)}\nNode tag: {node.tag}"
)
special_tag_match = re.search(r"^(?:\{[^\}]+\})?(\w+)$", node.tag)
if special_tag_match:
# Skipped tags are just flat out skipped
if special_tag_match.group(1) in SKIPPED_TAGS:
Expand Down Expand Up @@ -605,8 +600,8 @@ def _add_kobo_spans_to_node(
return node

def _append_kobo_spans_from_text(
self, node: etree.ElementBase, text: str, name: str
) -> etree.ElementBase:
self, node: etree._Element, text: str, name: str
) -> etree._Element:
if not text:
self.log.error(f"[{name}] No text passed, can't add spans")
return False
Expand Down
5 changes: 3 additions & 2 deletions device/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ def save_settings(cls, config_widget):
def _modify_epub(
self, infile: str, metadata, container: Optional[KEPubContainer] = None
):
if container is None:
container = KEPubContainer(infile, common.log, do_cleanup=self.clean_markup)
if not infile.endswith(EPUB_EXT) or not self.kepubify_book(metadata):
if infile.endswith(KEPUB_EXT):
common.log.info(
Expand All @@ -175,6 +173,9 @@ def _modify_epub(
+ "exceptions"
)

if container is None:
container = KEPubContainer(infile, common.log, do_cleanup=self.clean_markup)

if container.is_drm_encumbered:
self.skip_renaming_files.add(metadata.uuid)
if self.upload_encumbered:
Expand Down

0 comments on commit 327f809

Please sign in to comment.