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