Skip to content

Commit 72688e1

Browse files
committed
Add Hints support.
This is a pretty simple implementation. Following commits will correctly test the feature and adapt a bit the API.
1 parent 787c9ed commit 72688e1

File tree

6 files changed

+80
-8
lines changed

6 files changed

+80
-8
lines changed

libzim/lib.cxx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ std::unique_ptr<zim::writer::ContentProvider> WriterItemWrapper::callCythonRetur
129129
return ret_val;
130130
}
131131

132+
zim::writer::Hints WriterItemWrapper::callCythonReturnHints(std::string methodName) const
133+
{
134+
if (!this->m_obj)
135+
throw std::runtime_error("Python object not set");
136+
137+
std::string error;
138+
139+
auto ret_val = hints_cy_call_fct(this->m_obj, methodName, &error);
140+
if (!error.empty())
141+
throw std::runtime_error(error);
142+
143+
return ret_val;
144+
}
145+
132146
std::string
133147
WriterItemWrapper::getPath() const
134148
{
@@ -152,3 +166,8 @@ WriterItemWrapper::getContentProvider() const
152166
{
153167
return callCythonReturnContentProvider("get_contentprovider");
154168
}
169+
170+
zim::writer::Hints WriterItemWrapper::getHints() const
171+
{
172+
return callCythonReturnHints("get_hints");
173+
}

libzim/lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ class WriterItemWrapper : public zim::writer::Item, private ObjWrapper
115115
virtual std::string getTitle() const;
116116
virtual std::string getMimeType() const;
117117
virtual std::unique_ptr<zim::writer::ContentProvider> getContentProvider() const;
118+
virtual zim::writer::Hints getHints() const;
118119

119120
private:
120121
std::unique_ptr<zim::writer::ContentProvider> callCythonReturnContentProvider(std::string) const;
122+
zim::writer::Hints callCythonReturnHints(std::string) const;
121123
};
122124

123125
class ContentProviderWrapper : public zim::writer::ContentProvider, private ObjWrapper

libzim/wrapper.pxd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ from libcpp cimport bool
2424
from libcpp.memory cimport shared_ptr, unique_ptr
2525
from libcpp.string cimport string
2626
from libcpp.vector cimport vector
27+
from libcpp.map cimport map
2728

2829

2930
cdef extern from "zim/zim.h" namespace "zim":
@@ -50,6 +51,9 @@ cdef extern from "zim/blob.h" namespace "zim":
5051
cdef extern from "zim/writer/item.h" namespace "zim::writer":
5152
cdef cppclass WriterItem "zim::writer::Item":
5253
pass
54+
ctypedef enum HintKeys:
55+
COMPRESS
56+
FRONT_ARTICLE
5357

5458
cdef extern from "zim/writer/contentProvider.h" namespace "zim::writer":
5559
cdef cppclass ContentProvider:
@@ -66,7 +70,7 @@ cdef extern from "zim/writer/creator.h" namespace "zim::writer":
6670
void startZimCreation(string filepath) nogil except +;
6771
void addItem(shared_ptr[WriterItem] item) nogil except +
6872
void addMetadata(string name, string content, string mimetype) nogil except +
69-
void addRedirection(string path, string title, string targetpath) nogil except +
73+
void addRedirection(string path, string title, string targetpath, map[HintKeys, uint64_t] hints) nogil except +
7074
void finishZimCreation() nogil except +
7175
void setMainPath(string mainPath)
7276
void addIllustration(unsigned int size, string content)

libzim/wrapper.pyx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ from libcpp.string cimport string
3333
from libcpp cimport bool
3434
from libcpp.set cimport set
3535
from libcpp.memory cimport shared_ptr, make_shared, unique_ptr
36+
from libcpp.map cimport map
3637

3738
import pathlib
3839
import datetime
3940
import traceback
4041

4142

43+
44+
4245
#########################
4346
# Blob #
4447
#########################
@@ -166,6 +169,23 @@ cdef public api:
166169

167170
return 0
168171

172+
map[HintKeys, uint64_t] convertToCppHints(dict hintsDict):
173+
cdef map[HintKeys, uint64_t] ret;
174+
for key, value in hintsDict.items():
175+
ret[key.value] = <uint64_t>value
176+
return ret
177+
178+
map[HintKeys, uint64_t] hints_cy_call_fct(object obj, string method, string* error) with gil:
179+
cdef map[HintKeys, uint64_t] ret;
180+
try:
181+
func = getattr(obj, method.decode('UTF-8'))
182+
hintsDict = func()
183+
return convertToCppHints(func())
184+
except Exception as e:
185+
error[0] = traceback.format_exc().encode('UTF-8')
186+
187+
return ret
188+
169189

170190
class Compression(enum.Enum):
171191
""" Compression algorithms available to create ZIM files """
@@ -174,6 +194,12 @@ class Compression(enum.Enum):
174194
zstd = wrapper.CompressionType.zimcompZstd
175195

