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