1 # Text wrapper for tdb bindings
3 # Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
4 # Published under the GNU LGPLv3 or later
11 class TdbTextWrapper(object):
12 """Text interface for a TDB file"""
14 def __init__(self
, tdb
):
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
)
33 result
= self
._tdb
.firstkey()
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')
47 def store(self
, key
, value
):
48 key
= key
.encode('utf-8')
49 value
= value
.encode('utf-8')
50 self
._tdb
.store(key
, value
)
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
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')
77 if sys
.version_info
> (3, 0):
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",
98 "transaction_prepare_commit",
111 "increment_seqnum_nonblock",
116 def _add_getter(name
):
117 orig
= getattr(tdb
.Tdb
, name
)
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",