@@ -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,13 @@ 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+ attr = getattr (obj, attribute.decode(' UTF-8' ), None )
93+ if not attr:
94+ return False
95+ return True
96+
9097 string string_cy_call_fct(object obj, string method, string * error) with gil:
9198 """ Lookup and execute a pure virtual method on object returning a string"""
9299 try :
@@ -122,15 +129,27 @@ cdef public api:
122129
123130 return NULL
124131
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
132+ zim.IndexData* indexdata_cy_call_fct(object obj, string method, string * error) with gil:
133+ """ Lookup and execute a pure virtual method on object returning a IndexData"""
134+ try :
135+ indexData = call_method(obj, method)
136+ if not indexData:
137+ # indexData is none
138+ return NULL ;
139+ return new zim.IndexDataWrapper(< PyObject* > indexData)
140+ except Exception as e:
141+ error[0 ] = traceback.format_exc().encode(' UTF-8' )
142+
143+ return NULL
144+
145+ bool bool_cy_call_fct(object obj, string method, string * error) with gil:
146+ """ Lookup and execute a pure virtual method on object returning a bool"""
147+ try :
148+ return call_method(obj, method)
149+ except Exception as e:
150+ error[0 ] = traceback.format_exc().encode(' UTF-8' )
151+
152+ return False
134153
135154 uint64_t uint64_cy_call_fct(object obj, string method, string * error) with gil:
136155 """ Lookup and execute a pure virtual method on object returning an uint64_t"""
@@ -141,6 +160,26 @@ cdef public api:
141160
142161 return 0
143162
163+ uint32_t uint32_cy_call_fct(object obj, string method, string * error) with gil:
164+ """ Lookup and execute a pure virtual method on object returning an uint_32"""
165+ try :
166+ return < uint32_t> call_method(obj, method)
167+ except Exception as e:
168+ error[0 ] = traceback.format_exc().encode(' UTF-8' )
169+
170+ return 0
171+
172+ zim.GeoPosition geoposition_cy_call_fct(object obj, string method, string * error) with gil:
173+ """ Lookup and execute a pure virtual method on object returning a GeoPosition"""
174+ try :
175+ geoPosition = call_method(obj, method)
176+ if geoPosition:
177+ return zim.GeoPosition(True , geoPosition[0 ], geoPosition[1 ]);
178+ except Exception as e:
179+ error[0 ] = traceback.format_exc().encode(' UTF-8' )
180+
181+ return zim.GeoPosition(False , 0 , 0 )
182+
144183 map [zim.HintKeys, uint64_t] convertToCppHints(dict hintsDict):
145184 """ C++ Hints from Python dict"""
146185 cdef map [zim.HintKeys, uint64_t] ret;
@@ -439,6 +478,40 @@ class FileProvider(ContentProvider):
439478 yield WritingBlob(res )
440479 res = fh.read(bsize)
441480
481+ class IndexData:
482+ """ IndexData stub to override
483+
484+ Return a subclass of it in Item.get_indexdata()"""
485+ __module__ = writer_module_name
486+
487+ def has_indexdata(self ) -> bool:
488+ """Return true if the IndexData actually contains data"""
489+ return False
490+
491+ def get_title(self ) -> str:
492+ """Title to index. Might be the same as Item.get_title or not"""
493+ raise NotImplementedError("get_title must be implemented.")
494+
495+ def get_content(self ) -> str:
496+ """Content to index. Might be the same as Item.get_title or not"""
497+ raise NotImplementedError("get_content must be implemented.")
498+
499+ def get_keywords(self ) -> str:
500+ """Keywords used to index the item.
501+
502+ Must be a string containing keywords separated by a space"""
503+ raise NotImplementedError("get_keywords must be implemented.")
504+
505+ def get_wordcount(self ) -> int:
506+ """Number of word in content"""
507+ raise NotImplementedError("get_wordcount must be implemented.")
508+
509+ def get_geoposition(self ) -> Optional[Tuple[float , float]]:
510+ """GeoPosition used to index the item.
511+
512+ Must be a tuple (latitude , longitude ) or None"""
513+ return None
514+
442515
443516class BaseWritingItem:
444517 """Item stub to override
@@ -529,6 +602,7 @@ writer_public_objects = [
529602 ContentProvider,
530603 FileProvider,
531604 StringProvider,
605+ IndexData,
532606 pascalize
533607]
534608writer = create_module(writer_module_name, writer_module_doc, writer_public_objects)
0 commit comments