2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 12:57:52 +00:00

Make plugin callback functions return the function itself when decorated

This commit is contained in:
Dan 2019-06-23 01:33:46 +02:00
parent b4f0f411bd
commit 31f39a00ab
9 changed files with 79 additions and 108 deletions

View File

@ -1106,7 +1106,7 @@ class Client(Methods, BaseClient):
for name in vars(module).keys():
# noinspection PyBroadException
try:
handler, group = getattr(module, name)
handler, group = getattr(module, name).pyrogram_plugin
if isinstance(handler, Handler) and isinstance(group, int):
self.add_handler(handler, group)
@ -1141,7 +1141,7 @@ class Client(Methods, BaseClient):
for name in handlers:
# noinspection PyBroadException
try:
handler, group = getattr(module, name)
handler, group = getattr(module, name).pyrogram_plugin
if isinstance(handler, Handler) and isinstance(group, int):
self.add_handler(handler, group)
@ -1179,7 +1179,7 @@ class Client(Methods, BaseClient):
for name in handlers:
# noinspection PyBroadException
try:
handler, group = getattr(module, name)
handler, group = getattr(module, name).pyrogram_plugin
if isinstance(handler, Handler) and isinstance(group, int):
self.remove_handler(handler, group)

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple
from typing import Callable
import pyrogram
from pyrogram.client.filters.filter import Filter
from pyrogram.client.handlers.handler import Handler
from ...ext import BaseClient
@ -44,18 +43,15 @@ class OnCallbackQuery(BaseClient):
The group identifier, defaults to 0.
"""
def decorator(func: callable) -> Tuple[Handler, int]:
if isinstance(func, tuple):
func = func[0].callback
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.CallbackQueryHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
func.pyrogram_plugin = (
pyrogram.CallbackQueryHandler(func, self),
group if filters is None else filters
)
handler = pyrogram.CallbackQueryHandler(func, filters)
if isinstance(self, Filter):
return pyrogram.CallbackQueryHandler(func, self), group if filters is None else filters
if self is not None:
self.add_handler(handler, group)
return handler, group
return func
return decorator

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple
from typing import Callable
import pyrogram
from pyrogram.client.filters.filter import Filter
from pyrogram.client.handlers.handler import Handler
from ...ext import BaseClient
@ -44,18 +43,15 @@ class OnDeletedMessages(BaseClient):
The group identifier, defaults to 0.
"""
def decorator(func: callable) -> Tuple[Handler, int]:
if isinstance(func, tuple):
func = func[0].callback
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.DeletedMessagesHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
func.pyrogram_plugin = (
pyrogram.DeletedMessagesHandler(func, self),
group if filters is None else filters
)
handler = pyrogram.DeletedMessagesHandler(func, filters)
if isinstance(self, Filter):
return pyrogram.DeletedMessagesHandler(func, self), group if filters is None else filters
if self is not None:
self.add_handler(handler, group)
return handler, group
return func
return decorator

View File

@ -16,8 +16,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Callable
import pyrogram
from pyrogram.client.handlers.handler import Handler
from ...ext import BaseClient
@ -28,12 +29,10 @@ class OnDisconnect(BaseClient):
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the :obj:`~pyrogram.DisconnectHandler`.
"""
def decorator(func: callable) -> Handler:
handler = pyrogram.DisconnectHandler(func)
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.DisconnectHandler(func))
if self is not None:
self.add_handler(handler)
return handler
return func
return decorator

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple
from typing import Callable
import pyrogram
from pyrogram.client.filters.filter import Filter
from pyrogram.client.handlers.handler import Handler
from ...ext import BaseClient
@ -43,18 +42,15 @@ class OnInlineQuery(BaseClient):
The group identifier, defaults to 0.
"""
def decorator(func: callable) -> Tuple[Handler, int]:
if isinstance(func, tuple):
func = func[0].callback
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.InlineQueryHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
func.pyrogram_plugin = (
pyrogram.InlineQueryHandler(func, self),
group if filters is None else filters
)
handler = pyrogram.InlineQueryHandler(func, filters)
if isinstance(self, Filter):
return pyrogram.InlineQueryHandler(func, self), group if filters is None else filters
if self is not None:
self.add_handler(handler, group)
return handler, group
return func
return decorator

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple
from typing import Callable
import pyrogram
from pyrogram.client.filters.filter import Filter
from pyrogram.client.handlers.handler import Handler
from ...ext import BaseClient
@ -43,18 +42,15 @@ class OnMessage(BaseClient):
The group identifier, defaults to 0.
"""
def decorator(func: callable) -> Tuple[Handler, int]:
if isinstance(func, tuple):
func = func[0].callback
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.MessageHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
func.pyrogram_plugin = (
pyrogram.MessageHandler(func, self),
group if filters is None else filters
)
handler = pyrogram.MessageHandler(func, filters)
if isinstance(self, Filter):
return pyrogram.MessageHandler(func, self), group if filters is None else filters
if self is not None:
self.add_handler(handler, group)
return handler, group
return func
return decorator

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple
from typing import Callable
import pyrogram
from pyrogram.client.filters.filter import Filter
from pyrogram.client.handlers.handler import Handler
from ...ext import BaseClient
@ -43,18 +42,15 @@ class OnPoll(BaseClient):
The group identifier, defaults to 0.
"""
def decorator(func: callable) -> Tuple[Handler, int]:
if isinstance(func, tuple):
func = func[0].callback
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.PollHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
func.pyrogram_plugin = (
pyrogram.PollHandler(func, self),
group if filters is None else filters
)
handler = pyrogram.PollHandler(func, filters)
if isinstance(self, Filter):
return pyrogram.PollHandler(func, self), group if filters is None else filters
if self is not None:
self.add_handler(handler, group)
return handler, group
return func
return decorator

View File

@ -16,10 +16,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple
from typing import Callable
import pyrogram
from pyrogram.client.handlers.handler import Handler
from ...ext import BaseClient
@ -37,18 +36,15 @@ class OnRawUpdate(BaseClient):
The group identifier, defaults to 0.
"""
def decorator(func: callable) -> Tuple[Handler, int]:
if isinstance(func, tuple):
func = func[0].callback
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.RawUpdateHandler(func), group)
else:
func.pyrogram_plugin = (
pyrogram.RawUpdateHandler(func),
group if self is None else group
)
handler = pyrogram.RawUpdateHandler(func)
if isinstance(self, int):
return handler, group if self is None else group
if self is not None:
self.add_handler(handler, group)
return handler, group
return func
return decorator

View File

@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Tuple
from typing import Callable
import pyrogram
from pyrogram.client.filters.filter import Filter
from pyrogram.client.handlers.handler import Handler
from ...ext import BaseClient
@ -41,18 +40,15 @@ class OnUserStatus(BaseClient):
The group identifier, defaults to 0.
"""
def decorator(func: callable) -> Tuple[Handler, int]:
if isinstance(func, tuple):
func = func[0].callback
def decorator(func: Callable) -> Callable:
if isinstance(self, pyrogram.Client):
self.add_handler(pyrogram.UserStatusHandler(func, filters), group)
elif isinstance(self, Filter) or self is None:
func.pyrogram_plugin = (
pyrogram.UserStatusHandler(func, self),
group if filters is None else filters
)
handler = pyrogram.UserStatusHandler(func, filters)
if isinstance(self, Filter):
return pyrogram.UserStatusHandler(func, self), group if filters is None else filters
if self is not None:
self.add_handler(handler, group)
return handler, group
return func
return decorator