Skip to content

Commit 88ddf31

Browse files
authored
Merge pull request #175 from openzim/clone_entry
Add wrapper on `creator.addAlias`.
2 parents 09dbb1e + 4842bc7 commit 88ddf31

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: test
22
on: [push]
33

44
env:
5-
LIBZIM_DL_VERSION: "9.0.0"
5+
LIBZIM_DL_VERSION: "2023-12-04"
66
MACOSX_DEPLOYMENT_TARGET: "12.0"
77

88
jobs:

.github/workflows/wheels.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
- main
88

99
env:
10-
LIBZIM_DL_VERSION: "9.0.0"
10+
LIBZIM_DL_VERSION: "2023-12-04"
1111
MACOSX_DEPLOYMENT_TARGET: "12.0"
1212
CIBW_ENVIRONMENT_PASS_LINUX: "LIBZIM_DL_VERSION"
1313
CIBW_BUILD_VERBOSITY: "3"

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_alias(self, str path: str, str title: str, str targetPath: str, dict hints: Dict[Hint, pyint]):
399+
"""Alias the (existing) entry `targetPath` as 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.addAlias(_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 addAlias(string path, string title, string targetpath, map[HintKeys, uint64_t] hints) except + nogil
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,35 @@ def test_creator_redirection(fpath, lipsum_item):
468468
)
469469

470470

471+
def test_creator_alias(fpath, lipsum_item):
472+
# ensure we can't add if not started
473+
c = Creator(fpath)
474+
with pytest.raises(RuntimeError, match="not started"):
475+
c.add_redirection("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
476+
del c
477+
478+
with Creator(fpath) as c:
479+
c.add_item(lipsum_item)
480+
c.add_alias("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
481+
with pytest.raises(RuntimeError, match="doesn't exist"):
482+
c.add_alias(
483+
"accueil",
484+
"bonjour",
485+
HOME_PATH + "_non_existent",
486+
{Hint.FRONT_ARTICLE: True},
487+
)
488+
489+
zim = Archive(fpath)
490+
assert zim.entry_count == 2
491+
assert zim.has_entry_by_path("home") is True
492+
assert zim.has_entry_by_path("accueil") is False
493+
assert not zim.get_entry_by_path("home").is_redirect
494+
assert (
495+
zim.get_entry_by_path("home").get_item().content
496+
== zim.get_entry_by_path(HOME_PATH).get_item().content
497+
)
498+
499+
471500
def test_item_notimplemented(fpath, lipsum_item):
472501
item = Item()
473502

0 commit comments

Comments
 (0)