Skip to content

Commit 07e0534

Browse files
author
renaud gaudin
committed
WIP: Switch to Illustration API
Creator `set_faviconpath(path)` is now replaced with `add_illustration(size, content)` which instead of recording the path of a Favicon Entry, receives the bytes of a PNG image for a specific size (adding a 48 sized one being mandatory as per zim spec.) Archive `has_favicon_entry` is replaced with `has_illustration(size=None)` Archive `favicon_entry` is replaced with `get_illustration_item(size=None)` Size is optionnal and defaults to 48 in libzim. Note that we used to retrieve an Entry and now receive an Item. Additionnaly, Archive gets a `get_illustration_sizes()` which returns a set of int sizes for which an illustration is present. WARNING: this is not implemented ATM and simply return a set with 48 regarless of what's currently in the ZIM.
1 parent 3bb9cfe commit 07e0534

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

libzim/lib.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ class ZimArchive : public zim::Archive
8181
{ return to_ptr<ZimEntry>(zim::Archive::getEntryByTitle(title)); }
8282
ZimEntry* getMainEntry() const
8383
{ return to_ptr<ZimEntry>(zim::Archive::getMainEntry()); }
84-
ZimEntry* getFaviconEntry() const
85-
{ return to_ptr<ZimEntry>(zim::Archive::getFaviconEntry()); }
84+
// ZimEntry* getFaviconEntry() const
85+
// { return to_ptr<ZimEntry>(zim::Archive::getFaviconEntry()); }
86+
ZimItem* getIllustrationItem() const
87+
{ return to_ptr<ZimItem>(zim::Archive::getIllustrationItem()); }
88+
ZimItem* getIllustrationItem(unsigned int size) const
89+
{ return to_ptr<ZimItem>(zim::Archive::getIllustrationItem(size)); }
8690
std::string getUuid() const
8791
{ zim::Uuid uuid = zim::Archive::getUuid();
8892
std::string uuids(uuid.data, uuid.size()); return uuids; }

libzim/wrapper.pxd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ cdef extern from "zim/writer/creator.h" namespace "zim::writer":
6969
void addRedirection(string path, string title, string targetpath) nogil except +
7070
void finishZimCreation() nogil except +
7171
void setMainPath(string mainPath)
72-
void setFaviconPath(string faviconPath)
72+
void addIllustration(unsigned int size, string content)
7373

7474
cdef extern from "lib.h":
7575
# The only thing we need to know here is how to create the Wrapper.
@@ -144,15 +144,18 @@ cdef extern from "lib.h":
144144
vector[string] getMetadataKeys() except +
145145

146146
ZimEntry* getMainEntry() except +
147-
ZimEntry* getFaviconEntry() except +
147+
ZimItem* getIllustrationItem() except +
148+
ZimItem* getIllustrationItem(int size) except +
148149
size_type getEntryCount() except +
149150

150151
string getChecksum() except +
151152
string getFilename() except +
152153
string getUuid() except +
153154

154155
bool hasMainEntry() except +
155-
bool hasFaviconEntry() except +
156+
bool hasIllustration() except +
157+
bool hasIllustration(unsigned int size) except +
158+
# set[unsigned int] getIllustrationSizes() except +
156159
bool hasEntryByPath(string path) except +
157160
bool hasEntryByTitle(string title) except +
158161
bool is_multiPart() except +

libzim/wrapper.pyx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ from cpython.buffer cimport PyBUF_WRITABLE
3131
from libc.stdint cimport uint64_t
3232
from libcpp.string cimport string
3333
from libcpp cimport bool
34+
from libcpp.set cimport set
3435
from libcpp.memory cimport shared_ptr, make_shared, unique_ptr
3536

3637
import pathlib
@@ -240,9 +241,9 @@ cdef class Creator:
240241
self.c_creator.setMainPath(mainPath.encode('utf8'))
241242
return self
242243

243-
def set_faviconpath(self, str faviconPath) -> Creator:
244-
self.c_creator.setFaviconPath(faviconPath.encode('utf8'))
245-
return self
244+
def add_illustration(self, size: int, content):
245+
cdef string _content = content
246+
self.c_creator.addIllustration(size, _content)
246247

247248
# def set_uuid(self, uuid) -> Creator:
248249
# self.c_creator.setUuid(uuid)
@@ -561,14 +562,6 @@ cdef class PyArchive:
561562
def main_entry(self) -> Entry:
562563
return Entry.from_entry(self.c_archive.getMainEntry())
563564

