Skip to content

Commit

Permalink
fix ruff --preview errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Feb 22, 2024
1 parent 0bf3d24 commit 29eda70
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 36 deletions.
1 change: 1 addition & 0 deletions pyglossary/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

__all__ = ["fileCountLines"]


def fileCountLines(filename: str, newline: bytes = b"\n") -> int:
with open(filename, "rb") as _file:
bufgen = takewhile(
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/glossary_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ def convertV2(self, args: ConvertArgs) -> "str | None":
finalOutputFile = self._compressOutput(finalOutputFile, compression)

log.info(f"Writing file {finalOutputFile!r} done.")
log.info(f"Running time of convert: {now()-tm0:.1f} seconds")
log.info(f"Running time of convert: {now() - tm0:.1f} seconds")
showMemoryUsage()
self.cleanup()

Expand Down
2 changes: 1 addition & 1 deletion pyglossary/langs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def load(self) -> None:

log.debug(
f"LangDict: loaded, {len(self)} keys, "
f"took {(now() - t0)*1000:.1f} ms",
f"took {(now() - t0) * 1000:.1f} ms",
)

def __getitem__(self, key: str) -> "Lang | None":
Expand Down
4 changes: 2 additions & 2 deletions pyglossary/plugins/aard2_slob.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _open(self, filename: str, namePostfix: str) -> "slob.Writer":

if isfile(filename):
shutil.move(filename, f"{filename}.bak")
log.warning(f"renamed existing {filename!r} to {filename+'.bak'!r}")
log.warning(f"renamed existing {filename!r} to {filename + '.bak'!r}")
self._slobWriter = slobWriter = slob.Writer(
filename,
observer=self._slobObserver,
Expand Down Expand Up @@ -428,7 +428,7 @@ def write(self) -> "Generator[None, EntryType, None]":
fileIndex += 1
slobWriter = self._open(
f"{filenameNoExt}.{fileIndex}.slob",
f" (part {fileIndex+1})",
f" (part {fileIndex + 1})",
)
sumBlobSize = 0
entryCount = 0
20 changes: 9 additions & 11 deletions pyglossary/plugins/babylon_bgl/bgl_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,7 @@ def setGlossaryInfo(self) -> None:
continue
# TODO: a bool flag to add empty value infos?
# leave "creationTime" and "lastUpdated" as is
if key in {
"utf8Encoding",
}:
if key == "utf8Encoding":
key = "bgl_" + key
try:
glos.setInfo(key, s_value)
Expand Down Expand Up @@ -814,7 +812,7 @@ def __iter__(self) -> "Iterator[EntryType]":
continue
pos = wordData.pos
# defi:
succeed, pos, u_defi, b_defi = self.readEntryDefi(
succeed, pos, u_defi, _b_defi = self.readEntryDefi(
block,
pos,
wordData,
Expand Down Expand Up @@ -993,7 +991,7 @@ def readEntry_Type11(
)
return Err
b_word = block.data[pos : pos + wordLen]
u_word, u_word_html = self.processKey(b_word)
u_word, _u_word_html = self.processKey(b_word)
pos += wordLen
self.wordLenMax = max(self.wordLenMax, len(u_word))

Expand Down Expand Up @@ -1219,7 +1217,7 @@ def processAlternativeKey(self, b_word: bytes, b_key: bytes) -> str:
b_word is a bytes instance
returns u_word_main, as str instance (utf-8 encoding).
"""
b_word_main, strip_count = stripDollarIndexes(b_word)
b_word_main, _strip_count = stripDollarIndexes(b_word)
# convert to unicode
if self._strict_string_conversion:
try:
Expand Down Expand Up @@ -1274,7 +1272,7 @@ def processDefi(self, b_defi: bytes, b_key: bytes) -> str:
fields.u_defi = fields.u_defi.strip()

if fields.b_title:
fields.u_title, singleEncoding = self.decodeCharsetTags(
fields.u_title, _singleEncoding = self.decodeCharsetTags(
fields.b_title,
self.sourceEncoding,
)
Expand All @@ -1283,7 +1281,7 @@ def processDefi(self, b_defi: bytes, b_key: bytes) -> str:

if fields.b_title_trans:
# sourceEncoding or targetEncoding ?
fields.u_title_trans, singleEncoding = self.decodeCharsetTags(
fields.u_title_trans, _singleEncoding = self.decodeCharsetTags(
fields.b_title_trans,
self.sourceEncoding,
)
Expand All @@ -1298,7 +1296,7 @@ def processDefi(self, b_defi: bytes, b_key: bytes) -> str:
# what is this?
pass
elif fields.code_transcription_50 == 0x1B:
fields.u_transcription_50, singleEncoding = self.decodeCharsetTags(
fields.u_transcription_50, _singleEncoding = self.decodeCharsetTags(
fields.b_transcription_50,
self.sourceEncoding,
)
Expand All @@ -1323,7 +1321,7 @@ def processDefi(self, b_defi: bytes, b_key: bytes) -> str:

if fields.b_transcription_60:
if fields.code_transcription_60 == 0x1B:
fields.u_transcription_60, singleEncoding = self.decodeCharsetTags(
fields.u_transcription_60, _singleEncoding = self.decodeCharsetTags(
fields.b_transcription_60,
self.sourceEncoding,
)
Expand All @@ -1341,7 +1339,7 @@ def processDefi(self, b_defi: bytes, b_key: bytes) -> str:
)

if fields.b_field_1a:
fields.u_field_1a, singleEncoding = self.decodeCharsetTags(
fields.u_field_1a, _singleEncoding = self.decodeCharsetTags(
fields.b_field_1a,
self.sourceEncoding,
)
Expand Down
4 changes: 2 additions & 2 deletions pyglossary/plugins/babylon_bgl/bgl_reader_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ def __del__(self) -> None:
BglReader.__del__(self)

def readEntryWord(self, block, pos):
succeed, pos, u_word, b_word = BglReader.readEntryWord(self, block, pos)
succeed, pos, _u_word, b_word = BglReader.readEntryWord(self, block, pos)
if not succeed:
return
self.rawDumpFileWriteText(f"\n\nblock type = {block.type}\nkey = ")
self.rawDumpFileWriteData(b_word)

def readEntryDefi(self, block, pos, b_key):
succeed, pos, u_defi, b_defi = BglReader.readEntryDefi(self, block, pos, b_key)
succeed, pos, _u_defi, b_defi = BglReader.readEntryDefi(self, block, pos, b_key)
if not succeed:
return
self.rawDumpFileWriteText("\ndefi = ")
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/dicformids.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def writeBucket(dicIndex: int, entryList: "list[EntryType]") -> None:
dicFp = open(
join(
self._dirname,
f"directory{self.directoryPostfix}{dicIndex+1}.csv",
f"directory{self.directoryPostfix}{dicIndex + 1}.csv",
),
mode="w",
encoding="utf-8",
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/dsl/lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def lexRefText(tr: TransformerType) -> tuple[LexType, ErrorType]:
if not target:
target = text

tr.output += f'<a href={quoteattr("bword://"+target)}>{escape(text)}</a>'
tr.output += f'<a href={quoteattr("bword://" + target)}>{escape(text)}</a>'
tr.resetBuf()
return lexRoot, None

Expand Down
4 changes: 2 additions & 2 deletions pyglossary/plugins/html_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ def addLinks(text: str, pos: int) -> None:
b_size = len(text[start : m.end()].encode(encoding))
linksTxtFileObj.write(
f"{escapeNTB(target)}\t"
f"{len(self._filenameList)-1}\t"
f"{pos+b_start:x}\t"
f"{len(self._filenameList) - 1}\t"
f"{pos + b_start:x}\t"
f"{b_size:x}\n",
)
linksTxtFileObj.flush()
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/info_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def write(self) -> "Generator[None, EntryType, None]":
tag = tag.strip("< />").lower()
allTagsCounter[tag] += 1
elif defiFormat == "b":
filenameNoExt, ext = splitext(entry.s_word)
_filenameNoExt, ext = splitext(entry.s_word)
ext = ext.lstrip(".")
dataEntryExtCounter[ext] += 1

Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/octopus_mdict_new/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def open(self, filename: str) -> None:
FILE.3.mdd
"""

filenameNoExt, ext = splitext(self._filename)
filenameNoExt, _ext = splitext(self._filename)
mddBase = "".join([filenameNoExt, extsep])
for fname in (f"{mddBase}mdd", f"{mddBase}1.mdd"):
if isfile(fname):
Expand Down
4 changes: 3 additions & 1 deletion pyglossary/plugins/quickdic6.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,10 @@ def write_hashset(
write_byte(fp, 0x78)
return fp.tell() - write_start_offset


T = TypeVar("T")


def read_list(
fp: IO[bytes],
fun: "Callable[[IO[bytes]], T]",
Expand Down Expand Up @@ -689,7 +691,7 @@ def _extract_synonyms_from_indices(self) -> None:
self._synonyms: dict[tuple[int, int], set[str]] = {}
assert self._dic is not None
for index in self._dic.indices:
_, _, _, _, swap_flag, _, index_entries, _, rows = index
_, _, _, _, swap_flag, _, index_entries, _, _ = index

# Note that we ignore swapped indices because pyglossary assumes
# uni-directional dictionaries.
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _writeInfo(self) -> None:
key = key.replace("'", "''")
value = value.replace("'", "''")
fileObj.write(
f"INSERT INTO dbinfo_extra VALUES({index+1}, "
f"INSERT INTO dbinfo_extra VALUES({index + 1}, "
f"'{key}', '{value}');\n",
)

Expand Down
16 changes: 8 additions & 8 deletions pyglossary/plugins/stardict.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ def writeCompact(self, defiFormat: str) -> "Generator[None, EntryType, None]":
idxFile.close()
if not os.listdir(self._resDir):
os.rmdir(self._resDir)
log.info(f"Writing dict file took {now()-t0:.2f} seconds")
log.info(f"Writing dict file took {now() - t0:.2f} seconds")

self.writeSynFile(altIndexList)
self.writeIfoFile(
Expand Down Expand Up @@ -1049,7 +1049,7 @@ def writeGeneral(self) -> "Generator[None, EntryType, None]":
idxFile.close()
if not os.listdir(self._resDir):
os.rmdir(self._resDir)
log.info(f"Writing dict file took {now()-t0:.2f} seconds")
log.info(f"Writing dict file took {now() - t0:.2f} seconds")
log.debug("defiFormatsCount = " + pformat(defiFormatCounter.most_common()))

self.writeSynFile(altIndexList)
Expand All @@ -1073,7 +1073,7 @@ def writeSynFile(self, altIndexList: "T_SdList[tuple[bytes, int]]") -> None:
# 0.20 seconds without key function (default sort)

log.info(
f"Sorting {len(altIndexList)} synonyms took {now()-t0:.2f} seconds",
f"Sorting {len(altIndexList)} synonyms took {now() - t0:.2f} seconds",
)
log.info(f"Writing {len(altIndexList)} synonyms...")
t0 = now()
Expand All @@ -1085,7 +1085,7 @@ def writeSynFile(self, altIndexList: "T_SdList[tuple[bytes, int]]") -> None:
),
)
log.info(
f"Writing {len(altIndexList)} synonyms took {now()-t0:.2f} seconds",
f"Writing {len(altIndexList)} synonyms took {now() - t0:.2f} seconds",
)

def writeCompactMergeSyns(
Expand Down Expand Up @@ -1150,7 +1150,7 @@ def writeCompactMergeSyns(
dictFile.close()
if not os.listdir(self._resDir):
os.rmdir(self._resDir)
log.info(f"Writing dict file took {now()-t0:.2f} seconds")
log.info(f"Writing dict file took {now() - t0:.2f} seconds")

self.writeIfoFile(
wordCount,
Expand Down Expand Up @@ -1223,7 +1223,7 @@ def writeGeneralMergeSyns(self) -> "Generator[None, EntryType, None]":
dictFile.close()
if not os.listdir(self._resDir):
os.rmdir(self._resDir)
log.info(f"Writing dict file took {now()-t0:.2f} seconds")
log.info(f"Writing dict file took {now() - t0:.2f} seconds")
log.debug("defiFormatsCount = " + pformat(defiFormatCounter.most_common()))

self.writeIfoFile(
Expand All @@ -1242,14 +1242,14 @@ def writeIdxFile(self, indexList: "T_SdList[tuple[bytes, bytes]]") -> int:

indexList.sort()
log.info(
f"Sorting {len(indexList)} {filename} took {now()-t0:.2f} seconds",
f"Sorting {len(indexList)} {filename} took {now() - t0:.2f} seconds",
)
log.info(f"Writing {len(indexList)} index entries...")
t0 = now()
with open(filename, mode="wb") as indexFile:
indexFile.write(b"".join(key + b"\x00" + value for key, value in indexList))
log.info(
f"Writing {len(indexList)} {filename} took {now()-t0:.2f} seconds",
f"Writing {len(indexList)} {filename} took {now() - t0:.2f} seconds",
)
return len(indexList)

Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/yomichan.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def flushTerms() -> None:
return

with open(
f"term_bank_{termBankIndex+1}.json",
f"term_bank_{termBankIndex + 1}.json",
mode="w",
encoding="utf-8",
) as _file:
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ lint.select = [
"W", # pycodestyle Warning
"YTT", # flake8-2020
"ARG", # flake8-unused-arguments: ARG001 Unused function argument
# "FURB",

# "C90", # mccabe: C901: {name} is too complex ({complexity})
# "N", # pep8-naming
Expand All @@ -32,6 +33,7 @@ lint.select = [
# "ANN", # flake8-annotationsq, still gives errors for `self`
]
lint.ignore = [
"FURB101", # `open` and `read` should be replaced by `Path(rootConfJsonFile).read_text()`
"PGH003", # Use specific rule codes when ignoring type issues
"B019", # Use of `functools.lru_cache` or `functools.cache` on methods can lead to memory leaks
"D100", # Missing docstring in public module
Expand Down Expand Up @@ -137,7 +139,7 @@ lint.dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
"scripts/wiki-formats.py" = ["E501"]
"pyglossary/io_utils.py" = ["ANN"]
"pyglossary/plugin_lib/*.py" = ["ANN"]
"pyglossary/plugins/babylon_bgl/bgl_reader_debug.py" = ["ANN"]
"pyglossary/plugins/babylon_bgl/bgl_reader_debug.py" = ["ANN", "FURB"]
"pyglossary/ui/*.py" = ["ANN"]
"pyglossary/ui/*/*.py" = ["ANN"]
"tests/*.py" = ["ANN"]
Expand Down
2 changes: 1 addition & 1 deletion scripts/wiktextract/extract-schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def parseDict(data: "dict[str, Any]", path: "list[str]", node: Node):


def commonValuesSortKey(item):
key, values = item
_key, values = item
return abs(len(values) - 5)


Expand Down

0 comments on commit 29eda70

Please sign in to comment.