Skip to content

Commit

Permalink
re-format all codes with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Mar 14, 2024
1 parent a83ce44 commit 80a642e
Show file tree
Hide file tree
Showing 42 changed files with 187 additions and 314 deletions.
4 changes: 2 additions & 2 deletions pyglossary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
__version__ = VERSION

__all__ = [
"Glossary",
"__version__",
"Glossary",
"__version__",
]
4 changes: 4 additions & 0 deletions pyglossary/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ def compressionOpenFunc(c: str) -> "Callable | None":
return open
if c == "gz":
import gzip

return gzip.open
if c == "bz2":
import bz2

return bz2.open
if c == "lzma":
import lzma

return lzma.open
if c == "dz":
import gzip

return gzip.open
return None

Expand Down
3 changes: 1 addition & 2 deletions pyglossary/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,7 @@ def _unix_show_exception(
pip = "pip3"
else:
raise RuntimeError(
f"Unknown path separator(os.sep=={os.sep!r})"
", unknown operating system!",
f"Unknown path separator(os.sep=={os.sep!r}), unknown operating system!",
)

pluginsDir = join(rootDir, "pyglossary", "plugins")
Expand Down
6 changes: 2 additions & 4 deletions pyglossary/ebook_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,13 @@ class EbookWriter:
</body>
</html>"""
INDEX_XHTML_LINK_TEMPLATE = (
' <span class="indexGroup">'
'<a href="{ref}">{label}</a></span>'
' <span class="indexGroup"><a href="{ref}">{label}</a></span>'
)

INDEX_XHTML_LINK_JOINER = " &#8226;\n"

OPF_MANIFEST_ITEM_TEMPLATE = (
' <item href="{ref}" id="{id}"'
' media-type="{mediaType}" />'
' <item href="{ref}" id="{id}" media-type="{mediaType}" />'
)

OPF_SPINE_ITEMREF_TEMPLATE = ' <itemref idref="{id}" />'
Expand Down
20 changes: 7 additions & 13 deletions pyglossary/entry_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,7 @@ def fixStr(st: str) -> str:

class RemoveHtmlTags(EntryFilter):
name = "remove_html"
desc = (
"Remove given comma-separated HTML tags"
" (not their contents) from definition"
)
desc = "Remove given comma-separated HTML tags (not their contents) from definition"

def __init__(self, glos: "GlossaryType", tagsStr: str) -> None:
tags = tagsStr.split(",")
Expand Down Expand Up @@ -264,10 +261,7 @@ def __init__(
) -> None:
log.info("Normalizing HTML tags")
self._pattern = re.compile(
"(" + "|".join(
fr"</?{tag}[^<>]*?>"
for tag in self._tags
) + ")",
"(" + "|".join(rf"</?{tag}[^<>]*?>" for tag in self._tags) + ")",
re.DOTALL | re.IGNORECASE,
)

Expand Down Expand Up @@ -510,7 +504,7 @@ def run(self, entry: "EntryType") -> "EntryType | None":
self._max_mem_usage = usage
word = entry.s_word
if len(word) > self.MAX_WORD_LEN:
word = word[:self.MAX_WORD_LEN - 3] + "..."
word = word[: self.MAX_WORD_LEN - 3] + "..."
core.trace(log, f"MaxMemUsage: {usage:,}, {word=}")
return entry

Expand All @@ -529,18 +523,18 @@ def run(self, entry: "EntryType") -> "EntryType | None":
("normalize_html", False, NormalizeHtml),
("unescape_word_links", False, UnescapeWordLinks),
(None, True, LanguageCleanup),

# -------------------------------------
# TODO
# ("text_list_symbol_cleanup", False, TextListSymbolCleanup),

# -------------------------------------
(None, True, NonEmptyWordFilter),
(None, True, NonEmptyDefiFilter),
(None, True, RemoveEmptyAndDuplicateAltWords),

# -------------------------------------
# filters that are enabled by plugins using glossary methods:
(None, False, PreventDuplicateWords),
(None, False, StripFullHtml),

# -------------------------------------
# filters are added conditionally (other than with config or glossary methods):
(None, False, ShowProgressBar),
(None, False, ShowMaxMemoryUsage),
Expand Down
13 changes: 9 additions & 4 deletions pyglossary/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from itertools import (
repeat,
takewhile,
Expand All @@ -9,8 +10,12 @@
def fileCountLines(filename: str, newline: bytes = b"\n") -> int:
with open(filename, "rb") as _file:
bufgen = takewhile(
lambda x: x, (_file.read(1024 * 1024) for _ in repeat(None)),
)
return sum(
buf.count(newline) for buf in bufgen if buf
lambda x: x, # predicate
(_file.read(1024 * 1024) for _ in repeat(None)), # iterable
)
return sum(buf.count(newline) for buf in bufgen if buf)


if __name__ == "__main__":
for filename in sys.argv[1:]:
print(fileCountLines(filename), filename) # noqa: T201
Loading

0 comments on commit 80a642e

Please sign in to comment.