comparison sat/tools/utils.py @ 3537:f9a5b810f14d

core (memory/storage): backend storage is now based on SQLAlchemy
author Goffi <goffi@goffi.org>
date Thu, 03 Jun 2021 15:20:47 +0200
parents be6d91572633
children aa58451b77ba
comparison
equal deleted inserted replaced
3536:0985c47ffd96 3537:f9a5b810f14d
26 import sys 26 import sys
27 import random 27 import random
28 import inspect 28 import inspect
29 import textwrap 29 import textwrap
30 import functools 30 import functools
31 from asyncio import iscoroutine 31 import asyncio
32 from twisted.python import procutils, failure 32 from twisted.python import procutils, failure
33 from twisted.internet import defer 33 from twisted.internet import defer
34 from sat.core.constants import Const as C 34 from sat.core.constants import Const as C
35 from sat.core.log import getLogger 35 from sat.core.log import getLogger
36 36
100 try: 100 try:
101 ret = func(*args, **kwargs) 101 ret = func(*args, **kwargs)
102 except Exception as e: 102 except Exception as e:
103 return defer.fail(failure.Failure(e)) 103 return defer.fail(failure.Failure(e))
104 else: 104 else:
105 if iscoroutine(ret): 105 if asyncio.iscoroutine(ret):
106 return defer.ensureDeferred(ret) 106 return defer.ensureDeferred(ret)
107 elif isinstance(ret, defer.Deferred): 107 elif isinstance(ret, defer.Deferred):
108 return ret 108 return ret
109 elif isinstance(ret, failure.Failure): 109 elif isinstance(ret, failure.Failure):
110 return defer.fail(ret) 110 return defer.fail(ret)
111 else: 111 else:
112 return defer.succeed(ret) 112 return defer.succeed(ret)
113
114
115 def aio(func):
116 """Decorator to return a Deferred from asyncio coroutine
117
118 Functions with this decorator are run in asyncio context
119 """
120 def wrapper(*args, **kwargs):
121 return defer.Deferred.fromFuture(asyncio.ensure_future(func(*args, **kwargs)))
122 return wrapper
123
124
125 def as_future(d):
126 return d.asFuture(asyncio.get_event_loop())
113 127
114 128
115 def xmpp_date(timestamp=None, with_time=True): 129 def xmpp_date(timestamp=None, with_time=True):
116 """Return date according to XEP-0082 specification 130 """Return date according to XEP-0082 specification
117 131