From 893e8e7a9664e1ce53ae0c33ac407f7f99fb1131 Mon Sep 17 00:00:00 2001 From: Goffi Date: Thu, 3 Jun 2021 15:21:43 +0200 Subject: [PATCH] tools (utils): new `ensure_deferred` decorator --- sat/tools/utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sat/tools/utils.py b/sat/tools/utils.py index 199b183e..2e71d9a9 100644 --- a/sat/tools/utils.py +++ b/sat/tools/utils.py @@ -126,6 +126,17 @@ def as_future(d): return d.asFuture(asyncio.get_event_loop()) +def ensure_deferred(func): + """Decorator to apply ensureDeferred to a function + + to be used when the function is called by third party library (e.g. wokkel) + Otherwise, it's better to use ensureDeferred as early as possible. + """ + def wrapper(*args, **kwargs): + return defer.ensureDeferred(func(*args, **kwargs)) + return wrapper + + def xmpp_date(timestamp=None, with_time=True): """Return date according to XEP-0082 specification