| 1 |
import asyncio |
| 2 |
|
| 3 |
from ..ldapconnection import BaseLDAPConnection, LDAPSearchScope |
| 4 |
from ..errors import LDAPError, NotAllowedOnNonleaf |
| 5 |
|
| 6 |
|
| 7 |
class AIOLDAPConnection(BaseLDAPConnection): |
| 8 |
""" |
| 9 |
Asynchronous LDAP connection object that works with asyncio. |
| 10 |
It has the same methods and properties as :class:`bonsai.LDAPConnection`, but |
| 11 |
with the exception of :meth:`bonsai.LDAPConnection.close` and |
| 12 |
:meth:`bonsai.LDAPConnection.fileno` all of them are awaitable. |
| 13 |
|
| 14 |
:param LDAPClient client: a client object. |
| 15 |
:param loop: an asyncio IO loop. |
| 16 |
""" |
| 17 |
|
| 18 |
def __init__(self, client, loop=None): |
| 19 |
self._loop = loop or asyncio.get_running_loop() |
| 20 |
self.__open_coro = None |
| 21 |
super().__init__(client, is_async=True) |
| 22 |
|
| 23 |
async def __aenter__(self): |
| 24 |
"""Async context manager entry point.""" |
| 25 |
return await self.__open_coro |
| 26 |
|
| 27 |
async def __aexit__(self, type, value, traceback): |
| 28 |
"""Async context manager exit point.""" |
| 29 |
self.close() |
| 30 |
|
| 31 |
def __await__(self): |
| 32 |
return self.__open_coro.__await__() |
| 33 |
|
| 34 |
__iter__ = __await__ |
| 35 |
|
| 36 |
def _ready(self, msg_id, fut): |
| 37 |
self._loop.remove_reader(self.fileno()) |
| 38 |
self._loop.remove_writer(self.fileno()) |
| 39 |
try: |
| 40 |
res = super().get_result(msg_id) |
| 41 |
if res is not None: |
| 42 |
fut.set_result(res) |
| 43 |
else: |
| 44 |
self._loop.add_reader(self.fileno(), self._ready, msg_id, fut) |
| 45 |
self._loop.add_writer(self.fileno(), self._ready, msg_id, fut) |
| 46 |
except LDAPError as exc: |
| 47 |
fut.set_exception(exc) |
| 48 |
|
| 49 |
async def _poll(self, msg_id, timeout=None): |
| 50 |
fut = asyncio.Future() |
| 51 |
self._loop.add_reader(self.fileno(), self._ready, msg_id, fut) |
| 52 |
self._loop.add_writer(self.fileno(), self._ready, msg_id, fut) |
| 53 |
try: |
| 54 |
return await asyncio.wait_for(fut, timeout) |
| 55 |
except Exception as exc: |
| 56 |
if self.fileno() > -1: |
| 57 |
self._loop.remove_reader(self.fileno()) |
| 58 |
self._loop.remove_writer(self.fileno()) |
| 59 |
raise exc |
| 60 |
|
| 61 |
def _evaluate(self, msg_id, timeout=None): |
| 62 |
return self._poll(msg_id, timeout) |
| 63 |
|
| 64 |
def open(self, timeout=None): |
| 65 |
self.__open_coro = super().open(timeout) |
| 66 |
return self |
| 67 |
|
| 68 |
async def delete(self, dname, timeout=None, recursive=False): |
| 69 |
try: |
| 70 |
return await super().delete(dname, timeout, recursive) |
| 71 |
except NotAllowedOnNonleaf as exc: |
| 72 |
if recursive: |
| 73 |
results = await self.search( |
| 74 |
dname, LDAPSearchScope.ONELEVEL, attrlist=["1.1"], timeout=timeout |
| 75 |
) |
| 76 |
for res in results: |
| 77 |
await self.delete(res.dn, timeout, True) |
| 78 |
return await self.delete(dname, timeout, False) |
| 79 |
else: |
| 80 |
raise exc |
| 81 |
|
| 82 |
async def _search_iter_anext(self, search_iter): |
| 83 |
try: |
| 84 |
return next(search_iter) |
| 85 |
except StopIteration: |
| 86 |
msgid = search_iter.acquire_next_page() |
| 87 |
if msgid is None: |
| 88 |
raise StopAsyncIteration from None |
| 89 |
search_iter = await self._evaluate(msgid) |
| 90 |
return next(search_iter) |
| 91 |
|
| 92 |
async def get_result(self, msg_id, timeout=None): |
| 93 |
return await self._evaluate(msg_id, timeout) |