Skip to content

Commit 58d777f

Browse files
author
renaud gaudin
committed
Fixed #45: Creator now accepts pathlib for filename
Creator converts passed filename to str before passing it to wrapper. This allows transparent use of pathlib.Path Also, Creator.filename now returns a pathlib.Path, created on __init__ from the wrapper returned filename. Tests updated to use pathlib by default and check .filename type. Test ensures passing regular str still works.
1 parent 2fb30e0 commit 58d777f

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

libzim/wrapper.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ from libcpp.string cimport string
2929
from libcpp cimport bool
3030
from libcpp.memory cimport shared_ptr, make_shared, unique_ptr
3131

32+
import pathlib
3233
import datetime
3334

3435

@@ -356,23 +357,23 @@ cdef class FilePy:
356357
----------
357358
*c_file : File
358359
a pointer to a C++ File object
359-
_filename : str
360+
_filename : pathlib.Path
360361
the file name of the File Reader object
361362
"""
362363

363364
cdef wrapper.File *c_file
364365
cdef object _filename
365366

366-
def __cinit__(self, str filename):
367+
def __cinit__(self, object filename):
367368
"""Constructs a File from full zim file path.
368369
Parameters
369370
----------
370371
filename : str
371372
Full path to a zim file
372373
"""
373374

374-
self.c_file = new wrapper.File(filename.encode('UTF-8'))
375-
self._filename = self.c_file.getFilename().decode("UTF-8", "strict")
375+
self.c_file = new wrapper.File(str(filename).encode('UTF-8'))
376+
self._filename = pathlib.Path(self.c_file.getFilename().decode("UTF-8", "strict"))
376377

377378
def __dealloc__(self):
378379
if self.c_file != NULL:

libzim/writer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919

20-
20+
import pathlib
2121
import datetime
2222
from collections import defaultdict
2323

@@ -113,7 +113,7 @@ class Creator:
113113
a pointer to the C++ Creator object
114114
_finalized : bool
115115
flag if the creator was finalized
116-
_filename : str
116+
_filename : pathlib.Path
117117
Zim file path
118118
_main_page : str
119119
Zim file main page
@@ -128,8 +128,8 @@ class Creator:
128128
"""
129129

130130
def __init__(self, filename, main_page, index_language="eng", min_chunk_size=2048):
131-
self._creatorWrapper = _Creator(filename, main_page, index_language, min_chunk_size)
132-
self.filename = filename
131+
self._creatorWrapper = _Creator(str(filename), main_page, index_language, min_chunk_size)
132+
self.filename = pathlib.Path(filename)
133133
self.main_page = main_page
134134
self.language = index_language
135135
self._metadata = {}

tests/test_libzim.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import pytest
20+
import pathlib
2021

2122
from libzim.writer import Article, Blob, Creator
2223
from libzim.reader import File
@@ -108,7 +109,7 @@ def article(article_content):
108109

109110
def test_write_article(tmpdir, article):
110111
with Creator(
111-
str(tmpdir / "test.zim"), main_page="welcome", index_language="eng", min_chunk_size=2048,
112+
tmpdir / "test.zim", main_page="welcome", index_language="eng", min_chunk_size=2048,
112113
) as zim_creator:
113114
zim_creator.add_article(article)
114115
zim_creator.update_metadata(
@@ -122,14 +123,14 @@ def test_write_article(tmpdir, article):
122123

123124
def test_article_metadata(tmpdir, metadata):
124125
with Creator(
125-
str(tmpdir / "test.zim"), main_page="welcome", index_language="eng", min_chunk_size=2048,
126+
tmpdir / "test.zim", main_page="welcome", index_language="eng", min_chunk_size=2048,
126127
) as zim_creator:
127128
zim_creator.update_metadata(**metadata)
128129
assert zim_creator._metadata == metadata
129130

130131

131132
def test_creator_params(tmpdir):
132-
path = str(tmpdir / "test.zim")
133+
path = tmpdir / "test.zim"
133134
main_page = "welcome"
134135
main_page_url = f"A/{main_page}"
135136
index_language = "eng"
@@ -148,7 +149,7 @@ def test_creator_params(tmpdir):
148149

149150
def test_segfault_on_realloc(tmpdir):
150151
""" assert that we are able to delete an unclosed Creator #31 """
151-
creator = Creator(str(tmpdir / "test.zim"), "welcome", "eng", 2048)
152+
creator = Creator(tmpdir / "test.zim", "welcome", "eng", 2048)
152153
del creator # used to segfault
153154
assert True
154155

@@ -157,23 +158,33 @@ def test_noleftbehind_empty(tmpdir):
157158
""" assert that ZIM with no articles don't leave files behind #41 """
158159
fname = "test_empty.zim"
159160
with Creator(
160-
str(tmpdir / fname), main_page="welcome", index_language="eng", min_chunk_size=2048,
161+
tmpdir / fname, main_page="welcome", index_language="eng", min_chunk_size=2048,
161162
) as zim_creator:
162163
print(zim_creator)
163164

164165
assert len([p for p in tmpdir.listdir() if p.basename.startswith(fname)]) == 1
165166

166167

167168
def test_double_close(tmpdir):
168-
creator = Creator(str(tmpdir / "test.zim"), "welcome", "eng", 2048)
169+
creator = Creator(tmpdir / "test.zim", "welcome", "eng", 2048)
169170
creator.close()
170171
with pytest.raises(RuntimeError):
171172
creator.close()
172173

173174

174175
def test_default_creator_params(tmpdir):
175176
""" ensure we can init a Creator without specifying all params """
176-
creator = Creator(str(tmpdir / "test.zim"), "welcome")
177+
creator = Creator(tmpdir / "test.zim", "welcome")
177178
assert True # we could init the Creator without specifying other params
178179
assert creator.language == "eng"
179180
assert creator.main_page == "welcome"
181+
182+
183+
def test_filename_param_types(tmpdir):
184+
path = tmpdir / "test.zim"
185+
with Creator(path, "welcome") as creator:
186+
assert creator.filename == path
187+
assert isinstance(creator.filename, pathlib.Path)
188+
with Creator(str(path), "welcome") as creator:
189+
assert creator.filename == path
190+
assert isinstance(creator.filename, pathlib.Path)

tests/test_libzim_file_reader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
ZIMFILES = [
1212
{
13-
"filename": str(DATA_DIR / "wikipedia_es_physics_mini.zim"),
13+
"filename": DATA_DIR / "wikipedia_es_physics_mini.zim",
1414
"checksum": "99ea7a5598c6040c4f50b8ac0653b703",
1515
"namespaces": "-AIMX",
1616
"article_count": 22027,
@@ -43,6 +43,7 @@ def article_data():
4343
def test_zim_filename(reader, zimdata):
4444
for k, v in zimdata.items():
4545
assert getattr(reader, k) == v
46+
assert isinstance(reader.filename, Path)
4647

4748

4849
def test_zim_read(reader, article_data):

0 commit comments

Comments
 (0)