xmlstream: add a simple contextmanager for temporary events

This commit is contained in:
mathieui 2021-01-31 17:01:41 +01:00
parent e9b07e32bb
commit 7932a03378

View File

@ -30,7 +30,7 @@ import weakref
import uuid import uuid
from asyncio import iscoroutinefunction, wait, Future from asyncio import iscoroutinefunction, wait, Future
from contextlib import contextmanager
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from slixmpp.xmlstream.asyncio import asyncio from slixmpp.xmlstream.asyncio import asyncio
@ -1208,3 +1208,16 @@ class XMLStream(asyncio.BaseProtocol):
disposable=True, disposable=True,
) )
return await asyncio.wait_for(fut, timeout, loop=self.loop) return await asyncio.wait_for(fut, timeout, loop=self.loop)
@contextmanager
def event_handler(self, event: str, handler: Callable):
"""
Context manager that adds then removes an event handler.
"""
self.add_event_handler(event, handler)
try:
yield
except Exception as exc:
raise
finally:
self.del_event_handler(event, handler)