diff options
author | Tim Potter <tpot@samba.org> | 2006-04-02 01:56:22 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 13:59:43 -0500 |
commit | e29489d9b6c9089d63667541bb8cff4637089d75 (patch) | |
tree | a07409e7851c0ce634b6004a0885686090ae49d9 /source4/scripting/swig/Tdb.py | |
parent | 0bc6e7a090dd92465d905ffb0a2530d67d0bcc9c (diff) | |
download | samba-e29489d9b6c9089d63667541bb8cff4637089d75.tar.gz samba-e29489d9b6c9089d63667541bb8cff4637089d75.tar.bz2 samba-e29489d9b6c9089d63667541bb8cff4637089d75.zip |
r14854: Finish off swig wrapper and torture tests for tdb.
(This used to be commit 1c4042e23f3e99f95133313607e2c2904503cf74)
Diffstat (limited to 'source4/scripting/swig/Tdb.py')
-rw-r--r-- | source4/scripting/swig/Tdb.py | 78 |
1 files changed, 70 insertions, 8 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]) |