564-
@property
565-
def has_favicon_entry(self) -> bool:
566-
return self.c_archive.hasFaviconEntry()
567-
568-
@property
569-
def favicon_entry(self) -> Entry:
570-
return Entry.from_entry(self.c_archive.getFaviconEntry())
571-
572565
@property
573566
def uuid(self) -> UUID:
574567
return UUID(self.c_archive.getUuid().hex())
@@ -605,6 +598,26 @@ cdef class PyArchive:
605598
def entry_count(self) -> int:
606599
return self.c_archive.getEntryCount()
607600

601+
def get_illustration_sizes(self):
602+
# FIXME: using static shortcut instead of libzim's
603+
# cdef set[unsigned int] sizes = self.c_archive.getIllustrationSizes()
604+
return {48}
605+
606+
def has_illustration(self, size: int = None) -> bool:
607+
""" whether Archive has an Illustration metadata for this size """
608+
if size is not None:
609+
return self.c_archive.hasIllustration(size)
610+
return self.c_archive.hasIllustration()
611+
612+
def get_illustration_item(self, size: int = None) -> Item:
613+
""" Illustration Metadata Item for this size """
614+
try:
615+
if size is not None:
616+
return Item.from_item(self.c_archive.getIllustrationItem(size))
617+
return Item.from_item(self.c_archive.getIllustrationItem())
618+
except RuntimeError as e:
619+
raise KeyError(str(e))
620+
608621
def suggest(self, query: str, start: int = 0, end: int = 10) -> Generator[str, None, None]:
609622
""" Paths of suggested entries in the archive from a title query -> Generator[str, None, None]
610623

tests/test_libzim_creator.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -331,27 +331,18 @@ def test_creator_mainpath(fpath, lipsum_item):
331331
assert zim.main_entry
332332

333333

334-
def test_creator_faviconpath(fpath, favicon_data):
335-
favicon_path = HOME_PATH
336-
favicon_item = StaticItem(
337-
mimetype="image/png", path=favicon_path, content=favicon_data
338-
)
339-
with Creator(fpath).set_faviconpath(favicon_path) as c:
340-
c.add_item(favicon_item)
341-
342-
zim = Archive(fpath)
343-
assert zim.has_favicon_entry is True
344-
assert zim.favicon_entry.path == "favicon"
345-
assert zim.favicon_entry.get_item().path == favicon_path
346-
347-
fpath.unlink()
348-
334+
def test_creator_illustration(fpath, favicon_data):
349335
with Creator(fpath) as c:
350-
c.add_item(favicon_item)
336+
c.add_illustration(48, favicon_data)
337+
c.add_illustration(96, favicon_data)
338+
351339
zim = Archive(fpath)
352-
assert zim.has_favicon_entry is False
353-
with pytest.raises(RuntimeError):
354-
assert zim.favicon_entry
340+
assert zim.has_illustration() is True
341+
assert zim.has_illustration(48) is True
342+
assert zim.has_illustration(96) is True
343+
assert zim.has_illustration(128) is False
344+
assert bytes(zim.get_illustration_item().content) == favicon_data
345+
assert bytes(zim.get_illustration_item(96).content) == favicon_data
355346

356347

357348
def test_creator_additem(fpath, lipsum_item):

tests/test_libzim_reader.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,17 @@ def test_reader_main_favicon_entries(
318318
assert zim.main_entry.path == "mainPage"
319319

320320
# make sure we have no favicon entry
321-
assert zim.has_favicon_entry is has_favicon_entry
321+
assert zim.has_illustration(48) is has_favicon_entry
322+
if has_favicon_entry:
323+
assert 48 in zim.get_illustration_sizes()
324+
322325
if has_favicon_entry is False:
323-
with pytest.raises(RuntimeError):
324-
assert zim.favicon_entry
326+
with pytest.raises(KeyError):
327+
assert zim.get_illustration_item(48)
325328
else:
326-
assert zim.favicon_entry
329+
assert zim.get_illustration_item()
327330
if new_ns:
328-
assert zim.favicon_entry.path == "-/favicon"
331+
assert zim.get_illustration_item().path == "Illustration_48x48@1"
329332

330333

331334
@pytest.mark.parametrize(
@@ -438,7 +441,7 @@ def test_reader_get_entries(
438441
with pytest.raises(KeyError):
439442
zim.get_entry_by_title("___missing")
440443

441-
# FIXME: unable to re
444+
# FIXME: unable to retrieve entry by title on example.zim
442445
if test_title and filename != "example.zim":
443446
assert zim.has_entry_by_title(test_title)
444447
assert zim.get_entry_by_title(test_title).path == entry.path

0 commit comments

Comments
 (0)