summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source4/scripting/swig/Tdb.py78
-rwxr-xr-xsource4/scripting/swig/torture/torture_tdb.py82
2 files changed, 149 insertions, 11 deletions
diff --git a/source4/scripting/swig/Tdb.py b/source4/scripting/swig/Tdb.py
index 65d40fc14b..571a18dbeb 100644
--- a/source4/scripting/swig/Tdb.py
+++ b/source4/scripting/swig/Tdb.py
@@ -22,24 +22,86 @@
import tdb, os, UserDict
+# 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:
- def __init__(self, name, hash_size = 0, tdb_flags = tdb.TDB_DEFAULT,
- open_flags = os.O_RDWR | os.O_CREAT, mode = 0600):
+ # Create and destroy Tdb objects
- self.tdb = tdb.open(name, hash_size, tdb_flags, open_flags, mode)
+ 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)
def __del__(self):
- tdb.close(self.tdb)
+ if hasattr(self, 'tdb'):
+ tdb.close(self.tdb)
+
+ # Random access to keys, values
def __getitem__(self, key):
- pass
+ result = tdb.fetch(self.tdb, key)
+ if result is None:
+ raise KeyError, key
+ return result
def __setitem__(self, key, item):
- pass
+ tdb.store(self.tdb, key, item)
def __delitem__(self, key):
- pass
+ if not tdb.exists(self.tdb, key):
+ raise KeyError, key
+ 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):
- pass
+ 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])
diff --git a/source4/scripting/swig/torture/torture_tdb.py b/source4/scripting/swig/torture/torture_tdb.py
index d9ca4dedba..59eb5fe1f0 100755
--- a/source4/scripting/swig/torture/torture_tdb.py
+++ b/source4/scripting/swig/torture/torture_tdb.py
@@ -1,6 +1,82 @@
#!/usr/bin/python
-import Tdb, os
+import sys, os
+import Tdb
-t = Tdb.Tdb('foo.tdb')
-os.unlink('foo.tdb')
+def fail(msg):
+ print 'FAILED:', msg
+ sys.exit(1)
+
+tdb_file = '/tmp/torture_tdb.tdb'
+
+# Create temporary tdb file
+
+t = Tdb.Tdb(tdb_file, flags = Tdb.CLEAR_IF_FIRST)
+
+# Check non-existent key throws KeyError exception
+
+try:
+ t['__none__']
+except KeyError:
+ pass
+else:
+ fail('non-existent key did not throw KeyError')
+
+# Check storing key
+
+t['bar'] = '1234'
+if t['bar'] != '1234':
+ fail('store key failed')
+
+# Check key exists
+
+if not t.has_key('bar'):
+ fail('has_key() failed for existing key')
+
+if t.has_key('__none__'):
+ fail('has_key() succeeded for non-existent key')
+
+# Delete key
+
+try:
+ del(t['__none__'])
+except KeyError:
+ pass
+else:
+ fail('delete of non-existent key did not throw KeyError')
+
+del t['bar']
+if t.has_key('bar'):
+ fail('delete of existing key did not delete key')
+
+# Clear all keys
+
+t.clear()
+if len(t) != 0:
+ fail('clear failed to remove all keys')
+
+# Other dict functions
+
+t['a'] = '1'
+t['ab'] = '12'
+t['abc'] = '123'
+
+if len(t) != 3:
+ fail('len method produced wrong value')
+
+keys = t.keys()
+values = t.values()
+items = t.items()
+
+if set(keys) != set(['a', 'ab', 'abc']):
+ fail('keys method produced wrong values')
+
+if set(values) != set(['1', '12', '123']):
+ fail('values method produced wrong values')
+
+if set(items) != set([('a', '1'), ('ab', '12'), ('abc', '123')]):
+ fail('values method produced wrong values')
+
+# Clean up
+
+os.unlink(tdb_file)