Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 2f0a1f4

Browse files
committed
Merge branch 'asyncio-dev'
2 parents faab2f4 + ecab62c commit 2f0a1f4

4 files changed

Lines changed: 52 additions & 15 deletions

File tree

pyrogram/client/ext/dispatcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ async def update_worker(self, lock):
188188

189189
if isinstance(handler, handler_type):
190190
try:
191-
if handler.check(parsed_update):
191+
if (await handler.check(parsed_update)):
192192
args = (parsed_update,)
193+
193194
except Exception as e:
194195
log.error(e, exc_info=True)
195196
continue

pyrogram/client/filters/filter.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import asyncio
20+
21+
1922
class Filter:
2023
def __call__(self, message):
2124
raise NotImplementedError
@@ -34,23 +37,48 @@ class InvertFilter(Filter):
3437
def __init__(self, base):
3538
self.base = base
3639

37-
def __call__(self, message):
38-
return not self.base(message)
40+
async def __call__(self, message):
41+
if asyncio.iscoroutinefunction(self.base.__call__):
42+
x = await self.base(message)
43+
else:
44+
x = self.base(message)
45+
46+
return not x
3947

4048

4149
class AndFilter(Filter):
4250
def __init__(self, base, other):
4351
self.base = base
4452
self.other = other
4553

46-
def __call__(self, message):
47-
return self.base(message) and self.other(message)
54+
async def __call__(self, message):
55+
if asyncio.iscoroutinefunction(self.base.__call__):
56+
x = await self.base(message)
57+
else:
58+
x = self.base(message)
59+
60+
if asyncio.iscoroutinefunction(self.other.__call__):
61+
y = await self.other(message)
62+
else:
63+
y = self.other(message)
64+
65+
return x and y
4866

4967

5068
class OrFilter(Filter):
5169
def __init__(self, base, other):
5270
self.base = base
5371
self.other = other
5472

55-
def __call__(self, message):
56-
return self.base(message) or self.other(message)
73+
async def __call__(self, message):
74+
if asyncio.iscoroutinefunction(self.base.__call__):
75+
x = await self.base(message)
76+
else:
77+
x = self.base(message)
78+
79+
if asyncio.iscoroutinefunction(self.other.__call__):
80+
y = await self.other(message)
81+
else:
82+
y = self.other(message)
83+
84+
return x or y

pyrogram/client/handlers/deleted_messages_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ class DeletedMessagesHandler(Handler):
4646
def __init__(self, callback: callable, filters=None):
4747
super().__init__(callback, filters)
4848

49-
def check(self, messages):
50-
return super().check(messages[0])
49+
async def check(self, messages):
50+
return (await super().check(messages[0]))

pyrogram/client/handlers/handler.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import asyncio
20+
21+
1922
class Handler:
2023
def __init__(self, callback: callable, filters=None):
2124
self.callback = callback
2225
self.filters = filters
2326

24-
def check(self, update):
25-
return (
26-
self.filters(update)
27-
if callable(self.filters)
28-
else True
29-
)
27+
async def check(self, update):
28+
29+
if callable(self.filters):
30+
if asyncio.iscoroutinefunction(self.filters.__call__):
31+
return (await self.filters(update))
32+
33+
else:
34+
return self.filters(update)
35+
36+
else:
37+
return True

0 commit comments

Comments
 (0)