1 /* Copyright(C) 2007 Brazil
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <senna/senna.h>
20 /* TODO: use exception */
30 } sennactx_ContextObject;
32 static PyTypeObject sennactx_DbType;
33 static PyTypeObject sennactx_ContextType;
38 sennactx_DbObject_new(PyTypeObject *type, PyObject *args, PyObject *keywds)
40 sennactx_DbObject *self;
41 if ((self = (sennactx_DbObject *)type->tp_alloc(type, 0))) {
44 return (PyObject *)self;
48 sennactx_DbObject_dealloc(sennactx_DbObject *self)
51 Py_BEGIN_ALLOW_THREADS
52 sen_db_close(self->db);
55 self->ob_type->tp_free(self);
59 sennactx_ContextObject_new(PyTypeObject *type, PyObject *args, PyObject *keywds)
61 sennactx_ContextObject *self;
62 if ((self = (sennactx_ContextObject *)type->tp_alloc(type, 0))) {
65 return (PyObject *)self;
69 sennactx_ContextObject_dealloc(sennactx_ContextObject *self)
72 Py_BEGIN_ALLOW_THREADS
73 sen_ctx_close(self->ctx);
76 self->ob_type->tp_free(self);
82 sennactx_DbClass_create(PyTypeObject *type, PyObject *args, PyObject *keywds)
86 sennactx_DbObject *self;
87 static char *kwlist[] = {"path", "flags", "encodings", NULL};
89 if (!PyArg_ParseTupleAndKeywords(args, keywds, "sii", kwlist,
90 &path, &flags, &enc)) {
93 if ((self = (sennactx_DbObject *)sennactx_DbObject_new(type, NULL, NULL))) {
94 Py_BEGIN_ALLOW_THREADS
95 self->db = sen_db_create(path, flags, enc);
98 return (PyObject *)self;
100 sennactx_DbObject_dealloc(self);
106 sennactx_DbClass_open(PyTypeObject *type, PyObject *args, PyObject *keywds)
109 sennactx_DbObject *self;
110 static char *kwlist[] = {"path", NULL};
112 if (!PyArg_ParseTupleAndKeywords(args, keywds, "s", kwlist,
116 if ((self = (sennactx_DbObject *)sennactx_DbObject_new(type, NULL, NULL))) {
117 Py_BEGIN_ALLOW_THREADS
118 self->db = sen_db_open(path);
121 return (PyObject *)self;
123 sennactx_DbObject_dealloc(self);
129 sennactx_ContextClass_connect(PyTypeObject *type, PyObject *args, PyObject *keywds)
133 sennactx_ContextObject *self;
134 static char *kwlist[] = {"host", "port", "flags", NULL};
136 if (!PyArg_ParseTupleAndKeywords(args, keywds, "sii", kwlist,
137 &host, &port, &flags)) {
140 if ((self = (sennactx_ContextObject *)sennactx_ContextObject_new(type, NULL, NULL))) {
141 Py_BEGIN_ALLOW_THREADS
142 self->ctx = sen_ctx_connect(host, port, flags);
145 return (PyObject *)self;
147 sennactx_ContextObject_dealloc(self);
152 /* instance methods */
155 sennactx_Db_close(sennactx_DbObject *self)
159 if (!self->db) { return NULL; }
160 Py_BEGIN_ALLOW_THREADS
161 rc = sen_db_close(self->db);
166 return Py_BuildValue("i", rc);
170 sennactx_Db_ctx_open(sennactx_DbObject *self, PyObject *args, PyObject *keywds)
173 sennactx_ContextObject *ctx;
174 static char *kwlist[] = {"flags", NULL};
176 if (!PyArg_ParseTupleAndKeywords(args, keywds, "i", kwlist,
180 if ((ctx = (sennactx_ContextObject *)sennactx_ContextObject_new(
181 &sennactx_ContextType, NULL, NULL))) {
182 Py_BEGIN_ALLOW_THREADS
183 ctx->ctx = sen_ctx_open(self->db, flags);
186 return (PyObject *)ctx;
188 sennactx_ContextObject_dealloc(ctx);
194 sennactx_Context_load(sennactx_ContextObject *self, PyObject *args, PyObject *keywds)
198 static char *kwlist[] = {"path", NULL};
200 if (!PyArg_ParseTupleAndKeywords(args, keywds, "s", kwlist,
204 if (!self->ctx) { return NULL; }
205 Py_BEGIN_ALLOW_THREADS
206 rc = sen_ctx_load(self->ctx, path);
208 return Py_BuildValue("i", rc);
212 sennactx_Context_send(sennactx_ContextObject *self, PyObject *args, PyObject *keywds)
217 static char *kwlist[] = {"str", "flags", NULL};
219 if (!PyArg_ParseTupleAndKeywords(args, keywds, "s#i", kwlist,
225 if (!self->ctx) { return NULL; }
226 Py_BEGIN_ALLOW_THREADS
227 rc = sen_ctx_send(self->ctx, str, str_len, flags);
229 return Py_BuildValue("i", rc);
233 sennactx_Context_recv(sennactx_ContextObject *self)
238 unsigned int str_len;
240 if (!self->ctx) { return NULL; }
241 Py_BEGIN_ALLOW_THREADS
242 rc = sen_ctx_recv(self->ctx, &str, &str_len, &flags);
244 return Py_BuildValue("(is#i)", rc, str, str_len, flags);
248 sennactx_Context_close(sennactx_ContextObject *self)
252 if (!self->ctx) { return NULL; }
253 Py_BEGIN_ALLOW_THREADS
254 rc = sen_ctx_close(self->ctx);
259 return Py_BuildValue("i", rc);
263 sennactx_Context_info_get(sennactx_ContextObject *self)
268 if (!self->ctx) { return NULL; }
269 rc = sen_ctx_info_get(self->ctx, &info);
270 /* TODO: handling unsigned int properlly */
271 /* TODO: get outbuf */
272 return Py_BuildValue("{s:i,s:i,s:i}",
274 "com_status", info.com_status,
275 "com_info", info.com_info
276 /* "outbuf", info.outbuf */
280 /* methods of classes */
282 static PyMethodDef sennactx_Db_methods[] = {
283 {"create", (PyCFunction)sennactx_DbClass_create,
284 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
285 "Create a Senna DB and Open an instance of it."},
286 {"open", (PyCFunction)sennactx_DbClass_open,
287 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
288 "Open a Senna DB instance."},
289 {"close", (PyCFunction)sennactx_Db_close,
291 "Release Senna DB instance."},
292 {"ctx_open", (PyCFunction)sennactx_Db_ctx_open,
293 METH_VARARGS | METH_KEYWORDS,
294 "Open a local senna context."},
295 {NULL, NULL, 0, NULL}
298 static PyMethodDef sennactx_Context_methods[] = {
299 /* {"open", (PyCFunction)sennactx_ContextClass_open,
300 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
301 "Create a local Senna context."}, */
302 {"connect", (PyCFunction)sennactx_ContextClass_connect,
303 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
304 "Create a remote Senna context."},
305 {"load", (PyCFunction)sennactx_Context_load,
306 METH_VARARGS | METH_KEYWORDS,
307 "Load a file into context."},
308 {"send", (PyCFunction)sennactx_Context_send,
309 METH_VARARGS | METH_KEYWORDS,
310 "Send message to context."},
311 {"recv", (PyCFunction)sennactx_Context_recv,
313 "Receive message from context."},
314 {"close", (PyCFunction)sennactx_Context_close,
316 "Release Senna context."},
317 {"info_get", (PyCFunction)sennactx_Context_info_get,
319 "Get context information."},
320 {NULL, NULL, 0, NULL}
323 static PyMethodDef module_methods[] = {
324 {NULL, NULL, 0, NULL}
329 static PyTypeObject sennactx_DbType = {
330 PyObject_HEAD_INIT(NULL)
332 "sennactx.Db", /* tp_name */
333 sizeof(sennactx_DbObject), /* tp_basicsize */
335 (destructor)sennactx_DbObject_dealloc, /* tp_dealloc */
341 0, /* tp_as_number */
342 0, /* tp_as_sequence */
343 0, /* tp_as_mapping */
349 0, /* tp_as_buffer */
350 Py_TPFLAGS_DEFAULT, /* tp_flags */
351 "Senna Db objects", /* tp_doc */
354 0, /* tp_richcompare */
355 0, /* tp_weaklistoffset */
358 sennactx_Db_methods, /* tp_methods */
363 0, /* tp_descr_get */
364 0, /* tp_descr_set */
365 0, /* tp_dictoffset */
368 sennactx_DbObject_new, /* tp_new */
371 static PyTypeObject sennactx_ContextType = {
372 PyObject_HEAD_INIT(NULL)
374 "sennactx.Context", /* tp_name */
375 sizeof(sennactx_ContextObject), /* tp_basicsize */
377 (destructor)sennactx_ContextObject_dealloc, /* tp_dealloc */
383 0, /* tp_as_number */
384 0, /* tp_as_sequence */
385 0, /* tp_as_mapping */
391 0, /* tp_as_buffer */
392 Py_TPFLAGS_DEFAULT, /* tp_flags */
393 "Senna Context objects", /* tp_doc */
396 0, /* tp_richcompare */
397 0, /* tp_weaklistoffset */
400 sennactx_Context_methods, /* tp_methods */
405 0, /* tp_descr_get */
406 0, /* tp_descr_set */
407 0, /* tp_dictoffset */
410 sennactx_ContextObject_new, /* tp_new */
415 typedef struct _ConstPair {
420 static ConstPair consts[] = {
422 {"RC_SUCCESS", sen_success},
423 {"RC_MEMORY_EXHAUSTED", sen_memory_exhausted},
424 {"RC_INVALID_FORMAT", sen_invalid_format},
425 {"RC_FILE_OPERATION_ERROR", sen_file_operation_error},
426 {"RC_OTHER_ERROR", sen_other_error},
428 {"ENC_DEFAULT", sen_enc_default},
429 {"ENC_NONE", sen_enc_none},
430 {"ENC_EUC_JP", sen_enc_euc_jp},
431 {"ENC_UTF8", sen_enc_utf8},
432 {"ENC_SJIS", sen_enc_sjis},
433 {"ENC_LATIN1", sen_enc_latin1},
434 {"ENC_KOI8R", sen_enc_koi8r},
436 {"CTX_MORE", SEN_CTX_MORE},
437 {"CTX_USEQL", SEN_CTX_USEQL},
438 {"CTX_BATCHMODE", SEN_CTX_BATCHMODE},
443 #ifndef PyMODINIT_FUNC
444 #define PyMODINIT_FUNC void
452 if (!(m = Py_InitModule3("sennactx", module_methods,
453 "Senna context module."))) {
458 /* register classes */
460 if (PyType_Ready(&sennactx_DbType) < 0) { goto exit; }
461 if (PyType_Ready(&sennactx_ContextType) < 0) { goto exit; }
462 Py_INCREF(&sennactx_DbType);
463 PyModule_AddObject(m, "Db", (PyObject *)&sennactx_DbType);
464 Py_INCREF(&sennactx_ContextType);
465 PyModule_AddObject(m, "Context", (PyObject *)&sennactx_ContextType);
467 /* register consts */
469 if (!(dict = PyModule_GetDict(m))) { goto exit; }
471 for (i = 0; consts[i].name; i++) {
473 if (!(v = PyInt_FromLong(consts[i].value))) {
476 PyDict_SetItemString(dict, consts[i].name, v);
479 if (Py_AtExit((void (*)(void))sen_fin)) { goto exit; }
481 if (PyErr_Occurred()) {
482 PyErr_SetString(PyExc_ImportError, "sennactx: init failed");