summaryrefslogtreecommitdiff
path: root/source4/lib/tdb/tdb.py
blob: a8c1d06e0d9717d731ce4c348dc13173d0def648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 1.3.35
#
# Don't modify this file, modify the SWIG interface instead.

"""
TDB is a simple key-value database similar to GDBM that supports multiple writers.
"""

import _tdb
import new
new_instancemethod = new.instancemethod
try:
    _swig_property = property
except NameError:
    pass # Python < 2.2 doesn't have 'property'.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
    if (name == "thisown"): return self.this.own(value)
    if (name == "this"):
        if type(value).__name__ == 'PySwigObject':
            self.__dict__[name] = value
            return
    method = class_type.__swig_setmethods__.get(name,None)
    if method: return method(self,value)
    if (not static) or hasattr(self,name):
        self.__dict__[name] = value
    else:
        raise AttributeError("You cannot add attributes to %s" % self)

def _swig_setattr(self,class_type,name,value):
    return _swig_setattr_nondynamic(self,class_type,name,value,0)

def _swig_getattr(self,class_type,name):
    if (name == "thisown"): return self.this.own()
    method = class_type.__swig_getmethods__.get(name,None)
    if method: return method(self)
    raise AttributeError,name

def _swig_repr(self):
    try: strthis = "proxy of " + self.this.__repr__()
    except: strthis = ""
    return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)

import types
try:
    _object = types.ObjectType
    _newclass = 1
except AttributeError:
    class _object : pass
    _newclass = 0
del types


def _swig_setattr_nondynamic_method(set):
    def set_attr(self,name,value):
        if (name == "thisown"): return self.this.own(value)
        if hasattr(self,name) or (name == "this"):
            set(self,name,value)
        else:
            raise AttributeError("You cannot add attributes to %s" % self)
    return set_attr


REPLACE = _tdb.REPLACE
INSERT = _tdb.INSERT
MODIFY = _tdb.MODIFY
DEFAULT = _tdb.DEFAULT
CLEAR_IF_FIRST = _tdb.CLEAR_IF_FIRST
INTERNAL = _tdb.INTERNAL
NOLOCK = _tdb.NOLOCK
NOMMAP = _tdb.NOMMAP
CONVERT = _tdb.CONVERT
BIGENDIAN = _tdb.BIGENDIAN
TDB_SUCCESS = _tdb.TDB_SUCCESS
TDB_ERR_CORRUPT = _tdb.TDB_ERR_CORRUPT
TDB_ERR_IO = _tdb.TDB_ERR_IO
TDB_ERR_LOCK = _tdb.TDB_ERR_LOCK
TDB_ERR_OOM = _tdb.TDB_ERR_OOM
TDB_ERR_EXISTS = _tdb.TDB_ERR_EXISTS
TDB_ERR_NOLOCK = _tdb.TDB_ERR_NOLOCK
TDB_ERR_LOCK_TIMEOUT = _tdb.TDB_ERR_LOCK_TIMEOUT
TDB_ERR_NOEXIST = _tdb.TDB_ERR_NOEXIST
TDB_ERR_EINVAL = _tdb.TDB_ERR_EINVAL
TDB_ERR_RDONLY = _tdb.TDB_ERR_RDONLY
class Tdb(object):
    """A TDB file."""
    thisown = _swig_property(lambda x: x.this.own(), lambda x, v: x.this.own(v), doc='The membership flag')
    __repr__ = _swig_repr
    def __init__(self, *args, **kwargs): 
        """
        S.__init__(name,hash_size=0,tdb_flags=TDB_DEFAULT,flags=O_RDWR,mode=0600)
        Open a TDB file.
        """
        _tdb.Tdb_swiginit(self,_tdb.new_Tdb(*args, **kwargs))
    __swig_destroy__ = _tdb.delete_Tdb
    def close(*args, **kwargs):
        """
        S.close() -> None
        Close the TDB file.
        """
        return _tdb.Tdb_close(*args, **kwargs)

    def errorstr(*args, **kwargs):
        """
        S.errorstr() -> errorstring
        Obtain last error message.
        """
        return _tdb.Tdb_errorstr(*args, **kwargs)

    def get(*args, **kwargs):
        """
        S.fetch(key) -> value
        Fetch a value.
        """
        return _tdb.Tdb_get(*args, **kwargs)

    def delete(*args, **kwargs):
        """
        S.delete(key) -> None
        Delete an entry.
        """
        return _tdb.Tdb_delete(*args, **kwargs)

    def store(*args, **kwargs):
        """
        S.store(key, value, flag=TDB_REPLACE) -> None
        Store an entry.
        """
        return _tdb.Tdb_store(*args, **kwargs)

    def firstkey(*args, **kwargs):
        """
        S.firstkey() -> data
        Return the first key in this database.
        """
        return _tdb.Tdb_firstkey(*args, **kwargs)

    def nextkey(*args, **kwargs):
        """
        S.nextkey(prev) -> data
        Return the next key in this database.
        """
        return _tdb.Tdb_nextkey(*args, **kwargs)

    def lock_all(*args, **kwargs):
        """S.lockall() -> bool"""
        return _tdb.Tdb_lock_all(*args, **kwargs)

    def unlock_all(*args, **kwargs):
        """S.unlockall() -> bool"""
        return _tdb.Tdb_unlock_all(*args, **kwargs)

    def reopen(*args, **kwargs):
        """
        S.reopen() -> bool
        Reopen this file.
        """
        return _tdb.Tdb_reopen(*args, **kwargs)

    def transaction_start(*args, **kwargs):
        """
        S.transaction_start() -> None
        Start a new transaction.
        """
        return _tdb.Tdb_transaction_start(*args, **kwargs)

    def transaction_commit(*args, **kwargs):
        """
        S.transaction_commit() -> None
        Commit the currently active transaction.
        """
        return _tdb.Tdb_transaction_commit(*args, **kwargs)

    def transaction_cancel(*args, **kwargs):
        """
        S.transaction_cancel() -> None
        Cancel the currently active transaction.
        """
        return _tdb.Tdb_transaction_cancel(*args, **kwargs)

    def hash_size(*args, **kwargs):
        """S.hash_size() -> int"""
        return _tdb.Tdb_hash_size(*args, **kwargs)

    def map_size(*args, **kwargs):
        """S.map_size() -> int"""
        return _tdb.Tdb_map_size(*args, **kwargs)

    def get_flags(*args, **kwargs):
        """S.get_flags() -> int"""
        return _tdb.Tdb_get_flags(*args, **kwargs)

    def set_max_dead(*args, **kwargs):
        """S.set_max_dead(int) -> None"""
        return _tdb.Tdb_set_max_dead(*args, **kwargs)

    def name(*args, **kwargs):
        """
        S.name() -> path
        Return filename of this TDB file.
        """
        return _tdb.Tdb_name(*args, **kwargs)

    def __repr__(self):
        return "Tdb('%s')" % self.name()


    def __getitem__(self, key):
        result = self.get(key)
        if result is None:
            raise KeyError, '%s: %s' % (key, self.errorstr())
        return result

    def __setitem__(self, key, item):
        if self.store(key, item) == -1:
            raise IOError, self.errorstr()

    def __delitem__(self, key):
        if not self.exists(key):
            raise KeyError, '%s: %s' % (key, self.errorstr())
        self.delete(key)

    def __contains__(self, key):
        return self.exists(key) != 0

    def has_key(self, key):
        return self.exists(key) != 0

    def fetch_uint32(self, key):
        data = self.get(key)
        if data is None:
            return None
        import struct
        return struct.unpack("<L", data)[0]

    def fetch_int32(self, key):
        data = self.get(key)
        if data is None:
            return None
        import struct
        return struct.unpack("<l", data)[0]


    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 = self.tdb.firstkey()
                if self.key is None:
                    raise StopIteration
                return self.key
            else:
                self.key = self.tdb.nextkey(self.key)
                if self.key is None:
                    raise StopIteration
                return self.key

    def __iter__(self):
        return self.TdbIterator(self)



    def keys(self):
        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])

    def iterkeys(self):
        for k in iter(self):
            yield k
       
    def itervalues(self):
        for k in iter(self):
            yield self[k]

    def iteritems(self):
        for k in iter(self):
            yield (k, self[k])



