summaryrefslogtreecommitdiff
path: root/source4/lib/tdb/swig/Tdb.py
diff options
context:
space:
mode:
authorTim Potter <tpot@samba.org>2006-04-08 00:40:52 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 14:00:48 -0500
commit8c23f133c4a1cd58a83c837bcd8f77b739e87ff2 (patch)
treeaefd60494d28ee850e8838f6021b74e15a895db0 /source4/lib/tdb/swig/Tdb.py
parent35ee2474874676184ec94d19f70e02b6459cfd9c (diff)
downloadsamba-8c23f133c4a1cd58a83c837bcd8f77b739e87ff2.tar.gz
samba-8c23f133c4a1cd58a83c837bcd8f77b739e87ff2.tar.bz2
samba-8c23f133c4a1cd58a83c837bcd8f77b739e87ff2.zip
r14968: Move tdb and ldb swig wrappers in to lib/tdb and lib/ldb directories.
(This used to be commit fa8d0dc14a1af9567401d54a803b34a6498b7cd4)
Diffstat (limited to 'source4/lib/tdb/swig/Tdb.py')
-rw-r--r--source4/lib/tdb/swig/Tdb.py115
1 files changed, 115 insertions, 0 deletions
diff --git a/source4/lib/tdb/swig/Tdb.py b/source4/lib/tdb/swig/Tdb.py
new file mode 100644
index 0000000000..ef0165ed8b
--- /dev/null
+++ b/source4/lib/tdb/swig/Tdb.py
@@ -0,0 +1,115 @@
+"""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 2 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, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+
+import tdb, os
+
+# Open flags
+
+DEFAULT = tdb.TDB_DEFAULT
+CLEAR_IF_FIRST = tdb.TDB_CLEAR_IF_FIRST
+INTERNAL = tdb.TDB_INTERNAL
+NOLOCK = tdb.TDB_NOLOCK
+NOMMAP = tdb.TDB_NOMMAP
+
+# Class representing a TDB file
+
+class Tdb:
+
+ # Create and destroy Tdb objects
+
+ def __init__(self, name, hash_size = 0, flags = tdb.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)
+
+ # 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])