@@ -40,15 +40,15 @@ import pathlib
4040import sys
4141import traceback
4242from types import ModuleType
43- from typing import Dict, Generator, Iterator, List, Set, Union
43+ from typing import Dict, Generator, Iterator, List, Optional, Set, Tuple , Union
4444from uuid import UUID
4545
4646from cpython.buffer cimport PyBUF_WRITABLE
4747from cpython.ref cimport PyObject
4848
4949from cython.operator import preincrement
5050
51- from libc.stdint cimport uint64_t
51+ from libc.stdint cimport uint32_t, uint64_t
5252from libcpp cimport bool
5353from libcpp.map cimport map
5454from libcpp.memory cimport shared_ptr
@@ -87,6 +87,10 @@ cdef object call_method(object obj, string method):
8787# object to the correct cpp type.
8888# Will be used by cpp side to call python method.
8989cdef public api:
90+ bool obj_has_attribute(object obj, string attribute) with gil:
91+ """ Check if a object has a given attribute"""
92+ return hasattr (obj, attribute.decode(' UTF-8' ))
93+
9094 string string_cy_call_fct(object obj, string method, string * error) with gil:
9195 """ Lookup and execute a pure virtual method on object returning a string"""
9296 try :
@@ -122,25 +126,57 @@ cdef public api:
122126
123127 return NULL
124128
125- # currently have no virtual method returning a bool (was should_index/compress)
126- # bool bool_cy_call_fct(object obj, string method, string *error) with gil:
127- # """Lookup and execute a pure virtual method on object returning a bool"""
128- # try:
129- # func = getattr(obj, method.decode('UTF-8'))
130- # return func()
131- # except Exception as e:
132- # error[0] = traceback.format_exc().encode('UTF-8')
133- # return False
134-
135- uint64_t int_cy_call_fct(object obj, string method, string * error) with gil:
136- """ Lookup and execute a pure virtual method on object returning an int"""
129+ zim.IndexData* indexdata_cy_call_fct(object obj, string method, string * error) with gil:
130+ """ Lookup and execute a pure virtual method on object returning a IndexData"""
131+ try :
132+ indexData = call_method(obj, method)
133+ if not indexData:
134+ # indexData is none
135+ return NULL ;
136+ return new zim.IndexDataWrapper(< PyObject* > indexData)
137+ except Exception as e:
138+ error[0 ] = traceback.format_exc().encode(' UTF-8' )
139+
140+ return NULL
141+
142+ bool bool_cy_call_fct(object obj, string method, string * error) with gil:
143+ """ Lookup and execute a pure virtual method on object returning a bool"""
144+ try :
145+ return call_method(obj, method)
146+ except Exception as e:
147+ error[0 ] = traceback.format_exc().encode(' UTF-8' )
148+
149+ return False
150+
151+ uint64_t uint64_cy_call_fct(object obj, string method, string * error) with gil:
152+ """ Lookup and execute a pure virtual method on object returning an uint64_t"""
137153 try :
138154 return < uint64_t> call_method(obj, method)
139155 except Exception as e:
140156 error[0 ] = traceback.format_exc().encode(' UTF-8' )
141157
142158 return 0
143159
160+ uint32_t uint32_cy_call_fct(object obj, string method, string * error) with gil:
161+ """ Lookup and execute a pure virtual method on object returning an uint_32"""
162+ try :
163+ return < uint32_t> call_method(obj, method)
164+ except Exception as e:
165+ error[0 ] = traceback.format_exc().encode(' UTF-8' )
166+
167+ return 0
168+
169+ zim.GeoPosition geoposition_cy_call_fct(object obj, string method, string * error) with gil:
170+ """ Lookup and execute a pure virtual method on object returning a GeoPosition"""
171+ try :
172+ geoPosition = call_method(obj, method)
173+ if geoPosition:
174+ return zim.GeoPosition(True , geoPosition[0 ], geoPosition[1 ]);
175+ except Exception as e:
176+ error[0 ] = traceback.format_exc().encode(' UTF-8' )
177+
178+ return zim.GeoPosition(False , 0 , 0 )
179+
144180 map [zim.HintKeys, uint64_t] convertToCppHints(dict hintsDict):
145181 """ C++ Hints from Python dict"""
146182 cdef map [zim.HintKeys, uint64_t] ret;
@@ -439,6 +475,40 @@ class FileProvider(ContentProvider):
439475 yield WritingBlob(res )
440476 res = fh.read(bsize)
441477
478+ class IndexData:
479+ """ IndexData stub to override
480+
481+ Return a subclass of it in Item.get_indexdata()"""
482+ __module__ = writer_module_name
483+
484+ def has_indexdata(self ) -> bool:
485+ """Return true if the IndexData actually contains data"""
486+ return False
487+
488+ def get_title(self ) -> str:
489+ """Title to index. Might be the same as Item.get_title or not"""
490+ raise NotImplementedError("get_title must be implemented.")
491+
492+ def get_content(self ) -> str:
493+ """Content to index. Might be the same as Item.get_title or not"""
494+ raise NotImplementedError("get_content must be implemented.")
495+
496+ def get_keywords(self ) -> str:
497+ """Keywords used to index the item.
498+
499+ Must be a string containing keywords separated by a space"""
500+ raise NotImplementedError("get_keywords must be implemented.")
501+
502+ def get_wordcount(self ) -> int:
503+ """Number of word in content"""
504+ raise NotImplementedError("get_wordcount must be implemented.")
505+
506+ def get_geoposition(self ) -> Optional[Tuple[float , float]]:
507+ """GeoPosition used to index the item.
508+
509+ Must be a tuple (latitude , longitude ) or None"""
510+ return None
511+
442512
443513class BaseWritingItem:
444514 """Item stub to override
@@ -529,6 +599,7 @@ writer_public_objects = [
529599 ContentProvider,
530600 FileProvider,
531601 StringProvider,
602+ IndexData,
532603 pascalize
533604]
534605writer = create_module(writer_module_name, writer_module_doc, writer_public_objects)
0 commit comments