ldap.Connection.walk(): Support decode & unroll

The entry instance passed to the walk callback contains raw results
of LDAP search operations, i.e. all attribute values are lists, and
all attribute values are bytes. Add the boolean parameters decode and
unroll to walk() as a convenience method to get decoded values. They
default to False, representing current behaviour.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-03-31 17:28:38 +02:00
commit bdb7127d27

View file

@ -138,7 +138,8 @@ class Connection: # export
slog(ERR, f'Failed to delete {dn} ({e})')
raise
def walk(self,
def walk(
self,
callback: Callable[[Self, Any, Any], None],
base: str,
scope,
@ -149,7 +150,10 @@ class Connection: # export
serverctrls=None,
clientctrls=None,
timeout=-1,
sizelimit=0):
sizelimit=0,
decode: bool=False,
unroll: bool=False
):
# TODO: Support ignored arguments
search_return = self.__ldap.search(base=base,
@ -164,6 +168,10 @@ class Connection: # export
if result_type != ldap.RES_SEARCH_ENTRY:
continue
for entry in result_data:
if decode:
entry = entry[0], {key: [val.decode() for val in vals] for key, vals in entry[1].items()}
if unroll and False:
entry = entry[0], {key: val[0] for key, val in entry[1].items()}
try:
callback(self, entry, context)
except Exception as e: