Skip to content

Commit

Permalink
stardict: writer.py: more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Dec 7, 2024
1 parent 1c77b0a commit bb949c5
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions pyglossary/plugins/stardict/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ def write(self) -> Generator[None, EntryType, None]:
yield from self.writeGeneralMergeSyns()
else:
yield from self.writeGeneral()

if not os.listdir(self._resDir):
os.rmdir(self._resDir)

if self._dictzip:
runDictzip(f"{self._filename}.dict")
syn_file = f"{self._filename}.syn"
Expand Down Expand Up @@ -200,7 +204,6 @@ def writeCompact(self, defiFormat: str) -> Generator[None, EntryType, None]:
defiFormat: format of article definition: h - html, m - plain text
"""
log.debug(f"writeCompact: {defiFormat=}")
dictMark = 0
altIndexList = self.newSynList()

dictFile = open(self._filename + ".dict", "wb")
Expand All @@ -211,7 +214,7 @@ def writeCompact(self, defiFormat: str) -> Generator[None, EntryType, None]:
t0 = now()
wordCount = 0

entryIndex = -1
dictMark, entryIndex = 0, -1
while True:
entry = yield
if entry is None:
Expand Down Expand Up @@ -247,8 +250,6 @@ def writeCompact(self, defiFormat: str) -> Generator[None, EntryType, None]:

dictFile.close()
idxFile.close()
if not os.listdir(self._resDir):
os.rmdir(self._resDir)
log.info(f"Writing dict file took {now() - t0:.2f} seconds")

self.writeSynFile(altIndexList)
Expand All @@ -265,7 +266,6 @@ def writeGeneral(self) -> Generator[None, EntryType, None]:
sametypesequence option is not used.
"""
log.debug("writeGeneral")
dictMark = 0
altIndexList = self.newSynList()

dictFile = open(self._filename + ".dict", "wb")
Expand All @@ -277,7 +277,7 @@ def writeGeneral(self) -> Generator[None, EntryType, None]:

dictMarkToBytes, dictMarkMax = self.dictMarkToBytesFunc()

entryIndex = -1
dictMark, entryIndex = 0, -1
while True:
entry = yield
if entry is None:
Expand Down Expand Up @@ -321,8 +321,6 @@ def writeGeneral(self) -> Generator[None, EntryType, None]:

dictFile.close()
idxFile.close()
if not os.listdir(self._resDir):
os.rmdir(self._resDir)
log.info(f"Writing dict file took {now() - t0:.2f} seconds")
log.debug("defiFormatsCount = " + pformat(defiFormatCounter.most_common()))

Expand Down Expand Up @@ -384,8 +382,7 @@ def writeCompactMergeSyns(

dictMarkToBytes, dictMarkMax = self.dictMarkToBytesFunc()

entryIndex = -1
dictMark = 0
dictMark, entryIndex = 0, -1
while True:
entry = yield
if entry is None:
Expand Down Expand Up @@ -414,8 +411,6 @@ def writeCompactMergeSyns(
wordCount = self.writeIdxFile(idxBlockList)

dictFile.close()
if not os.listdir(self._resDir):
os.rmdir(self._resDir)
log.info(f"Writing dict file took {now() - t0:.2f} seconds")

self.writeIfoFile(
Expand All @@ -442,8 +437,7 @@ def writeGeneralMergeSyns(self) -> Generator[None, EntryType, None]:

dictMarkToBytes, dictMarkMax = self.dictMarkToBytesFunc()

entryIndex = -1
dictMark = 0
dictMark, entryIndex = 0, -1
while True:
entry = yield
if entry is None:
Expand Down Expand Up @@ -480,8 +474,6 @@ def writeGeneralMergeSyns(self) -> Generator[None, EntryType, None]:
wordCount = self.writeIdxFile(idxBlockList)

dictFile.close()
if not os.listdir(self._resDir):
os.rmdir(self._resDir)
log.info(f"Writing dict file took {now() - t0:.2f} seconds")
log.debug("defiFormatsCount = " + pformat(defiFormatCounter.most_common()))

Expand Down

0 comments on commit bb949c5

Please sign in to comment.