The result of search() should be an iterable returning entries. If we
want to use the asynchronous connection.search() and
connection.result(), that's fine, but we should handle the result
unpacking inside search().
One benefit to Wade's new generator approach is that we can start
formatting early entries while we wait for later entries to arrive.
CONFIG.get('connection', 'basedn'),
ldap.SCOPE_SUBTREE,
filterstr.encode('utf-8'))
CONFIG.get('connection', 'basedn'),
ldap.SCOPE_SUBTREE,
filterstr.encode('utf-8'))
+ res_type = None
+ while res_type != ldap.RES_SEARCH_RESULT:
+ try:
+ res_type, res_data = connection.result(
+ msg_id, all=False, timeout=0)
+ except ldap.ADMINLIMIT_EXCEEDED:
+ #print "Partial results"
+ break
+ if res_data:
+ # use `yield from res_data` in Python >= 3.3, see PEP 380
+ for entry in res_data:
+ yield entry
def format_columns(address, data):
yield address
def format_columns(address, data):
yield address
(cache_hit, addresses) = cache_lookup(query)
if not cache_hit:
(cache_hit, addresses) = cache_lookup(query)
if not cache_hit:
try:
connection = connect()
try:
connection = connect()
- msg_id = search(query, connection)
-
- # wacky, but allows partial results
- while True:
- try:
- res_type, res_data = connection.result(msg_id, 0)
- except ldap.ADMINLIMIT_EXCEEDED:
- #print "Partial results"
- break
- # last result will have this set
- if res_type == ldap.RES_SEARCH_RESULT:
- break
-
- addresses += [entry for entry in format_entry(res_data[-1])]
-
- # Cache results for next lookup
+ entries = search(query=query, connection=connection)
+ for entry in entries:
+ addresses.extend(format_entry(entry))
cache_persist(query, addresses)
finally:
if connection:
cache_persist(query, addresses)
finally:
if connection: