lib:talloc. Fix memory leak when destructors reparent children.
[Samba.git] / lib / tdb / _tdb_text.py
blobc823bf8e244cea996e78abfb63ba74db742c709e
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
7 import functools
9 import tdb
12 class TdbTextWrapper(object):
13 """Text interface for a TDB file"""
15 def __init__(self, tdb):
16 self._tdb = tdb
18 @property
19 def raw(self):
20 return self._tdb
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')
28 def append(self, key, value):
29 key = key.encode('utf-8')
30 value = value.encode('utf-8')
31 self._tdb.append(key, value)
33 def firstkey(self):
34 result = self._tdb.firstkey()
35 if result:
36 return result.decode('utf-8')
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')
44 def delete(self, key):
45 key = key.encode('utf-8')
46 self._tdb.delete(key)
48 def store(self, key, value):
49 key = key.encode('utf-8')
50 value = value.encode('utf-8')
51 self._tdb.store(key, value)
53 def __iter__(self):
54 for key in iter(self._tdb):
55 yield key.decode('utf-8')
57 def __getitem__(self, key):
58 key = key.encode('utf-8')
59 result = self._tdb[key]
60 return result.decode('utf-8')
62 def __contains__(self, key):
63 key = key.encode('utf-8')
64 return key in self._tdb
66 def __repr__(self):
67 return '<TdbTextWrapper for %r>' % self._tdb
69 def __setitem__(self, key, value):
70 key = key.encode('utf-8')
71 value = value.encode('utf-8')
72 self._tdb[key] = value
74 def __delitem__(self, key):
75 key = key.encode('utf-8')
76 del self._tdb[key]
78 if sys.version_info > (3, 0):
79 keys = __iter__
80 else:
81 iterkeys = __iter__
82 has_key = __contains__
85 ## Add wrappers for functions and getters that don't deal with text
87 def _add_wrapper(name):
88 orig = getattr(tdb.Tdb, name)
90 def wrapper(self, *args, **kwargs):
91 return orig(self._tdb, *args, **kwargs)
92 wrapper.__name__ = orig.__name__
93 wrapper.__doc__ = orig.__doc__
95 setattr(TdbTextWrapper, name, wrapper)
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",
114 _add_wrapper(name)
117 def _add_getter(name):
118 orig = getattr(tdb.Tdb, name)
119 doc = orig.__doc__
121 def getter(self):
122 return getattr(self._tdb, name)
124 def setter(self, value):
125 return setattr(self._tdb, name, value)
127 setattr(TdbTextWrapper, name, property(getter, setter, doc=doc))
129 for name in ("hash_size",
130 "map_size",
131 "freelist_size",
132 "flags",
133 "max_dead",
134 "filename",
135 "seqnum",
136 "text",
138 _add_getter(name)