1 | """Provide a more Pythonic and object-oriented interface to tdb."""
|
---|
2 |
|
---|
3 | #
|
---|
4 | # Swig interface to Samba
|
---|
5 | #
|
---|
6 | # Copyright (C) Tim Potter 2006
|
---|
7 | #
|
---|
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.
|
---|
12 | #
|
---|
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.
|
---|
17 | #
|
---|
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.
|
---|
21 | #
|
---|
22 |
|
---|
23 | import os
|
---|
24 | from tdb import *
|
---|
25 |
|
---|
26 | # Open flags
|
---|
27 |
|
---|
28 | DEFAULT = TDB_DEFAULT
|
---|
29 | CLEAR_IF_FIRST = TDB_CLEAR_IF_FIRST
|
---|
30 | INTERNAL = TDB_INTERNAL
|
---|
31 | NOLOCK = TDB_NOLOCK
|
---|
32 | NOMMAP = TDB_NOMMAP
|
---|
33 |
|
---|
34 | # Class representing a TDB file
|
---|
35 |
|
---|
36 | class Tdb:
|
---|
37 |
|
---|
38 | # Create and destroy Tdb objects
|
---|
39 |
|
---|
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)
|
---|
45 |
|
---|
46 | def __del__(self):
|
---|
47 | self.close()
|
---|
48 |
|
---|
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
|
---|
54 |
|
---|
55 | # Random access to keys, values
|
---|
56 |
|
---|
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
|
---|
62 |
|
---|
63 | def __setitem__(self, key, item):
|
---|
64 | if tdb_store(self.tdb, key, item) == -1:
|
---|
65 | raise IOError, tdb_errorstr(self.tdb)
|
---|
66 |
|
---|
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)
|
---|
71 |
|
---|
72 | def has_key(self, key):
|
---|
73 | return tdb_exists(self.tdb, key)
|
---|
74 |
|
---|
75 | # Tdb iterator
|
---|
76 |
|
---|
77 | class TdbIterator:
|
---|
78 | def __init__(self, tdb):
|
---|
79 | self.tdb = tdb
|
---|
80 | self.key = None
|
---|
81 |
|
---|
82 | def __iter__(self):
|
---|
83 | return self
|
---|
84 |
|
---|
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
|
---|
96 |
|
---|
97 | def __iter__(self):
|
---|
98 | return Tdb.TdbIterator(self.tdb)
|
---|
99 |
|
---|
100 | # Implement other dict functions using TdbIterator
|
---|
101 |
|
---|
102 | def keys(self):
|
---|
103 | return [k for k in iter(self)]
|
---|
104 |
|
---|
105 | def values(self):
|
---|
106 | return [self[k] for k in iter(self)]
|
---|
107 |
|
---|
108 | def items(self):
|
---|
109 | return [(k, self[k]) for k in iter(self)]
|
---|
110 |
|
---|
111 | def __len__(self):
|
---|
112 | return len(self.keys())
|
---|
113 |
|
---|
114 | def clear(self):
|
---|
115 | for k in iter(self):
|
---|
116 | del(self[k])
|
---|