tdb: Handle TDB_NEXT_LOCK_ERR in tdb_traverse_internal
[samba.git] / lib / tdb / _tdb_text.py
1 # Text wrapper for tdb bindings
2 #
3 # Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
4 # Published under the GNU LGPLv3 or later
5
6 import sys
7 import functools
8
9 import tdb
10
11
12 class TdbTextWrapper(object):
13     """Text interface for a TDB file"""
14
15     def __init__(self, tdb):
16         self._tdb = tdb
17
18     @property
19     def raw(self):
20         return self._tdb
21
22     def get(self, key):
23         key = key.encode('utf-8')
24         result = self._tdb.get(key)
25         if result is not None:
26             return result.decode('utf-8')
27
28     def append(self, key, value):
29         key = key.encode('utf-8')
30         value = value.encode('utf-8')
31         self._tdb.append(key, value)
32
33     def firstkey(self):
34         result = self._tdb.firstkey()
35         if result:
36             return result.decode('utf-8')
37
38     def nextkey(self, key):
39         key = key.encode('utf-8')
40         result = self._tdb.nextkey(key)
41         if result is not None:
42             return result.decode('utf-8')
43
44     def delete(self, key):
45         key = key.encode('utf-8')
46         self._tdb.delete(key)
47
48     def store(self, key, value):
49         key = key.encode('utf-8')
50         value = value.encode('utf-8')
51         self._tdb.store(key, value)
52
53     def __iter__(self):
54         for key in iter(self._tdb):
55             yield key.decode('utf-8')
56
57     def __getitem__(self, key):
58         key = key.encode('utf-8')
59         result = self._tdb[key]
60         return result.decode('utf-8')
61
62     def __contains__(self, key):
63         key = key.encode('utf-8')
64         return key in self._tdb
65
66     def __repr__(self):
67         return '<TdbTextWrapper for %r>' % self._tdb
68
69     def __setitem__(self, key, value):
70         key = key.encode('utf-8')
71         value = value.encode('utf-8')
72         self._tdb[key] = value
73
74     def __delitem__(self, key):
75         key = key.encode('utf-8')
76         del self._tdb[key]
77
78     if sys.version_info > (3, 0):
79         keys = __iter__
80     else:
81         iterkeys = __iter__
82         has_key = __contains__
83
84
85 ## Add wrappers for functions and getters that don't deal with text
86
87 def _add_wrapper(name):
88     orig = getattr(tdb.Tdb, name)
89
90     def wrapper(self, *args, **kwargs):
91         return orig(self._tdb, *args, **kwargs)
92     wrapper.__name__ = orig.__name__
93     wrapper.__doc__ = orig.__doc__
94
95     setattr(TdbTextWrapper, name, wrapper)
96
97 for name in ("transaction_cancel",
98              "transaction_commit",
99              "transaction_prepare_commit",
100              "transaction_start",
101              "reopen",
102              "lock_all",
103              "unlock_all",
104              "read_lock_all",
105              "read_unlock_all",
106              "close",
107              "add_flags",
108              "remove_flags",
109              "clear",
110              "repack",
111              "enable_seqnum",
112              "increment_seqnum_nonblock",
113             ):
114     _add_wrapper(name)
115
116
117 def _add_getter(name):
118     orig = getattr(tdb.Tdb, name)
119     doc = orig.__doc__
120
121     def getter(self):
122         return getattr(self._tdb, name)
123
124     def setter(self, value):
125         return setattr(self._tdb, name, value)
126
127     setattr(TdbTextWrapper, name, property(getter, setter, doc=doc))
128
129 for name in ("hash_size",
130              "map_size",
131              "freelist_size",
132              "flags",
133              "max_dead",
134              "filename",
135              "seqnum",
136              "text",
137             ):
138     _add_getter(name)