-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdb.i
338 lines (287 loc) · 8.24 KB
/
kdb.i
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* @file
*
* @brief
*
* @copyright BSD License (see doc/LICENSE.md or http://www.libelektra.org)
*/
%module kdb
%include "stl.i"
%include "../common.i"
%feature("autodoc", "3");
/* handle exceptions */
%{
#define KDB_CATCH_EX(namespace, exception) \
catch(const namespace::exception &e) \
{ \
SWIG_Python_Raise(SWIG_NewPointerObj(new namespace::exception(e), \
SWIGTYPE_p_##namespace##__##exception, SWIG_POINTER_OWN), \
#exception, SWIGTYPE_p_##namespace##__##exception); \
SWIG_fail; \
}
%}
%pythoncode {
import warnings
}
%exceptionclass kdb::Exception;
%extend kdb::Exception {
%pythoncode %{
def __str__(self):
return self.what()
%}
}
%include "keyexcept.hpp"
%include "kdbexcept.hpp"
/*
* key.hpp
*/
// exception handling for kdb::Key
%exception {
KDB_CATCH(KEY_EXCEPTIONS)
}
// constructors
%pythonprepend kdb::Key::Key %{
value = None
meta = {}
# check for copy constructor
if len(args) and isinstance(args[0], str):
arg0, args = args[0], args[1:]
flags = 0
args = iter(args)
for arg in args:
if arg == KEY_END:
break
elif arg == KEY_SIZE:
# ignore value
next(args)
elif arg == KEY_VALUE:
value = next(args)
elif arg == KEY_FUNC:
#TODO swig directors?
raise TypeError("Unsupported meta type")
elif arg == KEY_FLAGS:
flags = next(args)
elif arg == KEY_META:
k = next(args)
meta[k] = next(args)
elif isinstance(arg, int):
warnings.warn("Deprecated option in keyNew: {0}".format(arg),
DeprecationWarning)
flags |= arg
else:
warnings.warn("Unknown option in keyNew {0}".format(arg),
RuntimeWarning)
args = [ arg0, flags ]
%}
%pythonappend kdb::Key::Key {
if value:
self.value = value
for k in meta:
self.setMeta(k, meta[k])
}
// properties
// we can't use %attribute here swig won't generate exception code for
// properties. thus we rename and create them using pure python code below
//%attributestring(kdb::Key, std::string, name, getName, setName);
//%attributestring(kdb::Key, std::string, basename, getBaseName, setBaseName);
//%attributestring(kdb::Key, std::string, fullname, getFullName);
%rename("_%s") kdb::Key::getName;
%rename("_%s") kdb::Key::setName;
%rename("_%s") kdb::Key::getBaseName;
%rename("_%s") kdb::Key::setBaseName;
%rename("_%s") kdb::Key::getFullName;
// only accept binary data in binary functions
%typemap(out) std::string kdb::Key::getBinary {
$result = PyBytes_FromStringAndSize($1.data(), $1.size());
}
%typemap(in) (const void *newBinary, size_t dataSize) {
Py_ssize_t len;
if (PyBytes_AsStringAndSize($input, reinterpret_cast<char **>(&$1), &len) == -1)
return NULL;
$2 = len;
}
%typemap(out) void *kdb::Key::getValue {
ssize_t size = arg1->getBinarySize();
$result = PyBytes_FromStringAndSize((const char*)$1, (size > 0) ? size : 0);
}
// add some other useful methods
%extend kdb::Key {
Key(const char *name, int flags = 0) {
return new kdb::Key(name,
KEY_FLAGS, flags,
KEY_END);
}
int __cmp__(const Key *o) {
return ckdb::keyCmp($self->getKey(), o->getKey());
}
kdb::Key *__copy__() {
return new kdb::Key($self->dup());
}
%pythoncode %{
def get(self):
"""returns the keys value"""
if self.isBinary():
return self._getBinary()
return self._getString()
def set(self, value):
"""set the keys value. Can be either string or binary"""
if isinstance(value, bytes):
return self._setBinary(value)
return self._setString(str(value))
def getMeta(self, name = None):
"""returns a metakey given by name. Name can be either string or Key.
If no metakey is found None is returned.
If name is omitted an iterator object is returned.
"""
if name is not None:
meta = self._getMeta(name)
return meta if meta else None
return self.__metaIter()
def setMeta(self, name, value):
"""set a new metakey consisting of name and value"""
if isinstance(value, str):
return self._setMeta(name, value)
raise TypeError("Unsupported value type")
def __metaIter(self):
self._rewindMeta()
meta = self._nextMeta()
while meta:
yield meta
meta = self._nextMeta()
name = property(_kdb.Key__getName, _kdb.Key__setName)
value = property(get, set, None, "Key value")
basename = property(_kdb.Key__getBaseName, _kdb.Key__setBaseName)
fullname = property(_kdb.Key__getFullName)
def __str__(self):
return self.name
%}
};
// define traits needed by SwigPyIterator
%fragment("SwigPyIterator_T");
%traits_swigtype(std::string);
%fragment(SWIG_Traits_frag(std::string));
%extend kdb::Key {
swig::SwigPyIterator* __iter__(PyObject **PYTHON_SELF) {
return swig::make_output_iterator(self->begin(), self->begin(),
self->end(), *PYTHON_SELF);
}
swig::SwigPyIterator* __reversed__(PyObject **PYTHON_SELF) {
return swig::make_output_iterator(self->rbegin(), self->rbegin(),
self->rend(), *PYTHON_SELF);
}
};
%include "key.hpp"
// metadata
%template(_getMeta) kdb::Key::getMeta<const kdb::Key>;
%template(_setMeta) kdb::Key::setMeta<std::string>;
// clear exception handler
%exception;
/*
* keyset.hpp
*/
%pythonprepend kdb::KeySet::KeySet %{
orig = []
if len(args):
orig = args[1:]
args = [ args[0] ]
%}
%pythonappend kdb::KeySet::KeySet %{
for arg in orig:
if arg is KS_END:
break
self.append(arg)
%}
%rename(__len__) kdb::KeySet::size;
%ignore kdb::KeySet::rewind;
%ignore kdb::KeySet::next;
%ignore kdb::KeySet::current;
%rename("_%s") kdb::KeySet::lookup;
%rename("_lookup") kdb::KeySet::at;
%extend kdb::KeySet {
KeySet(size_t alloc) {
return new kdb::KeySet(alloc, KS_END);
}
kdb::KeySet *__copy__() {
return new kdb::KeySet(*$self);
}
kdb::KeySet *__deepcopy__(PyObject *memo) {
(void) PyDict_Check(memo);
ssize_t size = $self->size();
kdb::KeySet *ks = new kdb::KeySet(size, KS_END);
for (cursor_t cursor = 0; cursor < size; ++cursor)
ks->append($self->at(cursor)->dup());
return ks;
}
%pythoncode %{
def lookup(self, name):
"""Lookup a key by name. Name can be either string, Key or indexes.
If index is negative, search starts at the end.
Returns None if no key is found.
"""
key = self._lookup(name)
return key if key else None
def __getitem__(self, key):
"""See lookup(...) for details.
Slices and negative indexes are supported as well.
"""
if isinstance(key, slice):
return [ self[k] for k in range(*key.indices(len(self))) ]
elif isinstance(key, ( int )):
item = self.lookup(key)
if item is None:
raise IndexError("index out of range")
return item
elif isinstance(key, ( str, Key )):
item = self.lookup(key)
if item is None:
raise KeyError(str(key))
return item
raise TypeError("Invalid argument type")
def __contains__(self, item):
"""See lookup(...) for details"""
if isinstance(item, ( str, Key )):
key = self._lookup(item)
return True if key else False
raise TypeError("Invalid argument type")
%}
}
// define traits needed by SwigPyIterator
%fragment("SwigPyIterator_T");
%traits_swigtype(kdb::Key);
%fragment(SWIG_Traits_frag(kdb::Key));
%extend kdb::KeySet {
swig::SwigPyIterator* __iter__(PyObject **PYTHON_SELF) {
return swig::make_output_iterator(self->begin(), self->begin(),
self->end(), *PYTHON_SELF);
}
swig::SwigPyIterator* __reversed__(PyObject **PYTHON_SELF) {
return swig::make_output_iterator(self->rbegin(), self->rbegin(),
self->rend(), *PYTHON_SELF);
}
}
%include "keyset.hpp"
/*
* kdb.hpp
*/
// exception handling for kdb::KDB
%exception {
KDB_CATCH(KDB_EXCEPTIONS)
}
%extend kdb::KDB {
%pythoncode %{
def __enter__(self):
"""Internal method for usage with context managers"""
return self
def __exit__(self, type, value, tb):
"""Internal method for usage with context managers.
Closes the database.
"""
try:
self.close(Key())
except:
pass
%}
}
%include "kdb.hpp"
// clear exception handler
%exception;