Tdb.error = new_instancemethod(_tdb.Tdb_error,None,Tdb)
Tdb.close = new_instancemethod(_tdb.Tdb_close,None,Tdb)
Tdb.append = new_instancemethod(_tdb.Tdb_append,None,Tdb)
Tdb.errorstr = new_instancemethod(_tdb.Tdb_errorstr,None,Tdb)
Tdb.get = new_instancemethod(_tdb.Tdb_get,None,Tdb)
Tdb.delete = new_instancemethod(_tdb.Tdb_delete,None,Tdb)
Tdb.store = new_instancemethod(_tdb.Tdb_store,None,Tdb)
Tdb.exists = new_instancemethod(_tdb.Tdb_exists,None,Tdb)
Tdb.firstkey = new_instancemethod(_tdb.Tdb_firstkey,None,Tdb)
Tdb.nextkey = new_instancemethod(_tdb.Tdb_nextkey,None,Tdb)
Tdb.lock_all = new_instancemethod(_tdb.Tdb_lock_all,None,Tdb)
Tdb.unlock_all = new_instancemethod(_tdb.Tdb_unlock_all,None,Tdb)
Tdb.read_lock_all = new_instancemethod(_tdb.Tdb_read_lock_all,None,Tdb)
Tdb.read_unlock_all = new_instancemethod(_tdb.Tdb_read_unlock_all,None,Tdb)
Tdb.reopen = new_instancemethod(_tdb.Tdb_reopen,None,Tdb)
Tdb.transaction_start = new_instancemethod(_tdb.Tdb_transaction_start,None,Tdb)
Tdb.transaction_commit = new_instancemethod(_tdb.Tdb_transaction_commit,None,Tdb)
Tdb.transaction_cancel = new_instancemethod(_tdb.Tdb_transaction_cancel,None,Tdb)
Tdb.transaction_recover = new_instancemethod(_tdb.Tdb_transaction_recover,None,Tdb)
Tdb.hash_size = new_instancemethod(_tdb.Tdb_hash_size,None,Tdb)
Tdb.map_size = new_instancemethod(_tdb.Tdb_map_size,None,Tdb)
Tdb.get_flags = new_instancemethod(_tdb.Tdb_get_flags,None,Tdb)
Tdb.set_max_dead = new_instancemethod(_tdb.Tdb_set_max_dead,None,Tdb)
Tdb.name = new_instancemethod(_tdb.Tdb_name,None,Tdb)
Tdb_swigregister = _tdb.Tdb_swigregister
Tdb_swigregister(Tdb)