-
Notifications
You must be signed in to change notification settings - Fork 1
/
_pyzim.pyx
103 lines (72 loc) · 2.63 KB
/
_pyzim.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# cython: embedsignature=True
from cpython.ref cimport PyObject
cimport cpython.ref as cpy_ref
from libcpp cimport bool
from libcpp.string cimport string
cdef extern from "cxxtools/log.h":
cdef void log_init() except +
cdef extern from "pyas.h":
cdef cppclass PyArticle:
PyArticle(char namespace, string url, string title, string aid, string redirectAid, string mimetype)
ctypedef PyArticle* (*GetNextArticle)(PyObject* pyObj)
ctypedef string (*GetData)(PyObject* pyObj, string aid)
cdef cppclass PyArticleSource:
PyArticleSource(PyObject*, GetNextArticle, GetData)
string mainPage
cdef void create(string& fname, PyArticleSource* src) except +
def init_log():
log_init()
class Article(object):
def __init__(self, title, namespace='A', url=None, aid=None, redirect_aid=None, mimetype='text/html'):
assert title, 'title is requried'
self.title = title
self.aid = aid or title
self.url = url or title
self.namespace = namespace
self.mimetype = mimetype
self.redirect_aid = redirect_aid or ''
cdef PyArticle* cy_get_next_article(PyObject* ptr):
cdef ArticleSource src = <ArticleSource>(ptr)
art = src.get_next_article()
if art is None:
return NULL
cdef PyArticle* pyart = new PyArticle(ord(art.namespace[0]), art.url, art.title, art.aid, art.redirect_aid, art.mimetype)
return pyart
cdef string cy_get_data(PyObject* ptr, string aid):
cdef ArticleSource src = <ArticleSource>(ptr)
data = src.get_data(aid.c_str())
return data
cdef class ArticleSource:
cdef PyArticleSource* _ptr
def __cinit__(self):
self._ptr = new PyArticleSource(<cpy_ref.PyObject*>self, cy_get_next_article, cy_get_data)
self.it = None
def get_next_article(self):
return None
def get_data(self, aid):
return 'ArticleSource.get_data() needs to be implemented!'
def __dealloc__(self):
del self._ptr
def create(self, fname):
create(fname, self._ptr)
property mainPage:
def __get__(self):
return self._ptr.mainPage
def __set__(self, val):
self._ptr.mainPage = val
cdef extern from "zim/file.h":
cdef cppclass _zimfile "zim::File":
_zimfile(char *) except +
bool verify() except +
string getChecksum() except +
cdef class zimfile(object):
cdef _zimfile *_ptr
def __cinit__(self, fn):
self._ptr = new _zimfile(fn)
def __dealloc__(self):
del self._ptr
def verify(self):
return self._ptr.verify()
@property
def checksum(self):
return self._ptr.getChecksum()