[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / tdb / swig / Tdb.py
blobaac7a90354d60841726ab8bc3b5e88438b0e7a63
1 """Provide a more Pythonic and object-oriented interface to tdb."""
4 # Swig interface to Samba
6 # Copyright (C) Tim Potter 2006
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 import os
24 from tdb import *
26 # Open flags
28 DEFAULT = TDB_DEFAULT
29 CLEAR_IF_FIRST = TDB_CLEAR_IF_FIRST
30 INTERNAL = TDB_INTERNAL
31 NOLOCK = TDB_NOLOCK
32 NOMMAP = TDB_NOMMAP
34 # Class representing a TDB file
36 class Tdb:
38 # Create and destroy Tdb objects
40 def __init__(self, name, hash_size = 0, flags = TDB_DEFAULT,
41 open_flags = os.O_RDWR | os.O_CREAT, mode = 0600):
42 self.tdb = tdb_open(name, hash_size, flags, open_flags, mode)
43 if self.tdb is None:
44 raise IOError, tdb_errorstr(self.tdb)
46 def __del__(self):
47 self.close()
49 def close(self):
50 if hasattr(self, 'tdb') and self.tdb is not None:
51 if tdb_close(self.tdb) == -1:
52 raise IOError, tdb_errorstr(self.tdb)
53 self.tdb = None
55 # Random access to keys, values
57 def __getitem__(self, key):
58 result = tdb_fetch(self.tdb, key)
59 if result is None:
60 raise KeyError, '%s: %s' % (key, tdb_errorstr(self.tdb))
61 return result
63 def __setitem__(self, key, item):
64 if tdb_store(self.tdb, key, item) == -1:
65 raise IOError, tdb_errorstr(self.tdb)
67 def __delitem__(self, key):
68 if not tdb_exists(self.tdb, key):
69 raise KeyError, '%s: %s' % (key, tdb_errorstr(self.tdb))
70 tdb_delete(self.tdb, key)
72 def has_key(self, key):
73 return tdb_exists(self.tdb, key)
75 # Tdb iterator
77 class TdbIterator:
78 def __init__(self, tdb):
79 self.tdb = tdb
80 self.key = None
82 def __iter__(self):
83 return self
85 def next(self):
86 if self.key is None:
87 self.key = tdb_firstkey(self.tdb)
88 if self.key is None:
89 raise StopIteration
90 return self.key
91 else:
92 self.key = tdb_nextkey(self.tdb, self.key)
93 if self.key is None:
94 raise StopIteration
95 return self.key
97 def __iter__(self):
98 return Tdb.TdbIterator(self.tdb)
100 # Implement other dict functions using TdbIterator
102 def keys(self):
103 return [k for k in iter(self)]
105 def values(self):
106 return [self[k] for k in iter(self)]
108 def items(self):
109 return [(k, self[k]) for k in iter(self)]
111 def __len__(self):
112 return len(self.keys())
114 def clear(self):
115 for k in iter(self):
116 del(self[k])