FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hydrogram/hydrogram/sync.py at dev · anandpsdev/hydrogram · GitHub
anandpsdev
/
hydrogram
Public
forked from
hydrogram/hydrogram
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
hydrogram
/
hydrogram
/
sync.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
117 lines (92 loc) · 3.72 KB
Breadcrumbs
hydrogram
/
hydrogram
/
sync.py
Copy path
File metadata and controls
117 lines (92 loc) · 3.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Hydrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2023 Dan <https://github.com/delivrance>
# Copyright (C) 2023-present Hydrogram <https://hydrogram.org>
#
# This file is part of Hydrogram.
#
# Hydrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hydrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
import
asyncio
import
functools
import
inspect
import
threading
from
hydrogram
import
types
from
hydrogram
.
methods
import
Methods
from
hydrogram
.
methods
.
utilities
import
compose
as
compose_module
from
hydrogram
.
methods
.
utilities
import
idle
as
idle_module
def
async_to_sync
(
obj
,
name
):
function
=
getattr
(
obj
,
name
)
main_loop
=
asyncio
.
get_event_loop
()
def
async_to_sync_gen
(
agen
,
loop
,
is_main_thread
):
async
def
anext
(
agen
):
try
:
return
await
agen
.
__anext__
(),
False
except
StopAsyncIteration
:
return
None
,
True
while
True
:
if
is_main_thread
:
item
,
done
=
loop
.
run_until_complete
(
anext
(
agen
))
else
:
item
,
done
=
asyncio
.
run_coroutine_threadsafe
(
anext
(
agen
),
loop
).
result
()
if
done
:
break
yield
item
@
functools
.
wraps
(
function
)
def
async_to_sync_wrap
(
*
args
,
**
kwargs
):
coroutine
=
function
(
*
args
,
**
kwargs
)
try
:
loop
=
asyncio
.
get_event_loop
()
except
RuntimeError
:
loop
=
asyncio
.
new_event_loop
()
asyncio
.
set_event_loop
(
loop
)
if
threading
.
current_thread
()
is
threading
.
main_thread
()
or
not
main_loop
.
is_running
():
if
loop
.
is_running
():
return
coroutine
if
inspect
.
iscoroutine
(
coroutine
):
return
loop
.
run_until_complete
(
coroutine
)
if
inspect
.
isasyncgen
(
coroutine
):
return
async_to_sync_gen
(
coroutine
,
loop
,
True
)
return
None
if
inspect
.
iscoroutine
(
coroutine
):
if
loop
.
is_running
():
async
def
coro_wrapper
():
return
await
asyncio
.
wrap_future
(
asyncio
.
run_coroutine_threadsafe
(
coroutine
,
main_loop
)
)
return
coro_wrapper
()
return
asyncio
.
run_coroutine_threadsafe
(
coroutine
,
main_loop
).
result
()
if
inspect
.
isasyncgen
(
coroutine
):
if
loop
.
is_running
():
return
coroutine
return
async_to_sync_gen
(
coroutine
,
main_loop
,
False
)
return
None
setattr
(
obj
,
name
,
async_to_sync_wrap
)
def
wrap
(
source
):
for
name
in
dir
(
source
):
method
=
getattr
(
source
,
name
)
if
not
name
.
startswith
(
"_"
)
and
(
inspect
.
iscoroutinefunction
(
method
)
or
inspect
.
isasyncgenfunction
(
method
)
):
async_to_sync
(
source
,
name
)
# Wrap all Client's relevant methods
wrap
(
Methods
)
# Wrap types' bound methods
for
class_name
in
dir
(
types
):
cls
=
getattr
(
types
,
class_name
)
if
inspect
.
isclass
(
cls
):
wrap
(
cls
)
# Special case for idle and compose, because they are not inside Methods
async_to_sync
(
idle_module
,
"idle"
)
idle
=
getattr
(
idle_module
,
"idle"
)
async_to_sync
(
compose_module
,
"compose"
)
compose
=
getattr
(
compose_module
,
"compose"
)
Back
|
FazBrowse Home
|
New Git URL