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+ }
0 commit comments