summaryrefslogtreecommitdiff
path: root/slixmpp/xmlstream
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2021-01-31 17:01:41 +0100
committermathieui <mathieui@mathieui.net>2021-01-31 17:01:41 +0100
commit7932a03378f9774b0a133ce71ce65e732b5d3994 (patch)
treeaecc4f147477255d00d02d80897b63cce5612145 /slixmpp/xmlstream
parente9b07e32bb44464d71371eff8b93be9635777bbd (diff)
downloadslixmpp-7932a03378f9774b0a133ce71ce65e732b5d3994.tar.gz
slixmpp-7932a03378f9774b0a133ce71ce65e732b5d3994.tar.bz2
slixmpp-7932a03378f9774b0a133ce71ce65e732b5d3994.tar.xz
slixmpp-7932a03378f9774b0a133ce71ce65e732b5d3994.zip
xmlstream: add a simple contextmanager for temporary events
Diffstat (limited to 'slixmpp/xmlstream')
-rw-r--r--slixmpp/xmlstream/xmlstream.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py
index 5074aa8c..b80c55d3 100644
--- a/slixmpp/xmlstream/xmlstream.py
+++ b/slixmpp/xmlstream/xmlstream.py
@@ -30,7 +30,7 @@ import weakref
import uuid
from asyncio import iscoroutinefunction, wait, Future
-
+from contextlib import contextmanager
import xml.etree.ElementTree as ET
from slixmpp.xmlstream.asyncio import asyncio
@@ -1208,3 +1208,16 @@ class XMLStream(asyncio.BaseProtocol):
disposable=True,
)
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)