ldap: Fixed iteration errors from dynldap library.
This commit is contained in:
parent
ac6c8de81b
commit
f33820a1f5
1 changed files with 35 additions and 18 deletions
|
@ -312,6 +312,19 @@ class Connection:
|
||||||
|
|
||||||
class LdapResult:
|
class LdapResult:
|
||||||
|
|
||||||
|
class Iterator:
|
||||||
|
def __init__(self, result):
|
||||||
|
self._result = result
|
||||||
|
self._position = -1
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
try:
|
||||||
|
r = self._result[self._position + 1]
|
||||||
|
self._position = self._position + 1
|
||||||
|
except IndexError:
|
||||||
|
raise StopIteration
|
||||||
|
return r
|
||||||
|
|
||||||
def __init__(self, conn, msgid, entryclass = LdapEntry):
|
def __init__(self, conn, msgid, entryclass = LdapEntry):
|
||||||
self._conn = conn
|
self._conn = conn
|
||||||
self._msgid = msgid
|
self._msgid = msgid
|
||||||
|
@ -321,43 +334,47 @@ class LdapResult:
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
try:
|
try:
|
||||||
self._conn._conn.abandon(self._msgid)
|
self._conn._conn.abandon(self._msgid)
|
||||||
except ldap.LDAPError:
|
except (ldap.LDAPError, TypeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __getitem__(self, idx):
|
def __getitem__(self, idx):
|
||||||
while len(self._entries)-1 < idx:
|
while len(self._entries)-1 < idx:
|
||||||
try:
|
try:
|
||||||
self.next()
|
self._next()
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
raise IndexError('list index out of range')
|
raise IndexError('list index out of range')
|
||||||
return self._entries[idx]
|
return self._entries[idx]
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self
|
return self.Iterator(self)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
try:
|
while True:
|
||||||
self.next()
|
try:
|
||||||
except StopIteration:
|
self._next()
|
||||||
pass
|
except StopIteration:
|
||||||
|
break
|
||||||
return len(self._entries)
|
return len(self._entries)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.keys())
|
return str(self.keys())
|
||||||
|
|
||||||
|
def _next(self):
|
||||||
|
if self._msgid is None:
|
||||||
|
raise StopIteration
|
||||||
|
try:
|
||||||
|
(rtype, entry) = self._conn._conn.result(self._msgid, all=0,
|
||||||
|
timeout=-1)
|
||||||
|
if rtype == ldap.RES_SEARCH_RESULT:
|
||||||
|
self._msgid = None
|
||||||
|
raise StopIteration
|
||||||
|
except IndexError:
|
||||||
|
raise StopIteration
|
||||||
|
self._entries.append(self._entryclass(self._conn, entry[0]))
|
||||||
|
return self._entries[len(self._entries)-1]
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
r = []
|
r = []
|
||||||
for entry in self:
|
for entry in self:
|
||||||
r.append(entry.dn)
|
r.append(entry.dn)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def next(self):
|
|
||||||
try:
|
|
||||||
(rtype, entry) = self._conn._conn.result(self._msgid, all=0,
|
|
||||||
timeout=-1)
|
|
||||||
if rtype == ldap.RES_SEARCH_RESULT:
|
|
||||||
raise StopIteration
|
|
||||||
except IndexError:
|
|
||||||
raise StopIteration
|
|
||||||
self._entries.append(self._entryclass(self._conn, entry[0]))
|
|
||||||
return self._entries[len(self._entries)-1]
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue