Skip to content

Commit

Permalink
fix bug of glos.addEntryObj(dataEntry) adding empty file
Browse files Browse the repository at this point in the history
because tmpDataDir is not set until glos.read()
set and create tmpDataDir on glos.tmpDataDir access, and add test, #424
  • Loading branch information
ilius committed Feb 3, 2023
1 parent 85d0aa2 commit fc27193
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pyglossary/glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pyglossary/glossary_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(
Expand Down
24 changes: 24 additions & 0 deletions tests/glossary_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit fc27193

Please sign in to comment.