svn commit: samba r25643 - in branches/4.0-python: . source/lib/tdb/swig

jelmer at samba.org jelmer at samba.org
Mon Oct 15 13:28:39 GMT 2007


Author: jelmer
Date: 2007-10-15 13:28:38 +0000 (Mon, 15 Oct 2007)
New Revision: 25643

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=25643

Log:
Merge Tdb.py into tdb.i.
Expand tdb.i a bit, supporting more functions.

Removed:
   branches/4.0-python/source/lib/tdb/swig/Tdb.py
   branches/4.0-python/source/lib/tdb/swig/tests/
Modified:
   branches/4.0-python/
   branches/4.0-python/source/lib/tdb/swig/tdb.i


Changeset:

Property changes on: branches/4.0-python
___________________________________________________________________
Name: bzr:revision-info
...skipped...
Name: bzr:file-ids
...skipped...
Name: bzr:revision-id:v3-trunk0
...skipped...

Deleted: branches/4.0-python/source/lib/tdb/swig/Tdb.py
===================================================================
--- branches/4.0-python/source/lib/tdb/swig/Tdb.py	2007-10-15 08:35:24 UTC (rev 25642)
+++ branches/4.0-python/source/lib/tdb/swig/Tdb.py	2007-10-15 13:28:38 UTC (rev 25643)
@@ -1,119 +0,0 @@
-"""Provide a more Pythonic and object-oriented interface to tdb."""
-
-#
-# Swig interface to Samba
-#
-# Copyright (C) Tim Potter 2006
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
-#   
-# This program 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 General Public License for more details.
-#   
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, see <http://www.gnu.org/licenses/>.
-#
-
-import os
-from tdb import *
-
-# Open flags
-
-DEFAULT        = TDB_DEFAULT
-CLEAR_IF_FIRST = TDB_CLEAR_IF_FIRST
-INTERNAL       = TDB_INTERNAL
-NOLOCK         = TDB_NOLOCK
-NOMMAP         = TDB_NOMMAP
-
-# Class representing a TDB file
-
-class Tdb:
-
-    # Create and destroy Tdb objects
-
-    def __init__(self, name, hash_size = 0, flags = TDB_DEFAULT,
-                 open_flags = os.O_RDWR | os.O_CREAT, mode = 0600):
-        self.tdb = tdb_open(name, hash_size, flags, open_flags, mode)
-        if self.tdb is None:
-            raise IOError, tdb_errorstr(self.tdb)
-        
-    def __del__(self):
-        self.close()
-
-    def close(self):
-        if hasattr(self, 'tdb') and self.tdb is not None:
-            if tdb_close(self.tdb) == -1:
-                raise IOError, tdb_errorstr(self.tdb)
-            self.tdb = None
-
-    # Random access to keys, values
-
-    def __getitem__(self, key):
-        result = tdb_fetch(self.tdb, key)
-        if result is None:
-            raise KeyError, '%s: %s' % (key, tdb_errorstr(self.tdb))
-        return result
-
-    def __setitem__(self, key, item):
-        if tdb_store(self.tdb, key, item) == -1:
-            raise IOError, tdb_errorstr(self.tdb)
-
-    def __delitem__(self, key):
-        if not tdb_exists(self.tdb, key):
-            raise KeyError, '%s: %s' % (key, tdb_errorstr(self.tdb))
-        tdb_delete(self.tdb, key)
-
-    def has_key(self, key):
-        return tdb_exists(self.tdb, key) != 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 = tdb_firstkey(self.tdb)
-                if self.key is None:
-                    raise StopIteration
-                return self.key
-            else:
-                self.key = tdb_nextkey(self.tdb, self.key)
-                if self.key is None:
-                    raise StopIteration
-                return self.key
-
-    def __iter__(self):
-        return Tdb.TdbIterator(self.tdb)
-
-    # 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])
-
-    # TODO: iterkeys, itervalues, iteritems
-
-    # TODO: any other missing methods for container types

Modified: branches/4.0-python/source/lib/tdb/swig/tdb.i
===================================================================
--- branches/4.0-python/source/lib/tdb/swig/tdb.i	2007-10-15 08:35:24 UTC (rev 25642)
+++ branches/4.0-python/source/lib/tdb/swig/tdb.i	2007-10-15 13:28:38 UTC (rev 25643)
@@ -3,7 +3,8 @@
 
    Swig interface to tdb.
 
-   Copyright (C) 2004,2005 Tim Potter <tpot at samba.org>
+   Copyright (C) 2004-2006 Tim Potter <tpot at samba.org>
+   Copyright (C) 2007 Jelmer Vernooij <jelmer at samba.org>
 
      ** NOTE! The following LGPL license applies to the tdb
      ** library. This does NOT imply that all of Samba is released
@@ -34,20 +35,11 @@
 #undef HAVE_FSTAT
 #endif
 
-#if (__GNUC__ >= 3)
-/** Use gcc attribute to check printf fns.  a1 is the 1-based index of
- * the parameter containing the format, and a2 the index of the first
- * argument. Note that some gcc 2.x versions don't handle this
- * properly **/
-#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
-#else
-#define PRINTF_ATTRIBUTE(a1, a2)
-#endif
-
 /* Include tdb headers */
