🔖 Update to v1.2.3

upgrade: pyrogram to 2.0.35
refactor: 依赖注入
This commit is contained in:
xtaodada 2022-07-24 14:47:13 +08:00
parent cce5688f19
commit c28eddaa4d
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
7 changed files with 56 additions and 11 deletions

View File

@ -14,7 +14,7 @@ import pyromod.listen
from pyrogram import Client from pyrogram import Client
import sys import sys
pgm_version = "1.2.2" pgm_version = "1.2.3"
CMD_LIST = {} CMD_LIST = {}
module_dir = __path__[0] module_dir = __path__[0]
working_dir = getcwd() working_dir = getcwd()

View File

@ -0,0 +1,7 @@
from pagermaid.single_utils import Client
from pagermaid.single_utils import Message
from pagermaid.sub_utils import Sub
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from sqlitedict import SqliteDict
from httpx import AsyncClient
from logging import Logger

View File

@ -3,6 +3,7 @@ import asyncio
from pyrogram import StopPropagation from pyrogram import StopPropagation
from pagermaid import hook_functions, logs from pagermaid import hook_functions, logs
from pagermaid.inject import inject
from pagermaid.single_utils import Message from pagermaid.single_utils import Message
@ -65,7 +66,7 @@ class Hook:
@staticmethod @staticmethod
async def startup(): async def startup():
if cors := [startup() for startup in hook_functions["startup"]]: if cors := [startup(**inject(None, startup)) for startup in hook_functions["startup"]]: # noqa
try: try:
await asyncio.gather(*cors) await asyncio.gather(*cors)
except Exception as exception: except Exception as exception:
@ -73,7 +74,7 @@ class Hook:
@staticmethod @staticmethod
async def shutdown(): async def shutdown():
if cors := [shutdown() for shutdown in hook_functions["shutdown"]]: if cors := [shutdown(**inject(None, shutdown)) for shutdown in hook_functions["shutdown"]]: # noqa
try: try:
await asyncio.gather(*cors) await asyncio.gather(*cors)
except Exception as exception: except Exception as exception:
@ -81,7 +82,7 @@ class Hook:
@staticmethod @staticmethod
async def command_pre(message: Message): async def command_pre(message: Message):
if cors := [pre(message) for pre in hook_functions["command_pre"]]: # noqa if cors := [pre(**inject(message, pre)) for pre in hook_functions["command_pre"]]: # noqa
try: try:
await asyncio.gather(*cors) await asyncio.gather(*cors)
except StopPropagation as e: except StopPropagation as e:
@ -91,7 +92,7 @@ class Hook:
@staticmethod @staticmethod
async def command_post(message: Message): async def command_post(message: Message):
if cors := [post(message) for post in hook_functions["command_post"]]: # noqa if cors := [post(**inject(message, post)) for post in hook_functions["command_post"]]: # noqa
try: try:
await asyncio.gather(*cors) await asyncio.gather(*cors)
except StopPropagation as e: except StopPropagation as e:
@ -101,7 +102,7 @@ class Hook:
@staticmethod @staticmethod
async def process_error_exec(message: Message, exc_info: BaseException, exc_format: str): async def process_error_exec(message: Message, exc_info: BaseException, exc_format: str):
if cors := [error(message, exc_info, exc_format) for error in hook_functions["process_error"]]: # noqa if cors := [error(**inject(message, error, exc_info=exc_info, exc_format=exc_format)) for error in hook_functions["process_error"]]: # noqa
try: try:
await asyncio.gather(*cors) await asyncio.gather(*cors)
except StopPropagation as e: except StopPropagation as e:

21
pagermaid/inject.py Normal file
View File

@ -0,0 +1,21 @@
import inspect
import pagermaid.enums as enums
import pagermaid.services as services
from typing import Dict, Optional
def inject(message: enums.Message, function, **data) -> Optional[Dict]:
try:
signature = inspect.signature(function)
except Exception:
return None
for parameter_name, parameter in signature.parameters.items():
class_name = parameter.annotation.__name__
param = message if class_name == "Message" else services.get(class_name)
if not param:
if parameter_name == "message":
param = message
else:
param = services.get(parameter_name.capitalize())
data.setdefault(parameter_name, param)
return data

View File

@ -16,6 +16,7 @@ from pyrogram.handlers import MessageHandler, EditedMessageHandler
from pagermaid import help_messages, logs, Config, bot, read_context, all_permissions from pagermaid import help_messages, logs, Config, bot, read_context, all_permissions
from pagermaid.group_manager import Permission from pagermaid.group_manager import Permission
from pagermaid.inject import inject
from pagermaid.single_utils import Message, AlreadyInConversationError, TimeoutConversationError, ListenerCanceled from pagermaid.single_utils import Message, AlreadyInConversationError, TimeoutConversationError, ListenerCanceled
from pagermaid.utils import lang, attach_report, sudo_filter, alias_command, get_permission_name, process_exit from pagermaid.utils import lang, attach_report, sudo_filter, alias_command, get_permission_name, process_exit
from pagermaid.utils import client as httpx_client from pagermaid.utils import client as httpx_client
@ -139,10 +140,15 @@ def listener(**args):
if command: if command:
await Hook.command_pre(message) await Hook.command_pre(message)
if function.__code__.co_argcount == 1: if data := inject(message, function):
await function(message) await function(**data)
elif function.__code__.co_argcount == 2: else:
await function(client, message) if function.__code__.co_argcount == 0:
await function()
if function.__code__.co_argcount == 1:
await function(message)
elif function.__code__.co_argcount == 2:
await function(client, message)
if command: if command:
await Hook.command_post(message) await Hook.command_post(message)
except StopPropagation as e: except StopPropagation as e:

View File

@ -0,0 +1,10 @@
from pagermaid import bot
from pagermaid import logs
from pagermaid.single_utils import sqlite
from pagermaid.scheduler import scheduler
from pagermaid.utils import client
def get(name: str):
data = {"Client": bot, "Logger": logs, "SqliteDict": sqlite, "AsyncIOScheduler": scheduler, "AsyncClient": client}
return data.get(name, None)

View File

@ -1,4 +1,4 @@
pyrogram==2.0.33 pyrogram==2.0.35
TgCrypto>=1.2.3 TgCrypto>=1.2.3
Pillow>=8.4.0 Pillow>=8.4.0
pytz>=2021.3 pytz>=2021.3