| 1 | from typing import Any, Optional, Union |
| 2 | from gevent.socket import wait_readwrite |
| 3 | |
| 4 | from ..ldapconnection import BaseLDAPConnection, LDAPSearchScope |
| 5 | from ..ldapdn import LDAPDN |
| 6 | from ..errors import NotAllowedOnNonleaf |
| 7 | |
| 8 | MYPY = False |
| 9 | |
| 10 | if MYPY: |
| 11 | from ..ldapclient import LDAPClient |
| 12 | |
| 13 | class GeventLDAPConnection(BaseLDAPConnection): |
| 14 | """ |
| 15 | Asynchronous LDAP connection object that works with Gevent. |
| 16 | It has the same methods and properties as :class:`bonsai.LDAPConnection`. |
| 17 | |
| 18 | :param LDAPClient client: a client object. |
| 19 | """ |
| 20 | |
| 21 | def __init__(self, client: 'LDAPClient') -> None: |
| 22 | super().__init__(client, is_async=True) |
| 23 | |
| 24 | def _poll(self, msg_id: int, timeout: Optional[float] = None) -> Any: |
| 25 | while True: |
| 26 | res = self.get_result(msg_id) |
| 27 | if res is not None: |
| 28 | return res |
| 29 | wait_readwrite(self.fileno(), timeout=timeout) |
| 30 | |
| 31 | def _evaluate(self, msg_id: int, timeout: Optional[float] = None) -> Any: |
| 32 | return self._poll(msg_id, timeout) |
| 33 | |
| 34 | def delete(self, dname: Union[str, LDAPDN], timeout: Optional[float] = None, |
| 35 | recursive: bool = False) -> bool: |
| 36 | try: |
| 37 | return super().delete(dname, timeout, recursive) |
| 38 | except NotAllowedOnNonleaf as exc: |
| 39 | if recursive: |
| 40 | results = self.search(dname, LDAPSearchScope.ONELEVEL, |
| 41 | attrlist=['1.1'], timeout=timeout) |
| 42 | for res in results: |
| 43 | self.delete(res.dn, timeout, True) |
| 44 | return self.delete(dname, timeout, False) |
| 45 | else: |
| 46 | raise exc |