Skip to content

Commit

Permalink
Add wrapper on creator.addClone.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Oct 30, 2023
1 parent 6a7e19f commit 825d427
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
20 changes: 19 additions & 1 deletion libzim/libzim.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,24 @@ cdef class _Creator:
with nogil:
self.c_creator.addRedirection(_path, _title, _targetPath, _hints)

def add_clone(self, str path: str, str title: str, str targetPath: str, dict hints: Dict[Hint, pyint]):
"""Clone the (existing) entry `targetPath` into a new entry `path`.
Raises
------
RuntimeError
If `targetPath` entry doesn't exist.
"""
if not self._started:
raise RuntimeError("Creator not started")

cdef string _path = path.encode('UTF-8')
cdef string _title = title.encode('UTF-8')
cdef string _targetPath = targetPath.encode('UTF-8')
cdef map[zim.HintKeys, uint64_t] _hints = convertToCppHints(hints)
with nogil:
self.c_creator.addClone(_path, _title, _targetPath, _hints)

def __enter__(self):
cdef string _path = str(self._filename).encode('UTF-8')
with nogil:
Expand Down Expand Up @@ -1287,7 +1305,7 @@ class ModuleLoader(importlib.abc.Loader):
'libzim.reader': reader,
'libzim.search': search,
'libzim.suggestion': suggestion,
'libzim.version': version
'libzim.version': version
}.get(spec.name, None)

@staticmethod
Expand Down
1 change: 1 addition & 0 deletions libzim/zim.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ cdef extern from "zim/writer/creator.h" namespace "zim::writer":
void addItem(shared_ptr[WriterItem] item) nogil except +
void addMetadata(string name, string content, string mimetype) nogil except +
void addRedirection(string path, string title, string targetpath, map[HintKeys, uint64_t] hints) nogil except +
void addClone(string path, string title, string targetpath, map[HintKeys, uint64_t] hints) nogil except +
void finishZimCreation() nogil except +
void setMainPath(string mainPath)
void addIllustration(unsigned int size, string content) nogil except +
Expand Down
22 changes: 22 additions & 0 deletions tests/test_libzim_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,28 @@ def test_creator_redirection(fpath, lipsum_item):
sugg_bonjour.getResults(0, sugg_hello.getEstimatedMatches())
)

def test_creator_clone(fpath, lipsum_item):
# ensure we can't add if not started
c = Creator(fpath)
with pytest.raises(RuntimeError, match="not started"):
c.add_redirection("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
del c

with Creator(fpath) as c:
c.add_item(lipsum_item)
c.add_clone("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
with pytest.raises(RuntimeError, match="doesn't exist"):
c.add_clone("accueil", "bonjour", HOME_PATH+"_no_existitant", {Hint.FRONT_ARTICLE: True})

zim = Archive(fpath)
assert zim.entry_count == 2
assert zim.has_entry_by_path("home") is True
assert zim.has_entry_by_path("accueil") is False
assert not zim.get_entry_by_path("home").is_redirect
assert (
zim.get_entry_by_path("home").get_item().content == zim.get_entry_by_path(HOME_PATH).get_item().content
)


def test_item_notimplemented(fpath, lipsum_item):
item = Item()
Expand Down

0 comments on commit 825d427

Please sign in to comment.