Skip to content

Commit 7f0be5f

Browse files
committed
Reorganize code in wrapper.pyx
No code change.
1 parent c019bd7 commit 7f0be5f

File tree

1 file changed

+80
-83
lines changed

1 file changed

+80
-83
lines changed

libzim/wrapper.pyx

Lines changed: 80 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -39,77 +39,9 @@ import traceback
3939

4040
pybool = type(True)
4141

42-
#########################
43-
# Blob #
44-
#########################
45-
46-
cdef class WritingBlob:
47-
cdef zim.Blob c_blob
48-
cdef bytes ref_content
49-
50-
def __cinit__(self, content):
51-
if isinstance(content, str):
52-
self.ref_content = content.encode('UTF-8')
53-
else:
54-
self.ref_content = content
55-
self.c_blob = move(zim.Blob(<char *> self.ref_content, len(self.ref_content)))
56-
57-
def size(self):
58-
return self.c_blob.size()
59-
60-
cdef Py_ssize_t itemsize = 1
61-
62-
cdef class ReadingBlob:
63-
cdef zim.Blob c_blob
64-
cdef Py_ssize_t size
65-
cdef int view_count
66-
67-
# Factory functions - Currently Cython can't use classmethods
68-
@staticmethod
69-
cdef from_blob(zim.Blob blob):
70-
""" Creates a python Blob from a C++ Blob (zim::) -> Blob
71-
72-
Parameters
73-
----------
74-
blob : Blob
75-
A C++ Entry
76-
Returns
77-
------
78-
Blob
79-
Casted blob """
80-
cdef ReadingBlob rblob = ReadingBlob()
81-
rblob.c_blob = move(blob)
82-
rblob.size = rblob.c_blob.size()
83-
rblob.view_count = 0
84-
return rblob
85-
86-
def __dealloc__(self):
87-
if self.view_count:
88-
raise RuntimeError("Blob has views")
89-
90-
def __getbuffer__(self, Py_buffer *buffer, int flags):
91-
if flags&PyBUF_WRITABLE:
92-
raise BufferError("Cannot create writable memoryview on readonly data")
93-
buffer.obj = self
94-
buffer.buf = <void*>self.c_blob.data()
95-
buffer.len = self.size
96-
buffer.readonly = 1
97-
buffer.format = 'c'
98-
buffer.internal = NULL # see References
99-
buffer.itemsize = itemsize
100-
buffer.ndim = 1
101-
buffer.shape = &self.size
102-
buffer.strides = &itemsize
103-
buffer.suboffsets = NULL # for pointer arrays only
104-
105-
self.view_count += 1
106-
107-
def __releasebuffer__(self, Py_buffer *buffer):
108-
self.view_count -= 1
109-
110-
111-
#------- pure virtual methods --------
112-
42+
###############################################################################
43+
#  Public API to be called from C++ side #
44+
###############################################################################
11345

11446
# This call a python method and return a python object.
11547
cdef object call_method(object obj, string method):
@@ -191,6 +123,26 @@ cdef public api:
191123
return ret
192124

193125

126+
###############################################################################
127+
#  Creator module #
128+
###############################################################################
129+
130+
cdef class WritingBlob:
131+
cdef zim.Blob c_blob
132+
cdef bytes ref_content
133+
134+
def __cinit__(self, content):
135+
if isinstance(content, str):
136+
self.ref_content = content.encode('UTF-8')
137+
else:
138+
self.ref_content = content
139+
self.c_blob = move(zim.Blob(<char *> self.ref_content, len(self.ref_content)))
140+
141+
def size(self):
142+
return self.c_blob.size()
143+
144+
145+
194146
class Compression(enum.Enum):
195147
""" Compression algorithms available to create ZIM files """
196148
none = zim.CompressionType.zimcompNone
@@ -336,9 +288,60 @@ cdef class Creator:
336288
def filename(self):
337289
return self._filename
338290

339-
########################
340-
# Entry #
341-
########################
291+
###############################################################################
292+
#  Reader module #
293+
###############################################################################
294+
295+
cdef Py_ssize_t itemsize = 1
296+
297+
cdef class ReadingBlob:
298+
cdef zim.Blob c_blob
299+
cdef Py_ssize_t size
300+
cdef int view_count
301+
302+
# Factory functions - Currently Cython can't use classmethods
303+
@staticmethod
304+
cdef from_blob(zim.Blob blob):
305+
""" Creates a python Blob from a C++ Blob (zim::) -> Blob
306+
307+
Parameters
308+
----------
309+
blob : Blob
310+
A C++ Entry
311+
Returns
312+
------
313+
Blob
314+
Casted blob """
315+
cdef ReadingBlob rblob = ReadingBlob()
316+
rblob.c_blob = move(blob)
317+
rblob.size = rblob.c_blob.size()
318+
rblob.view_count = 0
319+
return rblob
320+
321+
def __dealloc__(self):
322+
if self.view_count:
323+
raise RuntimeError("Blob has views")
324+
325+
def __getbuffer__(self, Py_buffer *buffer, int flags):
326+
if flags&PyBUF_WRITABLE:
327+
raise BufferError("Cannot create writable memoryview on readonly data")
328+
buffer.obj = self
329+
buffer.buf = <void*>self.c_blob.data()
330+
buffer.len = self.size
331+
buffer.readonly = 1
332+
buffer.format = 'c'
333+
buffer.internal = NULL # see References
334+
buffer.itemsize = itemsize
335+
buffer.ndim = 1
336+
buffer.shape = &self.size
337+
buffer.strides = &itemsize
338+
buffer.suboffsets = NULL # for pointer arrays only
339+
340+
self.view_count += 1
341+
342+
def __releasebuffer__(self, Py_buffer *buffer):
343+
self.view_count -= 1
344+
342345

343346
cdef class Entry:
344347
""" Entry in a Zim archive
@@ -453,12 +456,6 @@ cdef class Item:
453456
return f"{self.__class__.__name__}(url={self.path}, title={self.title})"
454457

455458

456-
457-
458-
#########################
459-
# Archive #
460-
#########################
461-
462459
cdef class Archive:
463460
""" Zim Archive Reader
464461
@@ -648,9 +645,9 @@ cdef class Archive:
648645
return f"{self.__class__.__name__}(filename={self.filename})"
649646

650647

651-
#########################
652-
#  Searcher #
653-
#########################
648+
###############################################################################
649+
#  Search module #
650+
###############################################################################
654651

655652
cdef class Query:
656653
cdef zim.Query c_query

0 commit comments

Comments
 (0)