From fc27193898d644643167250730c51e07605f5d9c Mon Sep 17 00:00:00 2001 From: Saeed Rasooli Date: Fri, 3 Feb 2023 07:02:49 +0330 Subject: [PATCH] fix bug of glos.addEntryObj(dataEntry) adding empty file because tmpDataDir is not set until glos.read() set and create tmpDataDir on glos.tmpDataDir access, and add test, #424 --- pyglossary/glossary.py | 5 +++++ pyglossary/glossary_utils.py | 6 +++--- tests/glossary_test.py | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/pyglossary/glossary.py b/pyglossary/glossary.py index c0788b4d5..b1d20f3ce 100644 --- a/pyglossary/glossary.py +++ b/pyglossary/glossary.py @@ -438,6 +438,8 @@ def filename(self) -> str: @property def tmpDataDir(self) -> str: + if not self._tmpDataDir: + self._setTmpDataDir(self._filename) return self._tmpDataDir def wordTitleStr( @@ -532,6 +534,7 @@ def _createReader(self, format: str, options: "Dict[str, Any]") -> "Any": return reader def _setTmpDataDir(self, filename: str): + import uuid # good thing about cacheDir is that we don't have to clean it up after # conversion is finished. # specially since dataEntry.save(...) will move the file from cacheDir @@ -543,6 +546,8 @@ def _setTmpDataDir(self, filename: str): # if self._hasWriteAccessToDir(f"{filename}_res", os.W_OK): # self._tmpDataDir = f"{filename}_res" # else: + if not filename: + filename = uuid.uuid1().hex self._tmpDataDir = join(cacheDir, os.path.basename(filename) + "_res") log.debug(f"tmpDataDir = {self._tmpDataDir}") os.makedirs(self._tmpDataDir, mode=0o700, exist_ok=True) diff --git a/pyglossary/glossary_utils.py b/pyglossary/glossary_utils.py index ba340bba6..27b5f4082 100644 --- a/pyglossary/glossary_utils.py +++ b/pyglossary/glossary_utils.py @@ -40,10 +40,10 @@ def __init__(self, glos): self._glos = glos self._sortKey = None - def append(self, entry): + def append(self, entry: "BaseEntry"): self._l.append(entry.getRaw(self._glos)) - def insert(self, pos, entry): + def insert(self, pos, entry: "BaseEntry"): self._l.insert(pos, entry.getRaw(self._glos)) def clear(self): @@ -52,7 +52,7 @@ def clear(self): def __len__(self): return len(self._l) - def __iter__(self): + def __iter__(self) -> "Iterator[BaseEntry]": glos = self._glos for rawEntry in self._l: yield Entry.fromRaw( diff --git a/tests/glossary_test.py b/tests/glossary_test.py index a4114be1e..9fb92d937 100644 --- a/tests/glossary_test.py +++ b/tests/glossary_test.py @@ -861,6 +861,30 @@ def test_addEntries_2(self): [entry.l_word for entry in glos], ) + def test_addEntries_3(self): + glos = self.glos = Glossary() + glos.addEntryObj(glos.newEntry(["a"], "test 1")) + glos.addEntryObj(glos.newEntry(["b"], "test 3")) + glos.addEntryObj(glos.newDataEntry( + "file.bin", + b"hello\x00world", + )) + glos.updateEntryFilters() + glos.updateIter() + wordListList = [] + dataEntries = [] + for entry in glos: + wordListList.append(entry.l_word) + if entry.isData(): + dataEntries.append(entry) + self.assertEqual( + wordListList, + [['a'], ['b'], ["file.bin"]], + ) + self.assertEqual(len(dataEntries), 1) + self.assertEqual(dataEntries[0].getFileName(), "file.bin") + self.assertEqual(dataEntries[0].data, b"hello\x00world") + def test_sortWords_1(self): glos = self.glos = Glossary() wordsList = self.addWords(