+#include <tdb.h>
+#include <fcntl.h>
 
-#include "lib/tdb/include/tdb.h"
-
+typedef TDB_CONTEXT tdb;
 %}
 
 /* The tdb functions will crash if a NULL tdb context is passed */
@@ -70,38 +62,35 @@
 		return NULL;
 	}
 	$1.dsize = PyString_Size($input);
-	$1.dptr = PyString_AsString($input);
+	$1.dptr = (uint8_t *)PyString_AsString($input);
 }
 
 %typemap(out) TDB_DATA {
 	if ($1.dptr == NULL && $1.dsize == 0) {
 		$result = Py_None;
 	} else {
-		$result = PyString_FromStringAndSize($1.dptr, $1.dsize);
+		$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;
 
-#define TDB_REPLACE 1
-#define TDB_INSERT 2
-#define TDB_MODIFY 3
-
 /* 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;
 
-#define TDB_DEFAULT 0 /* just a readability place holder */
-#define TDB_CLEAR_IF_FIRST 1
-#define TDB_INTERNAL 2 /* don't store on disk */
-#define TDB_NOLOCK   4 /* don't do any locking */
-#define TDB_NOMMAP   8 /* don't use mmap */
-#define TDB_CONVERT 16 /* convert endian (internal use) */
-#define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
-
 enum TDB_ERROR {
      TDB_SUCCESS=0, 
      TDB_ERR_CORRUPT, 
@@ -116,37 +105,142 @@
      TDB_ERR_RDONLY
 };
 
-/* Throw an IOError exception from errno if tdb_open() returns NULL */
+%rename(Tdb) tdb;
+%rename(lock_all) tdb_context::lockall;
 
-%exception {
-	$action
-	if (result == NULL) {
-		PyErr_SetFromErrno(PyExc_IOError);
-		SWIG_fail;
-	}
+%typemap(default) int tdb_flags {
+    $1 = TDB_DEFAULT;
 }
 
-TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
-		      int open_flags, mode_t mode);
+%typemap(default) int open_flags {
+    $1 = O_RDWR;
+}
 
-%exception;
+%typemap(default) int hash_size {
+    $1 = 0;
+}
 
-enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb);
+%typemap(default) mode_t mode {
+    $1 = 0600;
+}
 
-const char *tdb_errorstr(TDB_CONTEXT *tdb);
+%typemap(default) int flag {
+    $1 = TDB_REPLACE;
+}
 
-TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key);
+typedef struct tdb_context {
+    %extend {
+        tdb(const char *name, int hash_size,
+                    int tdb_flags,
+                    int open_flags, mode_t mode)
+        {
+            tdb *ret = tdb_open(name, hash_size, tdb_flags, open_flags, mode);
 
-int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key);
+            /* Throw an IOError exception from errno if tdb_open() returns 
+               NULL */
+            if (ret == NULL) {
+                PyErr_SetFromErrno(PyExc_IOError);
+                SWIG_fail;
+            }
 
-int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag = TDB_REPLACE);
+fail:
+            return ret;
+        }
+        enum TDB_ERROR error();
+        ~tdb() { tdb_close($self); }
+        int close();
+        int append(TDB_DATA key, TDB_DATA new_dbuf);
+        const char *errorstr();
+        TDB_DATA fetch(TDB_DATA key);
+        int delete(TDB_DATA key);
+        int store(TDB_DATA key, TDB_DATA dbuf, int flag);
+        int exists(TDB_DATA key);
+        TDB_DATA firstkey();
+        TDB_DATA nextkey(TDB_DATA key);
+        int lockall();
+        int unlockall();
+        int reopen();
+        int transaction_start();
+        int transaction_commit();
+        int transaction_cancel();
+        int transaction_recover();
+        int hash_size();
+        size_t map_size();
+        int get_flags();
+        void set_max_dead(int max_dead);
+        const char *name();
+    }
 
-int tdb_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf);
+    %pythoncode {
+    def __del__(self):
+        self.close()
 
-int tdb_close(TDB_CONTEXT *tdb);
+    def __str__(self):
+        return self.name()
 
-TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb);
+    # Random access to keys, values
+    def __getitem__(self, key):
+        result = self.fetch(key)
+        if result is None:
+            raise KeyError, '%s: %s' % (key, self.errorstr())
+        return result
 
-TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA key);
+    def __setitem__(self, key, item):
+        if self.store(key, item) == -1:
+            raise IOError, self.errorstr()
 
-int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key);
+    def __delitem__(self, key):
+        if not self.exists(key):
+            raise KeyError, '%s: %s' % (key, self.errorstr())
+        self.delete(key)
+
+    def has_key(self, key):
+        return self.exists(key) != 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 Tdb.TdbIterator(self.tdb)
+
+    # 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])
+
+    # TODO: iterkeys, itervalues, iteritems
+
+    # TODO: any other missing methods for container types
+    }
+} tdb;



More information about the samba-cvs mailing list