1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-10 01:18:15 +03:00

Add simple manually written replacement for the tdb module.

This commit is contained in:
Jelmer Vernooij 2008-12-18 18:57:21 +00:00
parent 7f148a2cf8
commit 2a61fd41e9
6 changed files with 453 additions and 4990 deletions

449
lib/tdb/pytdb.c Normal file
View File

@ -0,0 +1,449 @@
/*
Unix SMB/CIFS implementation.
Swig interface to tdb.
Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
** NOTE! The following LGPL license applies to the tdb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library 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 3 of the License, or (at your option) any later version.
This library 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 library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#ifdef HAVE_FSTAT
#undef HAVE_FSTAT
#endif
/* Include tdb headers */
#include <stdint.h>
#include <signal.h>
#include <tdb.h>
#include <fcntl.h>
typedef struct {
PyObject_HEAD
TDB_CONTEXT *ctx;
} PyTdbObject;
PyAPI_DATA(PyTypeObject) PyTdb;
static void PyErr_SetTDBError(TDB_CONTEXT *tdb)
{
PyErr_SetObject(PyExc_RuntimeError,
Py_BuildValue("(i,s)", tdb_error(tdb), tdb_errorstr(tdb)));
}
static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
{
TDB_DATA ret;
ret.dptr = (unsigned char *)PyString_AsString(data);
ret.dsize = PyString_Size(data);
return ret;
}
static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
{
if (data.dptr == NULL && data.dsize == 0) {
return Py_None;
} else {
PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr,
data.dsize);
free(data.dptr);
return ret;
}
}
#define PyErr_TDB_ERROR_IS_ERR_RAISE(ret, tdb) \
if (ret != 0) { \
PyErr_SetTDBError(tdb); \
return NULL; \
}
static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
char *name;
int hash_size = 0, tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600;
TDB_CONTEXT *ctx;
PyTdbObject *ret;
const char *kwnames[] = { "name", "hash_size", "tdb_flags", "flags", "mode", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiii", (char **)kwnames, &name, &hash_size, &tdb_flags, &flags, &mode))
return NULL;
ctx = tdb_open(name, hash_size, tdb_flags, flags, mode);
if (ctx == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
ret = PyObject_New(PyTdbObject, &PyTdb);
ret->ctx = ctx;
return (PyObject *)ret;
}
static PyObject *obj_transaction_cancel(PyTdbObject *self)
{
int ret = tdb_transaction_cancel(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_transaction_commit(PyTdbObject *self)
{
int ret = tdb_transaction_commit(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_transaction_recover(PyTdbObject *self)
{
int ret = tdb_transaction_recover(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_transaction_start(PyTdbObject *self)
{
int ret = tdb_transaction_start(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_reopen(PyTdbObject *self)
{
int ret = tdb_reopen(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_lockall(PyTdbObject *self)
{
int ret = tdb_lockall(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_unlockall(PyTdbObject *self)
{
int ret = tdb_unlockall(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_lockall_read(PyTdbObject *self)
{
int ret = tdb_lockall_read(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_unlockall_read(PyTdbObject *self)
{
int ret = tdb_unlockall_read(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_close(PyTdbObject *self)
{
int ret = tdb_close(self->ctx);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_get(PyTdbObject *self, PyObject *args)
{
TDB_DATA key;
PyObject *py_key;
if (!PyArg_ParseTuple(args, "O", &py_key))
return NULL;
key = PyString_AsTDB_DATA(py_key);
return PyString_FromTDB_DATA(tdb_fetch(self->ctx, key));
}
static PyObject *obj_append(PyTdbObject *self, PyObject *args)
{
TDB_DATA key, data;
PyObject *py_key, *py_data;
int ret;
if (!PyArg_ParseTuple(args, "OO", &py_key, &py_data))
return NULL;
key = PyString_AsTDB_DATA(py_key);
data = PyString_AsTDB_DATA(py_data);
ret = tdb_append(self->ctx, key, data);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_firstkey(PyTdbObject *self)
{
return PyString_FromTDB_DATA(tdb_firstkey(self->ctx));
}
static PyObject *obj_nextkey(PyTdbObject *self, PyObject *args)
{
TDB_DATA key;
PyObject *py_key;
if (!PyArg_ParseTuple(args, "O", &py_key))
return NULL;
key = PyString_AsTDB_DATA(py_key);
return PyString_FromTDB_DATA(tdb_nextkey(self->ctx, key));
}
static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
{
TDB_DATA key;
PyObject *py_key;
int ret;
if (!PyArg_ParseTuple(args, "O", &py_key))
return NULL;
key = PyString_AsTDB_DATA(py_key);
ret = tdb_delete(self->ctx, key);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
{
TDB_DATA key;
int ret;
PyObject *py_key;
if (!PyArg_ParseTuple(args, "O", &py_key))
return NULL;
key = PyString_AsTDB_DATA(py_key);
ret = tdb_exists(self->ctx, key);
if (ret != TDB_ERR_NOEXIST) {
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
}
return (ret == TDB_ERR_NOEXIST)?Py_False:Py_True;
}
static PyObject *obj_store(PyTdbObject *self, PyObject *args)
{
TDB_DATA key, value;
int ret;
int flag = TDB_REPLACE;
PyObject *py_key, *py_value;
if (!PyArg_ParseTuple(args, "OO|i", &py_key, &py_value, &flag))
return NULL;
key = PyString_AsTDB_DATA(py_key);
value = PyString_AsTDB_DATA(py_value);
ret = tdb_store(self->ctx, key, value, flag);
PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
return Py_None;
}
static PyMethodDef tdb_object_methods[] = {
{ "transaction_cancel", (PyCFunction)obj_transaction_cancel, METH_NOARGS,
"S.transaction_cancel() -> None\n"
"Cancel the currently active transaction." },
{ "transaction_commit", (PyCFunction)obj_transaction_commit, METH_NOARGS,
"S.transaction_commit() -> None\n"
"Commit the currently active transaction." },
{ "transaction_recover", (PyCFunction)obj_transaction_recover, METH_NOARGS,
"S.transaction_recover() -> None\n"
"Recover the currently active transaction." },
{ "transaction_start", (PyCFunction)obj_transaction_start, METH_NOARGS,
"S.transaction_start() -> None\n"
"Start a new transaction." },
{ "reopen", (PyCFunction)obj_reopen, METH_NOARGS, "Reopen this file." },
{ "lock_all", (PyCFunction)obj_lockall, METH_NOARGS, NULL },
{ "unlock_all", (PyCFunction)obj_unlockall, METH_NOARGS, NULL },
{ "read_lock_all", (PyCFunction)obj_lockall_read, METH_NOARGS, NULL },
{ "read_unlock_all", (PyCFunction)obj_unlockall_read, METH_NOARGS, NULL },
{ "close", (PyCFunction)obj_close, METH_NOARGS, NULL },
{ "get", (PyCFunction)obj_get, METH_VARARGS, "S.fetch(key) -> value\n"
"Fetch a value." },
{ "append", (PyCFunction)obj_append, METH_VARARGS, "S.append(key, value) -> None\n"
"Append data to an existing key." },
{ "firstkey", (PyCFunction)obj_firstkey, METH_NOARGS, "S.firstkey() -> data\n"
"Return the first key in this database." },
{ "nextkey", (PyCFunction)obj_nextkey, METH_NOARGS, "S.nextkey(key) -> data\n"
"Return the next key in this database." },
{ "delete", (PyCFunction)obj_delete, METH_VARARGS, "S.delete(key) -> None\n"
"Delete an entry." },
{ "has_key", (PyCFunction)obj_has_key, METH_VARARGS, "S.has_key(key) -> None\n"
"Check whether key exists in this database." },
{ "store", (PyCFunction)obj_store, METH_VARARGS, "S.store(key, data, flag=REPLACE) -> None"
"Store data." },
{ NULL }
};
static PyObject *obj_get_hash_size(PyTdbObject *self, void *closure)
{
return PyInt_FromLong(tdb_hash_size(self->ctx));
}
static int obj_set_max_dead(PyTdbObject *self, PyObject *max_dead, void *closure)
{
if (!PyInt_Check(max_dead))
return -1;
tdb_set_max_dead(self->ctx, PyInt_AsLong(max_dead));
return 0;
}
static PyObject *obj_get_map_size(PyTdbObject *self, void *closure)
{
return PyInt_FromLong(tdb_map_size(self->ctx));
}
static PyObject *obj_get_flags(PyTdbObject *self, void *closure)
{
return PyInt_FromLong(tdb_get_flags(self->ctx));
}
static PyObject *obj_get_filename(PyTdbObject *self, void *closure)
{
return PyString_FromString(tdb_name(self->ctx));
}
static PyGetSetDef tdb_object_getsetters[] = {
{ (char *)"hash_size", (getter)obj_get_hash_size, NULL, NULL },
{ (char *)"map_size", (getter)obj_get_map_size, NULL, NULL },
{ (char *)"flags", (getter)obj_get_flags, NULL, NULL },
{ (char *)"max_dead", NULL, (setter)obj_set_max_dead, NULL },
{ (char *)"filename", (getter)obj_get_filename, NULL, (char *)"The filename of this TDB file."},
{ NULL }
};
static PyObject *tdb_object_repr(PyTdbObject *self)
{
return PyString_FromFormat("Tdb('%s')", tdb_name(self->ctx));
}
static void tdb_object_dealloc(PyTdbObject *self)
{
tdb_close(self->ctx);
PyObject_Del(self);
}
static PyObject *obj_getitem(PyTdbObject *self, PyObject *key)
{
TDB_DATA tkey, val;
if (!PyString_Check(key)) {
PyErr_SetString(PyExc_TypeError, "Expected string as key");
return NULL;
}
tkey.dptr = (unsigned char *)PyString_AsString(key);
tkey.dsize = PyString_Size(key);
val = tdb_fetch(self->ctx, tkey);
if (val.dptr == NULL) {
PyErr_SetString(PyExc_KeyError, "No such TDB entry");
return NULL;
} else {
return PyString_FromTDB_DATA(val);
}
}
static int obj_setitem(PyTdbObject *self, PyObject *key, PyObject *value)
{
TDB_DATA tkey, tval;
int ret;
if (!PyString_Check(key)) {
PyErr_SetString(PyExc_TypeError, "Expected string as key");
return -1;
}
tkey = PyString_AsTDB_DATA(key);
if (value == NULL) {
ret = tdb_delete(self->ctx, tkey);
} else {
if (!PyString_Check(value)) {
PyErr_SetString(PyExc_TypeError, "Expected string as value");
return -1;
}
tval = PyString_AsTDB_DATA(value);
ret = tdb_store(self->ctx, tkey, tval, TDB_REPLACE);
}
if (ret != 0) {
PyErr_SetTDBError(self->ctx);
return -1;
}
return ret;
}
static PyMappingMethods tdb_object_mapping = {
.mp_subscript = (binaryfunc)obj_getitem,
.mp_ass_subscript = (objobjargproc)obj_setitem,
};
PyTypeObject PyTdb = {
.tp_name = "Tdb",
.tp_basicsize = sizeof(PyTdbObject),
.tp_methods = tdb_object_methods,
.tp_getset = tdb_object_getsetters,
.tp_new = py_tdb_open,
.tp_doc = "A TDB file",
.tp_repr = (reprfunc)tdb_object_repr,
.tp_dealloc = (destructor)tdb_object_dealloc,
.tp_as_mapping = &tdb_object_mapping,
.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
};
static PyMethodDef tdb_methods[] = {
{ "open", (PyCFunction)py_tdb_open, METH_VARARGS|METH_KEYWORDS, "open(name, hash_size=0, tdb_flags=TDB_DEFAULT, flags=O_RDWR, mode=0600)\n"
"Open a TDB file." },
{ NULL }
};
void inittdb(void)
{
PyObject *m;
if (PyType_Ready(&PyTdb) < 0)
return;
m = Py_InitModule3("tdb", tdb_methods, "TDB is a simple key-value database similar to GDBM that supports multiple writers.");
if (m == NULL)
return;
PyModule_AddObject(m, "REPLACE", PyInt_FromLong(TDB_REPLACE));
PyModule_AddObject(m, "INSERT", PyInt_FromLong(TDB_INSERT));
PyModule_AddObject(m, "MODIFY", PyInt_FromLong(TDB_MODIFY));
PyModule_AddObject(m, "DEFAULT", PyInt_FromLong(TDB_DEFAULT));
PyModule_AddObject(m, "CLEAR_IF_FIRST", PyInt_FromLong(TDB_CLEAR_IF_FIRST));
PyModule_AddObject(m, "INTERNAL", PyInt_FromLong(TDB_INTERNAL));
PyModule_AddObject(m, "NOLOCK", PyInt_FromLong(TDB_NOLOCK));
PyModule_AddObject(m, "NOMMAP", PyInt_FromLong(TDB_NOMMAP));
PyModule_AddObject(m, "CONVERT", PyInt_FromLong(TDB_CONVERT));
PyModule_AddObject(m, "BIGENDIAN", PyInt_FromLong(TDB_BIGENDIAN));
PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
Py_INCREF(&PyTdb);
PyModule_AddObject(m, "Tdb", (PyObject *)&PyTdb);
}

View File

@ -1,10 +1,8 @@
[PYTHON::swig_tdb]
LIBRARY_REALNAME = _tdb.$(SHLIBEXT)
LIBRARY_REALNAME = tdb.$(SHLIBEXT)
PUBLIC_DEPENDENCIES = LIBTDB DYNCONFIG
swig_tdb_OBJ_FILES = $(tdbsrcdir)/tdb_wrap.o
swig_tdb_OBJ_FILES = $(tdbsrcdir)/pytdb.o
$(eval $(call python_py_module_template,tdb.py,$(tdbsrcdir)/tdb.py))
$(swig_tdb_OBJ_FILES): CFLAGS+=$(CFLAG_NO_UNUSED_MACROS) $(CFLAG_NO_CAST_QUAL)
$(swig_tdb_OBJ_FILES): CFLAGS+=$(CFLAG_NO_CAST_QUAL)

View File

@ -73,7 +73,7 @@ class SimpleTdbTests(TestCase):
self.tdb.map_size
def test_name(self):
self.tdb.name
self.tdb.filename
def test_iterator(self):
self.tdb["bla"] = "1"

View File

@ -1,328 +0,0 @@
/*
Unix SMB/CIFS implementation.
Swig interface to tdb.
Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
** NOTE! The following LGPL license applies to the tdb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library 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 3 of the License, or (at your option) any later version.
This library 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 library; if not, see <http://www.gnu.org/licenses/>.
*/
%define DOCSTRING
"TDB is a simple key-value database similar to GDBM that supports multiple writers."
%enddef
%module(docstring=DOCSTRING) tdb
%{
/* This symbol is used in both includes.h and Python.h which causes an
annoying compiler warning. */
#ifdef HAVE_FSTAT
#undef HAVE_FSTAT
#endif
/* Include tdb headers */
#include <stdint.h>
#include <signal.h>
#include <tdb.h>
#include <fcntl.h>
typedef TDB_CONTEXT tdb;
%}
/* The tdb functions will crash if a NULL tdb context is passed */
%import exception.i
%import stdint.i
%typemap(check,noblock=1) TDB_CONTEXT* {
if ($1 == NULL)
SWIG_exception(SWIG_ValueError,
"tdb context must be non-NULL");
}
/* In and out typemaps for the TDB_DATA structure. This is converted to
and from the Python string type which can contain arbitrary binary
data.. */
%typemap(in,noblock=1) TDB_DATA {
if ($input == Py_None) {
$1.dsize = 0;
$1.dptr = NULL;
} else if (!PyString_Check($input)) {
PyErr_SetString(PyExc_TypeError, "string arg expected");
return NULL;
} else {
$1.dsize = PyString_Size($input);
$1.dptr = (uint8_t *)PyString_AsString($input);
}
}
%typemap(out,noblock=1) TDB_DATA {
if ($1.dptr == NULL && $1.dsize == 0) {
$result = Py_None;
} else {
$result = PyString_FromStringAndSize((const char *)$1.dptr, $1.dsize);
free($1.dptr);
}
}
/* Treat a mode_t as an unsigned integer */
typedef int mode_t;
/* flags to tdb_store() */
%constant int REPLACE = TDB_REPLACE;
%constant int INSERT = TDB_INSERT;
%constant int MODIFY = TDB_MODIFY;
/* flags for tdb_open() */
%constant int DEFAULT = TDB_DEFAULT;
%constant int CLEAR_IF_FIRST = TDB_CLEAR_IF_FIRST;
%constant int INTERNAL = TDB_INTERNAL;
%constant int NOLOCK = TDB_NOLOCK;
%constant int NOMMAP = TDB_NOMMAP;
%constant int CONVERT = TDB_CONVERT;
%constant int BIGENDIAN = TDB_BIGENDIAN;
enum TDB_ERROR {
TDB_SUCCESS=0,
TDB_ERR_CORRUPT,
TDB_ERR_IO,
TDB_ERR_LOCK,
TDB_ERR_OOM,
TDB_ERR_EXISTS,
TDB_ERR_NOLOCK,
TDB_ERR_LOCK_TIMEOUT,
TDB_ERR_NOEXIST,
TDB_ERR_EINVAL,
TDB_ERR_RDONLY
};
%rename(lock_all) tdb_context::lockall;
%rename(unlock_all) tdb_context::unlockall;
%rename(read_lock_all) tdb_context::lockall_read;
%rename(read_unlock_all) tdb_context::unlockall_read;
%typemap(default,noblock=1) int tdb_flags {
$1 = TDB_DEFAULT;
}
%typemap(default,noblock=1) int flags {
$1 = O_RDWR;
}
%typemap(default,noblock=1) int hash_size {
$1 = 0;
}
%typemap(default,noblock=1) mode_t mode {
$1 = 0600;
}
%typemap(default,noblock=1) int flag {
$1 = TDB_REPLACE;
}
%rename(Tdb) tdb_context;
%feature("docstring") tdb_context "A TDB file.";
%typemap(out,noblock=1) tdb * {
/* Throw an IOError exception from errno if tdb_open() returns NULL */
if ($1 == NULL) {
PyErr_SetFromErrno(PyExc_IOError);
SWIG_fail;
}
$result = SWIG_NewPointerObj($1, $1_descriptor, 0);
}
typedef struct tdb_context {
%extend {
%feature("docstring") tdb "S.__init__(name,hash_size=0,tdb_flags=TDB_DEFAULT,flags=O_RDWR,mode=0600)\n"
"Open a TDB file.";
tdb(const char *name, int hash_size, int tdb_flags, int flags, mode_t mode) {
return tdb_open(name, hash_size, tdb_flags, flags, mode);
}
%feature("docstring") error "S.error() -> int\n"
"Find last error number returned by operation on this TDB.";
enum TDB_ERROR error();
~tdb() { tdb_close($self); }
%feature("docstring") close "S.close() -> None\n"
"Close the TDB file.";
int close();
int append(TDB_DATA key, TDB_DATA new_dbuf);
%feature("docstring") errorstr "S.errorstr() -> errorstring\n"
"Obtain last error message.";
const char *errorstr();
%rename(get) fetch;
%feature("docstring") fetch "S.fetch(key) -> value\n"
"Fetch a value.";
TDB_DATA fetch(TDB_DATA key);
%feature("docstring") delete "S.delete(key) -> None\n"
"Delete an entry.";
int delete(TDB_DATA key);
%feature("docstring") store "S.store(key, value, flag=TDB_REPLACE) -> None\n"
"Store an entry.";
int store(TDB_DATA key, TDB_DATA dbuf, int flag);
%feature("docstring") exists "S.exists(key) -> bool\n"
"Check whether key exists in this database.";
int exists(TDB_DATA key);
%feature("docstring") firstkey "S.firstkey() -> data\n"
"Return the first key in this database.";
TDB_DATA firstkey();
%feature("docstring") nextkey "S.nextkey(prev) -> data\n"
"Return the next key in this database.";
TDB_DATA nextkey(TDB_DATA key);
%feature("docstring") lockall "S.lockall() -> bool";
int lockall();
%feature("docstring") unlockall "S.unlockall() -> bool";
int unlockall();
%feature("docstring") unlockall "S.lockall_read() -> bool";
int lockall_read();
%feature("docstring") unlockall "S.unlockall_read() -> bool";
int unlockall_read();
%feature("docstring") reopen "S.reopen() -> bool\n"
"Reopen this file.";
int reopen();
%feature("docstring") transaction_start "S.transaction_start() -> None\n"
"Start a new transaction.";
int transaction_start();
%feature("docstring") transaction_commit "S.transaction_commit() -> None\n"
"Commit the currently active transaction.";
int transaction_commit();
%feature("docstring") transaction_cancel "S.transaction_cancel() -> None\n"
"Cancel the currently active transaction.";
int transaction_cancel();
int transaction_recover();
%feature("docstring") hash_size "S.hash_size() -> int";
int hash_size();
%feature("docstring") map_size "S.map_size() -> int";
size_t map_size();
%feature("docstring") get_flags "S.get_flags() -> int";
int get_flags();
%feature("docstring") set_max_dead "S.set_max_dead(int) -> None";
void set_max_dead(int max_dead);
%feature("docstring") name "S.name() -> path\n" \
"Return filename of this TDB file.";
const char *name();
}
%pythoncode {
def __repr__(self):
return "Tdb('%s')" % self.name()
# Random access to keys, values
def __getitem__(self, key):
result = self.get(key)
if result is None:
raise KeyError, '%s: %s' % (key, self.errorstr())
return result
def __setitem__(self, key, item):
if self.store(key, item) == -1:
raise IOError, self.errorstr()
def __delitem__(self, key):
if not self.exists(key):
raise KeyError, '%s: %s' % (key, self.errorstr())
self.delete(key)
def __contains__(self, key):
return self.exists(key) != 0
def has_key(self, key):
return self.exists(key) != 0
def fetch_uint32(self, key):
data = self.get(key)
if data is None:
return None
import struct
return struct.unpack("<L", data)[0]
def fetch_int32(self, key):
data = self.get(key)
if data is None:
return None
import struct
return struct.unpack("<l", data)[0]
# Tdb iterator
class TdbIterator:
def __init__(self, tdb):
self.tdb = tdb
self.key = None
def __iter__(self):
return self
def next(self):
if self.key is None:
self.key = self.tdb.firstkey()
if self.key is None:
raise StopIteration
return self.key
else:
self.key = self.tdb.nextkey(self.key)
if self.key is None:
raise StopIteration
return self.key
def __iter__(self):
return self.TdbIterator(self)
# Implement other dict functions using TdbIterator
def keys(self):
return [k for k in iter(self)]
def values(self):
return [self[k] for k in iter(self)]
def items(self):
return [(k, self[k]) for k in iter(self)]
def __len__(self):
return len(self.keys())
def clear(self):
for k in iter(self):
del(self[k])
def iterkeys(self):
for k in iter(self):
yield k
def itervalues(self):
for k in iter(self):
yield self[k]
def iteritems(self):
for k in iter(self):
yield (k, self[k])
# TODO: any other missing methods for container types
}
} tdb;
%pythoncode {
__docformat__ = 'restructuredText'
open = Tdb
}

View File

@ -1,344 +0,0 @@
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 1.3.36
#
# Don't modify this file, modify the SWIG interface instead.
"""
TDB is a simple key-value database similar to GDBM that supports multiple writers.
"""
import _tdb
import new
new_instancemethod = new.instancemethod
try:
_swig_property = property
except NameError:
pass # Python < 2.2 doesn't have 'property'.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
if (name == "thisown"): return self.this.own(value)
if (name == "this"):
if type(value).__name__ == 'PySwigObject':
self.__dict__[name] = value
return
method = class_type.__swig_setmethods__.get(name,None)
if method: return method(self,value)
if (not static) or hasattr(self,name):
self.__dict__[name] = value
else:
raise AttributeError("You cannot add attributes to %s" % self)
def _swig_setattr(self,class_type,name,value):
return _swig_setattr_nondynamic(self,class_type,name,value,0)
def _swig_getattr(self,class_type,name):
if (name == "thisown"): return self.this.own()
method = class_type.__swig_getmethods__.get(name,None)
if method: return method(self)
raise AttributeError,name
def _swig_repr(self):
try: strthis = "proxy of " + self.this.__repr__()
except: strthis = ""
return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)
import types
try:
_object = types.ObjectType
_newclass = 1
except AttributeError:
class _object : pass
_newclass = 0
del types
def _swig_setattr_nondynamic_method(set):
def set_attr(self,name,value):
if (name == "thisown"): return self.this.own(value)
if hasattr(self,name) or (name == "this"):
set(self,name,value)
else:
raise AttributeError("You cannot add attributes to %s" % self)
return set_attr
REPLACE = _tdb.REPLACE
INSERT = _tdb.INSERT
MODIFY = _tdb.MODIFY
DEFAULT = _tdb.DEFAULT
CLEAR_IF_FIRST = _tdb.CLEAR_IF_FIRST
INTERNAL = _tdb.INTERNAL
NOLOCK = _tdb.NOLOCK
NOMMAP = _tdb.NOMMAP
CONVERT = _tdb.CONVERT
BIGENDIAN = _tdb.BIGENDIAN
TDB_SUCCESS = _tdb.TDB_SUCCESS
TDB_ERR_CORRUPT = _tdb.TDB_ERR_CORRUPT
TDB_ERR_IO = _tdb.TDB_ERR_IO
TDB_ERR_LOCK = _tdb.TDB_ERR_LOCK
TDB_ERR_OOM = _tdb.TDB_ERR_OOM
TDB_ERR_EXISTS = _tdb.TDB_ERR_EXISTS
TDB_ERR_NOLOCK = _tdb.TDB_ERR_NOLOCK
TDB_ERR_LOCK_TIMEOUT = _tdb.TDB_ERR_LOCK_TIMEOUT
TDB_ERR_NOEXIST = _tdb.TDB_ERR_NOEXIST
TDB_ERR_EINVAL = _tdb.TDB_ERR_EINVAL
TDB_ERR_RDONLY = _tdb.TDB_ERR_RDONLY
class Tdb(object):
"""A TDB file."""
thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag')
__repr__ = _swig_repr
def __init__(self, *args, **kwargs):
"""
S.__init__(name,hash_size=0,tdb_flags=TDB_DEFAULT,flags=O_RDWR,mode=0600)
Open a TDB file.
"""
_tdb.Tdb_swiginit(self,_tdb.new_Tdb(*args, **kwargs))
def error(*args, **kwargs):
"""
S.error() -> int
Find last error number returned by operation on this TDB.
"""
return _tdb.Tdb_error(*args, **kwargs)
__swig_destroy__ = _tdb.delete_Tdb
def close(*args, **kwargs):
"""
S.close() -> None
Close the TDB file.
"""
return _tdb.Tdb_close(*args, **kwargs)
def errorstr(*args, **kwargs):
"""
S.errorstr() -> errorstring
Obtain last error message.
"""
return _tdb.Tdb_errorstr(*args, **kwargs)
def get(*args, **kwargs):
"""
S.fetch(key) -> value
Fetch a value.
"""
return _tdb.Tdb_get(*args, **kwargs)
def delete(*args, **kwargs):
"""
S.delete(key) -> None
Delete an entry.
"""
return _tdb.Tdb_delete(*args, **kwargs)
def store(*args, **kwargs):
"""
S.store(key, value, flag=TDB_REPLACE) -> None
Store an entry.
"""
return _tdb.Tdb_store(*args, **kwargs)
def exists(*args, **kwargs):
"""
S.exists(key) -> bool
Check whether key exists in this database.
"""
return _tdb.Tdb_exists(*args, **kwargs)
def firstkey(*args, **kwargs):
"""
S.firstkey() -> data
Return the first key in this database.
"""
return _tdb.Tdb_firstkey(*args, **kwargs)
def nextkey(*args, **kwargs):
"""
S.nextkey(prev) -> data
Return the next key in this database.
"""
return _tdb.Tdb_nextkey(*args, **kwargs)
def lock_all(*args, **kwargs):
"""S.lockall() -> bool"""
return _tdb.Tdb_lock_all(*args, **kwargs)
def unlock_all(*args, **kwargs):
"""S.unlockall() -> bool"""
return _tdb.Tdb_unlock_all(*args, **kwargs)
def reopen(*args, **kwargs):
"""
S.reopen() -> bool
Reopen this file.
"""
return _tdb.Tdb_reopen(*args, **kwargs)
def transaction_start(*args, **kwargs):
"""
S.transaction_start() -> None
Start a new transaction.
"""
return _tdb.Tdb_transaction_start(*args, **kwargs)
def transaction_commit(*args, **kwargs):
"""
S.transaction_commit() -> None
Commit the currently active transaction.
"""
return _tdb.Tdb_transaction_commit(*args, **kwargs)
def transaction_cancel(*args, **kwargs):
"""
S.transaction_cancel() -> None
Cancel the currently active transaction.
"""
return _tdb.Tdb_transaction_cancel(*args, **kwargs)
def hash_size(*args, **kwargs):
"""S.hash_size() -> int"""
return _tdb.Tdb_hash_size(*args, **kwargs)
def map_size(*args, **kwargs):
"""S.map_size() -> int"""
return _tdb.Tdb_map_size(*args, **kwargs)
def get_flags(*args, **kwargs):
"""S.get_flags() -> int"""
return _tdb.Tdb_get_flags(*args, **kwargs)
def set_max_dead(*args, **kwargs):
"""S.set_max_dead(int) -> None"""
return _tdb.Tdb_set_max_dead(*args, **kwargs)
def name(*args, **kwargs):
"""
S.name() -> path
Return filename of this TDB file.
"""
return _tdb.Tdb_name(*args, **kwargs)
def __repr__(self):
return "Tdb('%s')" % self.name()
def __getitem__(self, key):
result = self.get(key)
if result is None:
raise KeyError, '%s: %s' % (key, self.errorstr())
return result
def __setitem__(self, key, item):
if self.store(key, item) == -1:
raise IOError, self.errorstr()
def __delitem__(self, key):
if not self.exists(key):
raise KeyError, '%s: %s' % (key, self.errorstr())
self.delete(key)
def __contains__(self, key):
return self.exists(key) != 0
def has_key(self, key):
return self.exists(key) != 0
def fetch_uint32(self, key):
data = self.get(key)
if data is None:
return None
import struct
return struct.unpack("<L", data)[0]
def fetch_int32(self, key):
data = self.get(key)
if data is None:
return None
import struct
return struct.unpack("<l", data)[0]
class TdbIterator:
def __init__(self, tdb):
self.tdb = tdb
self.key = None
def __iter__(self):
return self
def next(self):
if self.key is None:
self.key = self.tdb.firstkey()
if self.key is None:
raise StopIteration
return self.key
else:
self.key = self.tdb.nextkey(self.key)
if self.key is None:
raise StopIteration
return self.key
def __iter__(self):
return self.TdbIterator(self)
def keys(self):
return [k for k in iter(self)]
def values(self):
return [self[k] for k in iter(self)]
def items(self):
return [(k, self[k]) for k in iter(self)]
def __len__(self):
return len(self.keys())
def clear(self):
for k in iter(self):
del(self[k])
def iterkeys(self):
for k in iter(self):
yield k
def itervalues(self):
for k in iter(self):
yield self[k]
def iteritems(self):
for k in iter(self):
yield (k, self[k])
Tdb.error = new_instancemethod(_tdb.Tdb_error,None,Tdb)
Tdb.close = new_instancemethod(_tdb.Tdb_close,None,Tdb)
Tdb.append = new_instancemethod(_tdb.Tdb_append,None,Tdb)
Tdb.errorstr = new_instancemethod(_tdb.Tdb_errorstr,None,Tdb)
Tdb.get = new_instancemethod(_tdb.Tdb_get,None,Tdb)
Tdb.delete = new_instancemethod(_tdb.Tdb_delete,None,Tdb)
Tdb.store = new_instancemethod(_tdb.Tdb_store,None,Tdb)
Tdb.exists = new_instancemethod(_tdb.Tdb_exists,None,Tdb)
Tdb.firstkey = new_instancemethod(_tdb.Tdb_firstkey,None,Tdb)
Tdb.nextkey = new_instancemethod(_tdb.Tdb_nextkey,None,Tdb)
Tdb.lock_all = new_instancemethod(_tdb.Tdb_lock_all,None,Tdb)
Tdb.unlock_all = new_instancemethod(_tdb.Tdb_unlock_all,None,Tdb)
Tdb.read_lock_all = new_instancemethod(_tdb.Tdb_read_lock_all,None,Tdb)
Tdb.read_unlock_all = new_instancemethod(_tdb.Tdb_read_unlock_all,None,Tdb)
Tdb.reopen = new_instancemethod(_tdb.Tdb_reopen,None,Tdb)
Tdb.transaction_start = new_instancemethod(_tdb.Tdb_transaction_start,None,Tdb)
Tdb.transaction_commit = new_instancemethod(_tdb.Tdb_transaction_commit,None,Tdb)
Tdb.transaction_cancel = new_instancemethod(_tdb.Tdb_transaction_cancel,None,Tdb)
Tdb.transaction_recover = new_instancemethod(_tdb.Tdb_transaction_recover,None,Tdb)
Tdb.hash_size = new_instancemethod(_tdb.Tdb_hash_size,None,Tdb)
Tdb.map_size = new_instancemethod(_tdb.Tdb_map_size,None,Tdb)
Tdb.get_flags = new_instancemethod(_tdb.Tdb_get_flags,None,Tdb)
Tdb.set_max_dead = new_instancemethod(_tdb.Tdb_set_max_dead,None,Tdb)
Tdb.name = new_instancemethod(_tdb.Tdb_name,None,Tdb)
Tdb_swigregister = _tdb.Tdb_swigregister
Tdb_swigregister(Tdb)
__docformat__ = 'restructuredText'
open = Tdb

File diff suppressed because it is too large Load Diff