s3:include: Remove trailing whitepaces in MacExtensions.h
[Samba.git] / lib / ldb / _ldb_text.py
blobe0505e177913256c0d9cf86c58d535675cb32c61
1 # Text wrapper for ldb bindings
3 # Copyright (C) 2015 Petr Viktorin <pviktori@redhat.com>
4 # Published under the GNU LGPLv3 or later
6 import ldb
9 def _recursive_encode(obj):
10 if isinstance(obj, bytes):
11 return obj
12 elif isinstance(obj, str):
13 return obj.encode('utf-8')
14 else:
15 return [_recursive_encode(o) for o in obj]
18 class _WrapBase(object):
20 @classmethod
21 def _wrap(cls, wrapped):
22 self = cls.__new__(cls)
23 self._wrapped = wrapped
24 return self
26 def __len__(self):
27 return len(self._wrapped)
29 def __eq__(self, other):
30 if hasattr(other, '_wrapped'):
31 return self._wrapped == other._wrapped
32 else:
33 return self._wrapped == other
35 def __ne__(self, other):
36 if hasattr(other, '_wrapped'):
37 return self._wrapped != other._wrapped
38 else:
39 return self._wrapped != other
41 def __lt__(self, other):
42 if hasattr(other, '_wrapped'):
43 return self._wrapped < other._wrapped
44 else:
45 return self._wrapped < other
47 def __le__(self, other):
48 if hasattr(other, '_wrapped'):
49 return self._wrapped >= other._wrapped
50 else:
51 return self._wrapped >= other
53 def __gt__(self, other):
54 if hasattr(other, '_wrapped'):
55 return self._wrapped > other._wrapped
56 else:
57 return self._wrapped > other
59 def __ge__(self, other):
60 if hasattr(other, '_wrapped'):
61 return self._wrapped >= other._wrapped
62 else:
63 return self._wrapped >= other
65 def __repr__(self):
66 return '%s.text' % repr(self._wrapped)
69 class MessageElementTextWrapper(_WrapBase):
71 """Text interface for a LDB message element"""
73 def __iter__(self):
74 for item in self._wrapped:
75 yield item.decode('utf-8')
77 def __getitem__(self, key):
78 result = self._wrapped[key]
79 if result is None:
80 return None
81 else:
82 return result.decode('utf-8')
84 @property
85 def flags(self):
86 return self._wrapped.flags
88 @property
89 def set_flags(self):
90 return self._wrapped.set_flags
93 _wrap_element = MessageElementTextWrapper._wrap
96 class MessageTextWrapper(_WrapBase):
98 """Text interface for a LDB message"""
100 def __getitem__(self, key):
101 result = self._wrapped[key]
102 if result is None:
103 return None
104 else:
105 return _wrap_element(result)
107 def get(self, *args, **kwargs):
108 result = self._wrapped.get(*args, **kwargs)
109 if isinstance(result, ldb.MessageElement):
110 return _wrap_element(result)
111 elif isinstance(result, bytes):
112 return result.decode('utf-8')
113 else:
114 return result
116 def __setitem__(self, key, item):
117 self._wrapped[key] = _recursive_encode(item)
119 def __delitem__(self, key):
120 del self._wrapped[key]
122 def elements(self):
123 return [_wrap_element(el) for el in self._wrapped.elements()]
125 def items(self):
126 return [(attr, _wrap_element(el)) for attr, el in self._wrapped.items()]
128 @property
129 def keys(self):
130 return self._wrapped.keys
132 @property
133 def remove(self):
134 return self._wrapped.remove
136 @property
137 def add(self):
138 return self._wrapped.add
140 @property
141 def dn(self):
142 return self._wrapped.dn
144 @dn.setter
145 def dn(self, new_value):
146 self._wrapped.dn = new_value