Skip to content

Commit 3a8df6a

Browse files
mgautierfrrenaud gaudin
authored andcommitted
The CompressionType is now a scoped enum Compression.
Cython stable don't support scoped enum. (Next version will). For now we have to use a workaround. We declare a `zim::Compression` enum but we never use it in python. We convert from the python value (to int) using cython and to c++ `zim::Compression` (from int) using a small helper function.
1 parent 201b5e2 commit 3a8df6a

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

libzim/libwrapper.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,18 @@ zim::writer::Hints WriterItemWrapper::getHints() const
165165
{
166166
return callMethodOnObj<zim::writer::Hints>(m_obj, "get_hints");
167167
}
168+
169+
zim::Compression comp_from_int(int compValue)
170+
{
171+
switch(compValue) {
172+
case 0:
173+
return zim::Compression::None;
174+
case 1:
175+
return zim::Compression::Lzma;
176+
case 2:
177+
return zim::Compression::Zstd;
178+
default:
179+
// Should we raise a error ?
180+
return zim::Compression::None;
181+
}
182+
}

libzim/libwrapper.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,13 @@ class ContentProviderWrapper : public zim::writer::ContentProvider, private ObjW
315315
zim::Blob feed() override;
316316
};
317317

318+
319+
// Small helpers
320+
321+
// The current stable cython version (0.29.24) doesn't support scoped enum (next version >30 will be).
322+
// The cython generated __Pyx_PyInt_As_enum__zim_3a__3a_Compression(PyOobject*)
323+
// try to do some strange bit shifting of `zim::Compression` which doesn't compile.
324+
// Let's provide our own function for this
325+
zim::Compression comp_from_int(int compValue);
326+
318327
#endif // LIBZIM_LIBWRAPPER_H

libzim/libzim.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ cdef class WritingBlob:
171171
class Compression(enum.Enum):
172172
""" Compression algorithms available to create ZIM files """
173173
__module__ = writer_module_name
174-
none = zim.CompressionType.zimcompNone
175-
lzma = zim.CompressionType.zimcompLzma
176-
zstd = zim.CompressionType.zimcompZstd
174+
# We don't care of the exact value. The function comp_from_int will do the right
175+
# conversion to zim::Compression
176+
none = 0
177+
lzma = 1
178+
zstd = 2
177179

178180

179181
class Hint(enum.Enum):
@@ -223,10 +225,10 @@ cdef class _Creator:
223225
self.c_creator.configVerbose(verbose)
224226
return self
225227

226-
def config_compression(self, comptype: Compression) -> Creator:
228+
def config_compression(self, compression: Compression) -> Creator:
227229
if self._started:
228230
raise RuntimeError("ZimCreator started")
229-
self.c_creator.configCompression(comptype.value)
231+
self.c_creator.configCompression(zim.comp_from_int(compression.value))
230232
return self
231233

232234
def config_clustersize(self, int size) -> Creator:

libzim/zim.pxd

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ cdef extern from "zim/zim.h" namespace "zim":
3131
ctypedef uint64_t size_type
3232
ctypedef uint64_t offset_type
3333
ctypedef uint32_t entry_index_type
34-
ctypedef enum CompressionType:
35-
zimcompNone
36-
zimcompZip
37-
zimcompBzip2
38-
zimcompLzma
39-
zimcompZstd
34+
cdef enum Compression:
35+
# We need to declare something here to be syntaxically correct
36+
# but we don't use those values (even if they are valid).
37+
None "zim::Compression::None"
38+
Lzma "zim::Compression::Lzma"
39+
Zstd "zim::Compression::Zstd"
4040

4141

4242
cdef extern from "zim/writer/item.h" namespace "zim::writer":
@@ -54,7 +54,7 @@ cdef extern from "zim/writer/contentProvider.h" namespace "zim::writer":
5454
cdef extern from "zim/writer/creator.h" namespace "zim::writer":
5555
cdef cppclass ZimCreator "zim::writer::Creator":
5656
void configVerbose(bint verbose)
57-
void configCompression(CompressionType comptype)
57+
void configCompression(Compression compression)
5858
void configClusterSize(int size)
5959
void configIndexing(bint indexing, string language)
6060
void configNbWorkers(int nbWorkers)
@@ -93,6 +93,8 @@ cdef extern from "libwrapper.h":
9393
cdef cppclass WriterItemWrapper:
9494
WriterItemWrapper(PyObject* obj) except +
9595

96+
Compression comp_from_int(int)
97+
9698

9799
# Import the cpp wrappers.
98100
cdef extern from "libwrapper.h" namespace "wrapper":

0 commit comments

Comments
 (0)