Skip to content

Commit 8bbca3d

Browse files
mgautierfrrgaudin
authored andcommitted
Add wrapper on creator.addClone.
1 parent 7c62b0c commit 8bbca3d

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

libzim/libzim.pyx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,24 @@ cdef class _Creator:
395395
with nogil:
396396
self.c_creator.addRedirection(_path, _title, _targetPath, _hints)
397397

398+
def add_clone(self, str path: str, str title: str, str targetPath: str, dict hints: Dict[Hint, pyint]):
399+
"""Clone the (existing) entry `targetPath` into a new entry `path`.
400+
401+
Raises
402+
------
403+
RuntimeError
404+
If `targetPath` entry doesn't exist.
405+
"""
406+
if not self._started:
407+
raise RuntimeError("Creator not started")
408+
409+
cdef string _path = path.encode('UTF-8')
410+
cdef string _title = title.encode('UTF-8')
411+
cdef string _targetPath = targetPath.encode('UTF-8')
412+
cdef map[zim.HintKeys, uint64_t] _hints = convertToCppHints(hints)
413+
with nogil:
414+
self.c_creator.addClone(_path, _title, _targetPath, _hints)
415+
398416
def __enter__(self):
399417
cdef string _path = str(self._filename).encode('UTF-8')
400418
with nogil:
@@ -1287,7 +1305,7 @@ class ModuleLoader(importlib.abc.Loader):
12871305
'libzim.reader': reader,
12881306
'libzim.search': search,
12891307
'libzim.suggestion': suggestion,
1290-
'libzim.version': version
1308+
'libzim.version': version
12911309
}.get(spec.name, None)
12921310

12931311
@staticmethod

libzim/zim.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ cdef extern from "zim/writer/creator.h" namespace "zim::writer":
7070
void addItem(shared_ptr[WriterItem] item) nogil except +
7171
void addMetadata(string name, string content, string mimetype) nogil except +
7272
void addRedirection(string path, string title, string targetpath, map[HintKeys, uint64_t] hints) nogil except +
73+
void addClone(string path, string title, string targetpath, map[HintKeys, uint64_t] hints) nogil except +
7374
void finishZimCreation() nogil except +
7475
void setMainPath(string mainPath)
7576
void addIllustration(unsigned int size, string content) nogil except +

tests/test_libzim_creator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,28 @@ def test_creator_redirection(fpath, lipsum_item):
467467
sugg_bonjour.getResults(0, sugg_hello.getEstimatedMatches())
468468
)
469469

470+
def test_creator_clone(fpath, lipsum_item):
471+
# ensure we can't add if not started
472+
c = Creator(fpath)
473+
with pytest.raises(RuntimeError, match="not started"):
474+
c.add_redirection("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
475+
del c
476+
477+
with Creator(fpath) as c:
478+
c.add_item(lipsum_item)
479+
c.add_clone("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
480+
with pytest.raises(RuntimeError, match="doesn't exist"):
481+
c.add_clone("accueil", "bonjour", HOME_PATH+"_no_existitant", {Hint.FRONT_ARTICLE: True})
482+
483+
zim = Archive(fpath)
484+
assert zim.entry_count == 2
485+
assert zim.has_entry_by_path("home") is True
486+
assert zim.has_entry_by_path("accueil") is False
487+
assert not zim.get_entry_by_path("home").is_redirect
488+
assert (
489+
zim.get_entry_by_path("home").get_item().content == zim.get_entry_by_path(HOME_PATH).get_item().content
490+
)
491+
470492

471493
def test_item_notimplemented(fpath, lipsum_item):
472494
item = Item()

0 commit comments

Comments
 (0)