Draft pyauto

This commit is contained in:
Changhua
2023-10-07 15:29:50 +08:00
parent 0df5d27e5a
commit df9557cce8
19 changed files with 66 additions and 1077 deletions
+6
View File
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from wcfauto.event.event import Event
from wcfauto.event.core import load_function
Event = load_function(Event)
+59
View File
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
import asyncio
import traceback
from threading import Thread
def load_function(cls):
cls._add_callback = _add_callback
cls._run_func = _run_func
return cls
def _add_callback(self, func, bot):
"""
消息处理函数加载器
:param func: 消息处理函数
:param args: 消息处理函数参数
:param kwargs: 消息处理函数参数
"""
if func in self._message_callback_func_list: return
self._message_callback_func_list.append(func)
self._message_callback_func[func] = bot
def _run_func(self):
"""
消息分发器, 将消息发送给所有消息处理函数
"""
try:
async_func = []
universal_func = []
for ele in self._message_callback_func:
if asyncio.iscoroutinefunction(ele):
async_func.append(ele)
else:
universal_func.append(ele)
# 同步函数运行器
def run_universal_func():
for fn in universal_func:
fn(self._message_callback_func[fn], self._message)
if len(universal_func) != 0: Thread(target=run_universal_func).start()
if len(async_func) == 0: return
# 异步函数运行器
async def _run_callback():
tasks = [asyncio.create_task(func(self._message_callback_func[func], self._message))
for func in async_func]
await asyncio.wait(tasks)
self._loop.run_until_complete(_run_callback())
except:
traceback.print_exc()
+33
View File
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
import asyncio
import logging
from abc import abstractmethod
class Event(object):
_message_callback_func = {}
_message_callback_func_list = []
_loop = asyncio.get_event_loop()
def __init__(self):
super(Event, self).__init__()
self._message = None
self._logger: logging = logging.getLogger()
@abstractmethod
def _add_callback(self, func, *args, **kwargs):
"""
消息处理函数加载器
:param func: 消息处理函数
:param args: 消息处理函数参数
:param kwargs: 消息处理函数参数
"""
raise NotImplementedError
@abstractmethod
def _run_func(self):
"""
消息分发器, 将消息发送给所有消息处理函数
"""
raise NotImplementedError