Skip to content

Commit 2cc7556

Browse files
jdcaballerovmgautierfr
authored andcommitted
Cython class blend initial version
1 parent 7c44579 commit 2cc7556

File tree

8 files changed

+481
-666
lines changed

8 files changed

+481
-666
lines changed

pyzim/lib.cxx

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
#include <Python.h>
2+
#include "lib.h"
3+
4+
#include "pyzim_api.h"
5+
6+
#include <iostream>
7+
#include <zim/writer/url.h>
8+
#include <zim/blob.h>
9+
#include <zim/writer/creator.h>
10+
11+
/*
12+
#########################
13+
# ZimArticle #
14+
#########################
15+
*/
16+
17+
ZimArticleWrapper::ZimArticleWrapper(PyObject *obj) : m_obj(obj)
18+
{
19+
if (import_pyzim())
20+
{
21+
std::cerr << "Error executing import_pyzim!\n";
22+
throw std::runtime_error("Error executing import_pyzim");
23+
}
24+
else
25+
{
26+
Py_XINCREF(this->m_obj);
27+
}
28+
}
29+
30+
ZimArticleWrapper::~ZimArticleWrapper()
31+
{
32+
Py_XDECREF(this->m_obj);
33+
}
34+
35+
std::string ZimArticleWrapper::callCythonReturnString(std::string methodName) const
36+
{
37+
if (!this->m_obj)
38+
throw std::runtime_error("Python object not set");
39+
40+
int error;
41+
42+
std::string ret_val = string_cy_call_fct(this->m_obj, methodName, &error);
43+
if (error)
44+
throw std::runtime_error("The pure virtual function " + methodName + " must be override");
45+
46+
return ret_val;
47+
}
48+
49+
zim::Blob ZimArticleWrapper::callCythonReturnBlob(std::string methodName) const
50+
{
51+
if (!this->m_obj)
52+
throw std::runtime_error("Python object not set");
53+
54+
int error;
55+
56+
zim::Blob ret_val = blob_cy_call_fct(this->m_obj, methodName, &error);
57+
if (error)
58+
throw std::runtime_error("The pure virtual function " + methodName + " must be override");
59+
60+
return ret_val;
61+
}
62+
63+
bool ZimArticleWrapper::callCythonReturnBool(std::string methodName) const
64+
{
65+
if (!this->m_obj)
66+
throw std::runtime_error("Python object not set");
67+
68+
int error;
69+
70+
bool ret_val = bool_cy_call_fct(this->m_obj, methodName, &error);
71+
if (error)
72+
throw std::runtime_error("The pure virtual function " + methodName + " must be override");
73+
74+
return ret_val;
75+
}
76+
77+
uint64_t ZimArticleWrapper::callCythonReturnInt(std::string methodName) const
78+
{
79+
if (!this->m_obj)
80+
throw std::runtime_error("Python object not set");
81+
82+
int error;
83+
84+
int ret_val = int_cy_call_fct(this->m_obj, methodName, &error);
85+
if (error)
86+
throw std::runtime_error("The pure virtual function " + methodName + " must be override");
87+
88+
return ret_val;
89+
}
90+
zim::writer::Url
91+
ZimArticleWrapper::getUrl() const
92+
{
93+
94+
std::string url = callCythonReturnString("get_url");
95+
96+
return zim::writer::Url(url.substr(0, 1)[0], url.substr(2, url.length()));
97+
}
98+
99+
std::string
100+
ZimArticleWrapper::getTitle() const
101+
{
102+
return callCythonReturnString("get_title");
103+
}
104+
105+
bool ZimArticleWrapper::isRedirect() const
106+
{
107+
return callCythonReturnBool("is_redirect");
108+
}
109+
110+
std::string
111+
ZimArticleWrapper::getMimeType() const
112+
{
113+
return callCythonReturnString("get_mime_type");
114+
}
115+
116+
std::string
117+
ZimArticleWrapper::getFilename() const
118+
{
119+
return callCythonReturnString("get_filename");
120+
}
121+
122+
bool ZimArticleWrapper::shouldCompress() const
123+
{
124+
return callCythonReturnBool("should_compress");
125+
}
126+
127+
bool ZimArticleWrapper::shouldIndex() const
128+
{
129+
return callCythonReturnBool("should_index");
130+
}
131+
132+
zim::writer::Url
133+
ZimArticleWrapper::getRedirectUrl() const
134+
{
135+
136+
std::string redirectUrl = callCythonReturnString("get_url");
137+
return zim::writer::Url(redirectUrl.substr(0, 1)[0], redirectUrl.substr(2, redirectUrl.length()));
138+
}
139+
140+
// zim::Blob
141+
// ZimArticleWrapper::getData() const
142+
// {
143+
// std::string content = callCythonReturnString("get_data");
144+
// return zim::Blob(&content[0], content.size());
145+
// }
146+
147+
zim::Blob
148+
ZimArticleWrapper::getData() const
149+
{
150+
return callCythonReturnBlob("get_data");
151+
}
152+
153+
zim::size_type
154+
ZimArticleWrapper::getSize() const
155+
{
156+
return this->getData().size();
157+
}
158+
159+
bool ZimArticleWrapper::isLinktarget() const
160+
{
161+
return false;
162+
}
163+
164+
bool ZimArticleWrapper::isDeleted() const
165+
{
166+
return false;
167+
}
168+
169+
std::string ZimArticleWrapper::getNextCategory()
170+
{
171+
return std::string();
172+
}
173+
174+
/*
175+
#########################
176+
# ZimCreator #
177+
#########################
178+
*/
179+
180+
class OverriddenZimCreator : public zim::writer::Creator
181+
{
182+
public:
183+
OverriddenZimCreator(std::string mainPage)
184+
: zim::writer::Creator(true),
185+
mainPage(mainPage) {}
186+
187+
virtual zim::writer::Url getMainUrl()
188+
{
189+
return zim::writer::Url('A', mainPage);
190+
}
191+
192+
std::string mainPage;
193+
};
194+
195+
ZimCreatorWrapper::ZimCreatorWrapper(OverriddenZimCreator *creator) : _creator(creator)
196+
{
197+
}
198+
199+
ZimCreatorWrapper::~ZimCreatorWrapper()
200+
{
201+
delete _creator;
202+
}
203+
204+
ZimCreatorWrapper *
205+
ZimCreatorWrapper::
206+
create(std::string fileName, std::string mainPage, std::string fullTextIndexLanguage, int minChunkSize)
207+
208+
{
209+
bool shouldIndex = !fullTextIndexLanguage.empty();
210+
211+
OverriddenZimCreator *c = new OverriddenZimCreator(mainPage); // TODO: consider when to delete this
212+
c->setIndexing(shouldIndex, fullTextIndexLanguage);
213+
c->setMinChunkSize(minChunkSize);
214+
c->startZimCreation(fileName);
215+
return (new ZimCreatorWrapper(c));
216+
}
217+
218+
void ZimCreatorWrapper::addArticle(std::shared_ptr<ZimArticleWrapper> article)
219+
{
220+
_creator->addArticle(article);
221+
}
222+
223+
void ZimCreatorWrapper::finalise()
224+
{
225+
_creator->finishZimCreation();
226+
delete this;
227+
}

