summaryrefslogtreecommitdiff
path: root/ethumb/ethumb.client.pyx
blob: d1a336ff046cd98b26b4099a727dc104f467cac6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Copyright (C) 2009 by ProFUSION embedded systems
#
# This file is part of Python-Ethumb.
#
# Python-Ethumb is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# Python-Ethumb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this Python-Ethumb.  If not, see <http://www.gnu.org/licenses/>.

cimport ethumb.python as python
import traceback

__extra_epydoc_fields__ = (
    ("parm", "Parameter", "Parameters"), # epydoc don't support pyrex properly
    )

def shutdown():
    ethumb_client_shutdown()

def init():
    return ethumb_client_init()

cdef void _connect_cb(Ethumb_Client *client, Eina_Bool success, void *data) with gil:
    obj = <object>data
    (self, func, args, kargs) = obj
    if success:
        s = True
    else:
        s = False
    try:
        func(self, s, *args, **kargs)
    except Exception, e:
        traceback.print_exc()

cdef void _free_connect_cb_data(void *data) with gil:
    obj = <object>data
    python.Py_DECREF(obj)

cdef void _generated_cb(int id, char *file, char *key, char *thumb_path, char *thumb_key, Eina_Bool success, void *data) with gil:
    obj = <object>data
    (self, func, args, kargs) = obj
    f = str_from_c(file)
    k = str_from_c(key)
    tp = str_from_c(thumb_path)
    tk = str_from_c(thumb_key)
    s = success != 0
    if success:
        s = True
    else:
        s = False
    try:
        func(id, f, k, tp, tk, s, *args, **kargs)
    except Exception, e:
        traceback.print_exc()

cdef void _generated_cb_free_data(void *data) with gil:
    obj = <object>data
    python.Py_DECREF(obj)

cdef char *str_to_c(object s):
    cdef char *mystr
    if s is None:
        mystr = NULL
    else:
        mystr = s
    return mystr

cdef object str_from_c(char *mystr):
    if mystr != NULL:
        return mystr

