Skip to content

Commit

Permalink
Merge pull request #175 from openzim/clone_entry
Browse files Browse the repository at this point in the history
Add wrapper on `creator.addAlias`.
  • Loading branch information
rgaudin committed Dec 8, 2023
2 parents 09dbb1e + 4842bc7 commit 88ddf31
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: test
on: [push]

env:
LIBZIM_DL_VERSION: "9.0.0"
LIBZIM_DL_VERSION: "2023-12-04"
MACOSX_DEPLOYMENT_TARGET: "12.0"

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- main

env:
LIBZIM_DL_VERSION: "9.0.0"
LIBZIM_DL_VERSION: "2023-12-04"
MACOSX_DEPLOYMENT_TARGET: "12.0"
CIBW_ENVIRONMENT_PASS_LINUX: "LIBZIM_DL_VERSION"
CIBW_BUILD_VERBOSITY: "3"
Expand Down
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_alias(self, str path: str, str title: str, str targetPath: str, dict hints: Dict[Hint, pyint]):
"""Alias the (existing) entry `targetPath` as 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.addAlias(_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 addAlias(string path, string title, string targetpath, map[HintKeys, uint64_t] hints) except + nogil
void finishZimCreation() nogil except +
void setMainPath(string mainPath)
void addIllustration(unsigned int size, string content) nogil except +
Expand Down
29 changes: 29 additions & 0 deletions tests/test_libzim_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,35 @@ def test_creator_redirection(fpath, lipsum_item):
)


def test_creator_alias(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_alias("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
with pytest.raises(RuntimeError, match="doesn't exist"):
c.add_alias(
"accueil",
"bonjour",
HOME_PATH + "_non_existent",
{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 88ddf31

Please sign in to comment.