pyzim/lib.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// -*- c++ -*-
2+
#ifndef PYZIM_LIB_H
3+
#define PYZIM_LIB_H 1
4+
5+
struct _object;
6+
typedef _object PyObject;
7+
8+
#include <zim/zim.h>
9+
#include <zim/writer/article.h>
10+
#include <zim/writer/url.h>
11+
#include <zim/blob.h>
12+
13+
#include <string>
14+
15+
class ZimArticleWrapper : public zim::writer::Article
16+
{
17+
public:
18+
PyObject *m_obj;
19+
20+
ZimArticleWrapper(PyObject *obj);
21+
virtual ~ZimArticleWrapper();
22+
23+
virtual zim::writer::Url getUrl() const;
24+
virtual std::string getTitle() const;
25+
virtual bool isRedirect() const;
26+
virtual std::string getMimeType() const;
27+
virtual std::string getFilename() const;
28+
virtual bool shouldCompress() const;
29+
virtual bool shouldIndex() const;
30+
virtual zim::writer::Url getRedirectUrl() const;
31+
virtual zim::Blob getData() const;
32+
virtual zim::size_type getSize() const;
33+
34+
virtual bool isLinktarget() const;
35+
virtual bool isDeleted() const;
36+
virtual std::string getNextCategory();
37+
38+
39+
private:
40+
std::string callCythonReturnString(std::string) const;
41+
zim::Blob callCythonReturnBlob(std::string) const;
42+
bool callCythonReturnBool(std::string) const;
43+
uint64_t callCythonReturnInt(std::string) const;
44+
};
45+
46+
class OverriddenZimCreator;
47+
48+
class ZimCreatorWrapper
49+
{
50+
public:
51+
OverriddenZimCreator *_creator;
52+
ZimCreatorWrapper(OverriddenZimCreator *creator);
53+
~ZimCreatorWrapper();
54+
static ZimCreatorWrapper *create(std::string fileName, std::string mainPage, std::string fullTextIndexLanguage, int minChunkSize);
55+
void addArticle(std::shared_ptr<ZimArticleWrapper> article);
56+
void finalise();
57+
};
58+
59+
#endif // !PYZIM_LIB_H

pyzim/pyzim.pxd

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from libcpp.string cimport string
2+
from libc.stdint cimport uint32_t, uint64_t
3+
from libcpp cimport bool
4+
from libcpp.memory cimport shared_ptr
5+
from libcpp.vector cimport vector
6+
7+
from cpython.ref cimport PyObject
8+
9+
cdef extern from "zim/blob.h" namespace "zim":
10+
cdef cppclass Blob:
11+
Blob()
12+
Blob(const char* data, uint64_t size)
13+
char* data()
14+
char* end()
15+
uint64_t size()
16+
17+
cdef extern from "zim/writer/url.h" namespace "zim::writer":
18+
cdef cppclass Url:
19+
string getLongUrl()
20+
21+
22+
cdef extern from "zim/writer/article.h" namespace "zim::writer":
23+
cdef cppclass Article:
24+
const string getTitle()
25+
26+
27+
cdef extern from "lib.h":
28+
cdef cppclass ZimArticleWrapper(Article):
29+
ZimArticleWrapper(PyObject *obj) except +
30+
const Url getUrl()
31+
const string getTitle()
32+
const bool isRedirect()
33+
const string getMimeType()
34+
const string getFilename()
35+
const bool shouldCompress()
36+
const bool shouldIndex()
37+
const Url getRedirectUrl()
38+
const Blob getData()
39+
40+
cdef cppclass ZimCreatorWrapper:
41+
@staticmethod
42+
ZimCreatorWrapper *create(string fileName, string mainPage, string fullTextIndexLanguage, int minChunkSize)
43+
void addArticle(shared_ptr[ZimArticleWrapper] article)
44+
void finalise()

0 commit comments

Comments
 (0)