Skip to content

Commit f0bcad5

Browse files
jdcaballerovmgautierfr
authored andcommitted
Remove duplicate code in public api
1 parent 125fbfa commit f0bcad5

File tree

1 file changed

+27
-44
lines changed

1 file changed

+27
-44
lines changed

libzim/libzim.pyx

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -88,62 +88,45 @@ cdef class ZimArticle:
8888
raise NotImplementedError
8989

9090

91+
#------ Helper for pure virtual methods --------
92+
93+
cdef get_article_method_from_object_ptr(void *ptr, string method, int *error):
94+
cdef ZimArticle art = <ZimArticle>(ptr)
95+
try:
96+
func = getattr(art, method.decode('UTF-8'))
97+
except AttributeError:
98+
error[0] = 1
99+
raise
100+
else:
101+
error[0] = 0
102+
return func
91103

92104
#------- ZimArticle pure virtual methods --------
93-
cdef public api:
105+
106+
cdef public api:
94107
string string_cy_call_fct(void *ptr, string method, int *error):
95108
"""Lookup and execute a pure virtual method on ZimArticle returning a string"""
96-
cdef ZimArticle art = <ZimArticle>(ptr)
97-
try:
98-
func = getattr(art, method.decode('UTF-8'))
99-
except AttributeError:
100-
error[0] = 1
101-
raise
102-
else:
103-
error[0] = 0
104-
value = func()
105-
return value.encode('UTF-8')
106-
109+
func = get_article_method_from_object_ptr(ptr, method, error)
110+
ret_str = func()
111+
return ret_str.encode('UTF-8')
112+
107113
Blob blob_cy_call_fct(void *ptr, string method, int *error):
108114
"""Lookup and execute a pure virtual method on ZimArticle returning a Blob"""
109-
cdef ZimArticle art = <ZimArticle>(ptr)
110115
cdef ZimBlob blob
111-
try:
112-
func = getattr(art, method.decode('UTF-8'))
113-
except AttributeError:
114-
error[0] = 1
115-
raise
116-
else:
117-
error[0] = 0
118-
blob = func()
119-
return dereference(blob.c_blob)
120-
116+
117+
func = get_article_method_from_object_ptr(ptr, method, error)
118+
blob = func()
119+
return dereference(blob.c_blob)
120+
121121
bool bool_cy_call_fct(void *ptr, string method, int *error):
122122
"""Lookup and execute a pure virtual method on ZimArticle returning a bool"""
123-
124-
cdef ZimArticle art = <ZimArticle>(ptr)
125-
try:
126-
func = getattr(art, method.decode('UTF-8'))
127-
except AttributeError:
128-
error[0] = 1
129-
raise
130-
else:
131-
error[0] = 0
132-
return func()
123+
func = get_article_method_from_object_ptr(ptr, method, error)
124+
return func()
133125

134126
uint64_t int_cy_call_fct(void *ptr, string method, int *error):
135127
"""Lookup and execute a pure virtual method on ZimArticle returning an int"""
136-
137-
cdef ZimArticle art = <ZimArticle>(ptr)
138-
try:
139-
func = getattr(art, method.decode('UTF-8'))
140-
except AttributeError:
141-
error[0] = 1
142-
raise
143-
else:
144-
error[0] = 0
145-
return <uint64_t> func()
146-
128+
func = get_article_method_from_object_ptr(ptr, method, error)
129+
return <uint64_t> func()
147130

148131
#########################
149132
# ZimCreator #

0 commit comments

Comments
 (0)