176196

197+
class Hint(enum.Enum):
198+
COMPRESS = wrapper.HintKeys.COMPRESS
199+
FRONT_ARTICLE = wrapper.HintKeys.FRONT_ARTICLE
200+
201+
202+
177203
cdef class Creator:
178204
""" Zim Creator
179205
@@ -278,15 +304,16 @@ cdef class Creator:
278304
with nogil:
279305
self.c_creator.addMetadata(_name, _content, _mimetype)
280306

281-
def add_redirection(self, str path, str title, str targetPath):
307+
def add_redirection(self, str path, str title, str targetPath, dict hints):
282308
if not self._started:
283309
raise RuntimeError("ZimCreator not started")
284310

285311
cdef string _path = path.encode('utf8')
286312
cdef string _title = title.encode('utf8')
287313
cdef string _targetPath = targetPath.encode('utf8')
314+
cdef map[HintKeys, uint64_t] _hints = convertToCppHints(hints)
288315
with nogil:
289-
self.c_creator.addRedirection(_path, _title, _targetPath)
316+
self.c_creator.addRedirection(_path, _title, _targetPath, _hints)
290317

291318
def __enter__(self):
292319
cdef string _path = str(self._filename).encode('utf8')

libzim/writer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333

3434
import os
3535
import datetime
36-
from typing import Union
36+
from typing import Union, Dict
3737

3838
from .wrapper import Creator as _Creator, Compression
3939
from .wrapper import WritingBlob as Blob
40+
from .wrapper import Hint
4041

4142
__all__ = [
4243
"Item",
@@ -132,6 +133,9 @@ def get_contentprovider(self) -> ContentProvider:
132133
"""ContentProvider containing the complete content of the item"""
133134
raise NotImplementedError("get_contentprovider must be implemented.")
134135

136+
def get_hints(self) -> Dict[Hint, int]:
137+
raise NotImplementedError("get_hint must be implemenent")
138+
135139
def __repr__(self) -> str:
136140
return (
137141
f"{self.__class__.__name__}(path={self.get_path()}, "

tests/test_libzim_creator.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import datetime
77
import itertools
88
import subprocess
9-
9+
from typing import Dict
1010

1111
import pytest
1212

@@ -18,6 +18,7 @@
1818
FileProvider,
1919
StringProvider,
2020
Blob,
21+
Hint,
2122
)
2223
from libzim.reader import Archive
2324

@@ -45,6 +46,9 @@ def get_contentprovider(self) -> libzim.writer.ContentProvider:
4546
return FileProvider(filepath=self.filepath)
4647
return StringProvider(content=getattr(self, "content", ""))
4748

49+
def get_hints(self) -> Dict[Hint, int]:
50+
return {Hint.FRONT_ARTICLE: True}
51+
4852

4953
@pytest.fixture(scope="function")
5054
def fpath(tmpdir):
@@ -423,13 +427,13 @@ def test_creator_redirection(fpath, lipsum_item):
423427
# ensure we can't add if not started
424428
c = Creator(fpath)
425429
with pytest.raises(RuntimeError, match="not started"):
426-
c.add_redirection("home", "hello", HOME_PATH)
430+
c.add_redirection("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
427431
del c
428432

429433
with Creator(fpath) as c:
430434
c.add_item(lipsum_item)
431-
c.add_redirection("home", "hello", HOME_PATH)
432-
c.add_redirection("accueil", "bonjour", HOME_PATH)
435+
c.add_redirection("home", "hello", HOME_PATH, {Hint.FRONT_ARTICLE: True})
436+
c.add_redirection("accueil", "bonjour", HOME_PATH, {Hint.FRONT_ARTICLE: True})
433437

434438
zim = Archive(fpath)
435439
assert zim.entry_count == 3
@@ -524,6 +528,9 @@ def get_mimetype(self):
524528
def get_contentprovider(self):
525529
return ""
526530

531+
def get_hints(self):
532+
return {}
533+
527534
with Creator(fpath) as c:
528535
with pytest.raises(RuntimeError, match="ContentProvider is None"):
529536
c.add_item(AnItem())
@@ -540,6 +547,9 @@ def get_title(self):
540547
def get_mimetype(self):
541548
return ""
542549

550+
def get_hints(self):
551+
return {}
552+
543553
with Creator(fpath) as c:
544554
with pytest.raises(RuntimeError, match="has no attribute"):
545555
c.add_item(AnItem())
@@ -569,6 +579,9 @@ def get_title(self):
569579
def get_mimetype(self):
570580
return ""
571581

582+
def get_hints(self):
583+
return {}
584+
572585
def get_contentprovider(self):
573586
return AContentProvider()
574587

@@ -599,6 +612,9 @@ def get_title(self):
599612
def get_mimetype(self):
600613
return ""
601614

615+
def get_hints(self):
616+
return {}
617+
602618
def get_contentprovider(self):
603619
return AContentProvider()
604620

0 commit comments

Comments
 (0)