2018-06-25 15:24:56 +03:00
|
|
|
"""
|
|
|
|
This magical module will rewrite all public methods in the public interface
|
|
|
|
of the library so they can run the loop on their own if it's not already
|
|
|
|
running. This rewrite may not be desirable if the end user always uses the
|
|
|
|
methods they way they should be ran, but it's incredibly useful for quick
|
|
|
|
scripts and the runtime overhead is relatively low.
|
|
|
|
|
|
|
|
Some really common methods which are hardly used offer this ability by
|
|
|
|
default, such as ``.start()`` and ``.run_until_disconnected()`` (since
|
|
|
|
you may want to start, and then run until disconnected while using async
|
|
|
|
event handlers).
|
|
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
import functools
|
|
|
|
import inspect
|
|
|
|
|
2018-06-27 10:34:35 +03:00
|
|
|
from async_generator import isasyncgenfunction
|
|
|
|
|
2018-06-25 15:24:56 +03:00
|
|
|
from .client.telegramclient import TelegramClient
|
2018-08-01 02:06:47 +03:00
|
|
|
from .tl.custom import (
|
2018-08-05 12:05:01 +03:00
|
|
|
Draft, Dialog, MessageButton, Forward, Message, InlineResult, Conversation
|
2018-08-01 02:06:47 +03:00
|
|
|
)
|
2018-07-10 16:15:22 +03:00
|
|
|
from .tl.custom.chatgetter import ChatGetter
|
|
|
|
from .tl.custom.sendergetter import SenderGetter
|
2018-06-25 15:24:56 +03:00
|
|
|
|
|
|
|
|
2018-06-27 10:34:35 +03:00
|
|
|
class _SyncGen:
|
2018-11-10 13:34:26 +03:00
|
|
|
def __init__(self, gen):
|
2018-06-27 10:34:35 +03:00
|
|
|
self.gen = gen
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
try:
|
2018-11-10 13:34:26 +03:00
|
|
|
return asyncio.get_event_loop() \
|
|
|
|
.run_until_complete(self.gen.__anext__())
|
2018-06-27 10:34:35 +03:00
|
|
|
except StopAsyncIteration:
|
|
|
|
raise StopIteration from None
|
|
|
|
|
|
|
|
|
2018-11-10 13:34:26 +03:00
|
|
|
def _syncify_wrap(t, method_name, gen):
|
2018-06-27 10:34:35 +03:00
|
|
|
method = getattr(t, method_name)
|
|
|
|
|
|
|
|
@functools.wraps(method)
|
|
|
|
def syncified(*args, **kwargs):
|
|
|
|
coro = method(*args, **kwargs)
|
2018-11-10 13:34:26 +03:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
if loop.is_running():
|
|
|
|
return coro
|
|
|
|
elif gen:
|
|
|
|
return _SyncGen(coro)
|
|
|
|
else:
|
|
|
|
return loop.run_until_complete(coro)
|
2018-06-27 10:34:35 +03:00
|
|
|
|
2018-10-08 12:13:24 +03:00
|
|
|
# Save an accessible reference to the original method
|
|
|
|
setattr(syncified, '__tl.sync', method)
|
2018-06-27 10:34:35 +03:00
|
|
|
setattr(t, method_name, syncified)
|
|
|
|
|
|
|
|
|
2018-06-26 11:54:04 +03:00
|
|
|
def syncify(*types):
|
|
|
|
"""
|
|
|
|
Converts all the methods in the given types (class definitions)
|
|
|
|
into synchronous, which return either the coroutine or the result
|
|
|
|
based on whether ``asyncio's`` event loop is running.
|
|
|
|
"""
|
|
|
|
for t in types:
|
2018-10-08 12:13:24 +03:00
|
|
|
for name in dir(t):
|
|
|
|
if not name.startswith('_') or name == '__call__':
|
|
|
|
if inspect.iscoroutinefunction(getattr(t, name)):
|
2018-11-10 13:34:26 +03:00
|
|
|
_syncify_wrap(t, name, gen=False)
|
2018-10-08 12:13:24 +03:00
|
|
|
elif isasyncgenfunction(getattr(t, name)):
|
2018-11-10 13:34:26 +03:00
|
|
|
_syncify_wrap(t, name, gen=True)
|
2018-06-26 11:54:04 +03:00
|
|
|
|
|
|
|
|
2018-07-10 16:15:22 +03:00
|
|
|
syncify(TelegramClient, Draft, Dialog, MessageButton,
|
2018-08-05 12:05:01 +03:00
|
|
|
ChatGetter, SenderGetter, Forward, Message, InlineResult, Conversation)
|