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.
29 CLEAR_IF_FIRST
= TDB_CLEAR_IF_FIRST
30 INTERNAL
= TDB_INTERNAL
34 # Class representing a TDB file
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
)
44 raise IOError, tdb_errorstr(self
.tdb
)
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
)
55 # Random access to keys, values
57 def __getitem__(self
, key
):
58 result
= tdb_fetch(self
.tdb
, key
)
60 raise KeyError, '%s: %s' % (key
, tdb_errorstr(self
.tdb
))
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
)
78 def __init__(self
, tdb
):
87 self
.key
= tdb_firstkey(self
.tdb
)
92 self
.key
= tdb_nextkey(self
.tdb
, self
.key
)
98 return Tdb
.TdbIterator(self
.tdb
)
100 # Implement other dict functions using TdbIterator
103 return [k
for k
in iter(self
)]
106 return [self
[k
] for k
in iter(self
)]
109 return [(k
, self
[k
]) for k
in iter(self
)]
112 return len(self
.keys())