Skip to content

Commit

Permalink
Resolve several mypy issues: incorrect object type definition, needed…
Browse files Browse the repository at this point in the history
… variables not passed to functions, extraneous parameter to LintMessage
  • Loading branch information
Vince committed Oct 27, 2023
1 parent 08db07a commit 9e10ce0
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions se/se_epub_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,6 @@ def _lint_metadata_checks(self) -> list:
except Exception:
missing_metadata_elements.append("<dc:description>")

# Check for double spacing
if self.metadata_dom.xpath(f"/package/metadata/*[re:test(., '[{se.NO_BREAK_SPACE}{se.HAIR_SPACE} ]{{2,}}')]"):
double_spaced_files.append(self.metadata_file_path)

# Check for illegal Wikipedia URLs
nodes = self.metadata_dom.xpath("/package/metadata/*[contains(., '.m.wikipedia.org') or @*[contains(., '.m.wikipedia.org')]]")
if nodes:
Expand Down Expand Up @@ -1054,7 +1050,7 @@ def files_not_in_spine(self) -> set:
spine_files = set(self.spine_file_paths + [self.toc_path])
return xhtml_files.difference(spine_files)

def _lint_css_checks(self, local_css_path: str, abbr_with_whitespace: list) -> tuple:
def _lint_css_checks(self, local_css_path: Path, abbr_with_whitespace: list) -> list:
"""
Process main CSS checks
Expand Down Expand Up @@ -1110,7 +1106,7 @@ def _lint_css_checks(self, local_css_path: str, abbr_with_whitespace: list) -> t

return messages

def _update_missing_styles(filename: str, dom: se.easy_xml.EasyXmlTree, local_css: dict) -> list:
def _update_missing_styles(filename: Path, dom: se.easy_xml.EasyXmlTree, local_css: dict) -> list:
"""
Identify any missing CSS styles in the file being checked
Expand All @@ -1129,7 +1125,7 @@ def _update_missing_styles(filename: str, dom: se.easy_xml.EasyXmlTree, local_cs
missing_styles += [node.to_tag_string() for node in dom.xpath("/html/body//span[contains(@class, 'elision')]")]

# Check to see if we included poetry or verse without the appropriate styling
if filename.name not in IGNORED_FILENAMES:
if filename not in IGNORED_FILENAMES:
nodes = dom.xpath("/html/body//*[re:test(@epub:type, 'dedication|epigraph|z3998:(poem|verse|song|hymn|lyrics)')][./p/span]")
for node in nodes:
if "dedication" in node.get_attr("epub:type") and not local_css["has_dedication_style"]:
Expand All @@ -1155,7 +1151,7 @@ def _update_missing_styles(filename: str, dom: se.easy_xml.EasyXmlTree, local_cs

return missing_styles

def _lint_image_checks(self, filename: str) -> list:
def _lint_image_checks(self, filename: Path) -> list:
"""
Process image checks
Expand Down Expand Up @@ -1201,7 +1197,7 @@ def _lint_image_checks(self, filename: str) -> list:

return messages

def _lint_svg_checks(filename: str, file_contents: str, svg_dom: se.easy_xml.EasyXmlTree, self, root: str) -> list:
def _lint_svg_checks(filename: Path, file_contents: str, svg_dom: se.easy_xml.EasyXmlTree, self, root: str) -> list:
"""
Perform several checks on svg files
Expand Down Expand Up @@ -1279,7 +1275,7 @@ def _lint_svg_checks(filename: str, file_contents: str, svg_dom: se.easy_xml.Eas

return messages

def _lint_special_file_checks(filename: str, dom: se.easy_xml.EasyXmlTree, file_contents: str, ebook_flags: dict, special_file: str, self) -> list:
def _lint_special_file_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, file_contents: str, ebook_flags: dict, special_file: str, self) -> list:
"""
Process error checks in “special” .xhtml files
Expand Down Expand Up @@ -1515,7 +1511,7 @@ def _lint_special_file_checks(filename: str, dom: se.easy_xml.EasyXmlTree, file_

return messages

def _lint_xhtml_css_checks(filename: str, dom: se.easy_xml.EasyXmlTree, local_css_path: str) -> list:
def _lint_xhtml_css_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, local_css_path: Path) -> list:
"""
Process CSS checks on an .xhtml file
Expand All @@ -1533,7 +1529,7 @@ def _lint_xhtml_css_checks(filename: str, dom: se.easy_xml.EasyXmlTree, local_cs
# Do we have any elements that have specified border color?
# `transparent` and `none` are allowed values for border-color
if dom.xpath("/html/body//*[attribute::*[re:test(local-name(), 'data-css-border.+?-color') and text() != 'transparent' and text != 'none']]"):
messages.append(LintMessage("c-004", "Don’t specify border colors, so that reading systems can adjust for night mode.", se.MESSAGE_TYPE_WARNING, local_css_path, matches))
messages.append(LintMessage("c-004", "Don’t specify border colors, so that reading systems can adjust for night mode.", se.MESSAGE_TYPE_WARNING, local_css_path))

# Check that footers have the expected styling
# Footers may sometimes be aligned with text-align: center as well as text-align: right
Expand Down Expand Up @@ -1611,7 +1607,7 @@ def _lint_xhtml_css_checks(filename: str, dom: se.easy_xml.EasyXmlTree, local_cs

return messages

def _lint_xhtml_metadata_checks(filename: str, dom: se.easy_xml.EasyXmlTree, self) -> list:
def _lint_xhtml_metadata_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, self) -> list:
"""
Process metadata checks on an .xhtml file
Expand Down Expand Up @@ -1653,7 +1649,7 @@ def _lint_xhtml_metadata_checks(filename: str, dom: se.easy_xml.EasyXmlTree, sel

return messages

def _lint_xhtml_syntax_checks(filename: str, dom: se.easy_xml.EasyXmlTree, self, file_contents: str, ebook_flags: dict, language: str) -> list:
def _lint_xhtml_syntax_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, self, file_contents: str, ebook_flags: dict, language: str) -> list:
"""
Process syntax checks on an .xhtml file
Expand Down Expand Up @@ -2238,7 +2234,7 @@ def _lint_xhtml_syntax_checks(filename: str, dom: se.easy_xml.EasyXmlTree, self,

return messages

def _lint_xhtml_typography_checks(filename: str, dom: se.easy_xml.EasyXmlTree, file_contents: str, special_file: str, ebook_flags: dict, self) -> list:
def _lint_xhtml_typography_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, file_contents: str, special_file: Optional[str], ebook_flags: dict, missing_files: list, self) -> tuple:
"""
Process typography checks on an .xhtml file
Expand All @@ -2248,10 +2244,11 @@ def _lint_xhtml_typography_checks(filename: str, dom: se.easy_xml.EasyXmlTree, f
file_contents: The contents of the file being checked
special_file: A string containing the type of special file the current file is, if any
ebook_flags: A dictionary containing several flags about an ebook
missing_files: A list of missing files
self
OUTPUTS
A list of LintMessage objects
tuple
"""

messages = [];
Expand Down Expand Up @@ -2721,9 +2718,9 @@ def _lint_xhtml_typography_checks(filename: str, dom: se.easy_xml.EasyXmlTree, f
if node_text != expected_text:
messages.append(LintMessage("t-073", f"Possible transcription error in Greek. Found: [text]{node_text}[/], but expected [text]{expected_text}[/text]. Hint: Use [bash]se unicode-names[/] to see differences in Unicode characters.", se.MESSAGE_TYPE_WARNING, filename))

return messages
return (messages, missing_files)

def _lint_xhtml_xhtml_checks(filename: str, dom: se.easy_xml.EasyXmlTree, file_contents: str) -> list:
def _lint_xhtml_xhtml_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, file_contents: str) -> list:
"""
Process XHTML checks on an .xhtml file
Expand Down Expand Up @@ -2790,7 +2787,7 @@ def _lint_xhtml_xhtml_checks(filename: str, dom: se.easy_xml.EasyXmlTree, file_c

return messages

def _lint_xhtml_typo_checks(filename: str, dom: se.easy_xml.EasyXmlTree, file_contents: str, special_file: str) -> list:
def _lint_xhtml_typo_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, file_contents: str, special_file: Optional[str]) -> list:
"""
Process typo checks on an .xhtml file
Expand Down Expand Up @@ -3166,6 +3163,7 @@ def lint(self, skip_lint_ignore: bool, allowed_messages: Optional[List[str]] = N

local_css_path = self.content_path / "css/local.css"
messages: List[LintMessage] = []
typography_messages: List[LintMessage] = []
cover_svg_title = ""
titlepage_svg_title = ""
xhtml_css_classes: Dict[str, int] = {}
Expand Down Expand Up @@ -3321,6 +3319,9 @@ def lint(self, skip_lint_ignore: bool, allowed_messages: Optional[List[str]] = N
ebook_flags["has_other_sources"] = other_source_count > 0

messages = messages + _lint_metadata_checks(self)
# Check for double spacing (done here so double_spaced_files doesn't have to be passed)
if self.metadata_dom.xpath(f"/package/metadata/*[re:test(., '[{se.NO_BREAK_SPACE}{se.HAIR_SPACE} ]{{2,}}')]"):
double_spaced_files.append(self.metadata_file_path)

# Check for malformed URLs
messages = messages + _get_malformed_urls(self.metadata_dom, self.metadata_file_path)
Expand Down Expand Up @@ -3377,7 +3378,9 @@ def lint(self, skip_lint_ignore: bool, allowed_messages: Optional[List[str]] = N
directories_not_url_safe.append(Path(root) / directory)

for filename in natsorted(filenames):
print(f"Before resolve, filename is type {type(filename)}")
filename = (Path(root) / filename).resolve()
print(f"After resolve, filename is type {type(filename)}")

if filename.stem != "LICENSE":
if filename.stem == "cover.source":
Expand Down Expand Up @@ -3561,7 +3564,9 @@ def lint(self, skip_lint_ignore: bool, allowed_messages: Optional[List[str]] = N

messages = messages + _lint_xhtml_syntax_checks(filename, dom, self, file_contents, ebook_flags, language)

messages = messages + _lint_xhtml_typography_checks(filename, dom, file_contents, special_file, ebook_flags, self)
(typography_messages, missing_files) = _lint_xhtml_typography_checks(filename, dom, file_contents, special_file, ebook_flags, missing_files, self)
if typography_messages:
messages = messages + typography_messages

messages = messages + _lint_xhtml_xhtml_checks(filename, dom, file_contents)

Expand Down

0 comments on commit 9e10ce0

Please sign in to comment.