cdef class Client:
    """Client for Ethumbd server."""
    def __init__(self, func, *args, **kargs):
        """Ethumb Client constructor."""
        if not callable(func):
            raise TypeError("Parameter 'func' must be callable")
        if self.obj == NULL:
            targs = (self, func, args, kargs)
            python.Py_INCREF(targs)
            self.obj = ethumb_client_connect(
                _connect_cb, <void*>targs, _free_connect_cb_data)

    # FDO
    def fdo_set(self, int s):
        ethumb_client_fdo_set(self.obj, s)

    # Size
    def size_set(self, int w, int h):
        ethumb_client_size_set(self.obj, w, h)

    def size_get(self):
        cdef int w, h
        ethumb_client_size_get(self.obj, &w, &h)
        return (w, h)

    property size:
        def __set__(self, value):
            cdef int w, h
            w, h = value
            self.size_set(w, h)

        def __get__(self):
            return self.size_get()

    # Format
    def format_set(self, int f):
        ethumb_client_format_set(self.obj, f)

    def format_get(self):
        return ethumb_client_format_get(self.obj)

    property format:
        def __set__(self, value):
            self.format_set(value)

        def __get__(self):
            return self.format_get()

    # Aspect
    def aspect_set(self, int a):
        ethumb_client_aspect_set(self.obj, a)

    def aspect_get(self):
        return ethumb_client_aspect_get(self.obj)

    property aspect:
        def __set__(self, value):
            self.aspect_set(value)

        def __get__(self):
            return self.aspect_get()

    # Crop
    def crop_set(self, float x, float y):
        ethumb_client_crop_align_set(self.obj, x, y)

    def crop_get(self):
        cdef float x, y
        ethumb_client_crop_align_get(self.obj, &x, &y)
        return (x, y)

    property crop:
        def __set__(self, value):
            cdef float x, y
            x, y = value
            self.crop_set(x, y)

        def __get__(self):
            return self.crop_get()

    # Quality
    def quality_set(self, int quality):
        ethumb_client_quality_set(self.obj, quality)

    def quality_get(self):
        return ethumb_client_quality_get(self.obj)

    property quality:
        def __set__(self, value):
            self.quality_set(value)

        def __get__(self):
            return self.quality_get()

    # Compress
    def compress_set(self, int compress):
        ethumb_client_compress_set(self.obj, compress)

    def compress_get(self):
        return ethumb_client_compress_get(self.obj)

    property compress:
        def __set__(self, value):
            self.compress_set(value)

        def __get__(self):
            return self.compress_get()

    # Directory
    def directory_set(self, path):
        ethumb_client_dir_path_set(self.obj, str_to_c(path))

    def directory_get(self):
        return str_from_c(ethumb_client_dir_path_get(self.obj))

    property directory:
        def __set__(self, value):
            self.directory_set(value)

        def __get__(self):
            return self.directory_get()

    # Category
    def category_set(self, category):
        ethumb_client_category_set(self.obj, str_to_c(category))

    def category_get(self):
        return str_from_c(ethumb_client_category_get(self.obj))

    property category:
        def __set__(self, value):
            self.category_set(value)

        def __get__(self):
            return self.category_get()

    # frame
    def frame_set(self, file, group, swallow):
        cdef char *f, *g, *s
        f = str_to_c(file)
        g = str_to_c(group)
        s = str_to_c(swallow)
        return ethumb_client_frame_set(self.obj, f, g, s)

    # file
    def file_set(self, path, key=None):
        cdef char *p, *k
        p = str_to_c(path)
        k = str_to_c(key)
        ethumb_client_file_set(self.obj, p, k)

    def file_get(self):
        cdef char *p, *k
        ethumb_client_file_get(self.obj, &p, &k)
        return (str_from_c(p), str_from_c(k))

    property file:
        def __set__(self, value):
            p, k = value
            self.file_set(p, k)

        def __get__(self):
            return self.file_get()

    def file_free(self):
        ethumb_client_file_free(self.obj)

    # thumbnail
    def thumb_set(self, path, key=None):
        cdef char *p, *k
        p = str_to_c(path)
        k = str_to_c(key)
        ethumb_client_thumb_path_set(self.obj, p, k)

    def thumb_get(self):
        cdef char *p, *k
        ethumb_client_thumb_path_get(self.obj, &p, &k)
        return (str_from_c(p), str_from_c(k))

    property thumb_path:
        def __set__(self, value):
            p, k = value
            self.thumb_set(p, k)
        def __get__(self):
            return self.thumb_get()

    # video_time
    def video_time_set(self, float time):
        ethumb_client_video_time_set(self.obj, time)

    def video_start_set(self, float start):
        ethumb_client_video_start_set(self.obj, start)

    def video_interval_set(self, float interval):
        ethumb_client_video_interval_set(self.obj, interval)

    def video_ntimes_set(self, int ntimes):
        ethumb_client_video_ntimes_set(self.obj, ntimes)

    def video_fps_set(self, int fps):
        ethumb_client_video_fps_set(self.obj, fps)

    # document_page
    def document_page_set(self, int page):
        ethumb_client_document_page_set(self.obj, page)

    # thumbnail generation
    def exists(self):
        return ethumb_client_thumb_exists(self.obj)

    def generate(self, func, *args, **kargs):
        targs = (self, func, args, kargs)
        python.Py_INCREF(targs)
        r = ethumb_client_generate(self.obj, _generated_cb, <void*>targs,
                                   _generated_cb_free_data)
        return r

    def cancel(self, int id):
        ethumb_client_queue_remove(self.obj, id, NULL, NULL)

    def clear(self):
        ethumb_client_queue_clear(self.obj)

    def __dealloc__(self):
        ethumb_client_disconnect(self.obj)

init()