2020-06-30 10:13:58 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-05-02 20:03:36 +08:00
|
|
|
from functools import wraps
|
2020-06-30 10:13:58 +08:00
|
|
|
from collections import defaultdict
|
|
|
|
from typing import Type, List, Dict, Optional, Callable
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
from .event import Event
|
2020-06-30 10:13:58 +08:00
|
|
|
from .typing import Handler
|
|
|
|
from .rule import Rule, user
|
2020-05-05 16:11:05 +08:00
|
|
|
from .exception import PausedException, RejectedException, FinishedException
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
matchers: Dict[int, List[Type["Matcher"]]] = defaultdict(list)
|
|
|
|
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
|
|
class Matcher:
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
rule: Rule = Rule()
|
2020-06-30 10:13:58 +08:00
|
|
|
handlers: List[Handler] = []
|
2020-05-05 16:11:05 +08:00
|
|
|
temp: bool = False
|
2020-06-30 10:13:58 +08:00
|
|
|
priority: int = 1
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
|
|
_default_state: dict = {}
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
|
|
# _default_parser: Optional[Callable[[Event, dict], None]] = None
|
|
|
|
# _args_parser: Optional[Callable[[Event, dict], None]] = None
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.handlers = self.handlers.copy()
|
|
|
|
self.state = self._default_state.copy()
|
2020-06-30 10:13:58 +08:00
|
|
|
# self.parser = self._args_parser or self._default_parser
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
|
|
@classmethod
|
2020-06-30 10:13:58 +08:00
|
|
|
def new(cls,
|
|
|
|
rule: Rule = Rule(),
|
|
|
|
handlers: list = [],
|
|
|
|
temp: bool = False,
|
|
|
|
priority: int = 1,
|
|
|
|
*,
|
|
|
|
default_state: dict = {}) -> Type["Matcher"]:
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
|
|
NewMatcher = type(
|
2020-06-24 21:56:49 +08:00
|
|
|
"Matcher", (Matcher,), {
|
2020-05-05 16:11:05 +08:00
|
|
|
"rule": rule,
|
|
|
|
"handlers": handlers,
|
|
|
|
"temp": temp,
|
2020-06-30 10:13:58 +08:00
|
|
|
"priority": priority,
|
|
|
|
"_default_state": default_state
|
2020-05-05 16:11:05 +08:00
|
|
|
})
|
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
matchers[priority].append(NewMatcher)
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
return NewMatcher
|
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# @classmethod
|
|
|
|
# def args_parser(cls, func: Callable[[Event, dict], None]):
|
|
|
|
# cls._default_parser = func
|
|
|
|
# return func
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
@classmethod
|
2020-06-30 10:13:58 +08:00
|
|
|
def handle(cls):
|
|
|
|
|
|
|
|
def _decorator(func: Handler) -> Handler:
|
|
|
|
cls.handlers.append(func)
|
|
|
|
return func
|
|
|
|
|
|
|
|
return _decorator
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
@classmethod
|
|
|
|
def receive(cls):
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
|
|
def _decorator(func: Handler) -> Handler:
|
|
|
|
|
|
|
|
@wraps(func)
|
2020-06-30 10:13:58 +08:00
|
|
|
async def _handler(bot, event: Event, state: dict):
|
2020-05-05 16:11:05 +08:00
|
|
|
raise PausedException
|
|
|
|
|
|
|
|
cls.handlers.append(_handler)
|
2020-06-30 10:13:58 +08:00
|
|
|
cls.handlers.append(func)
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
return _decorator
|
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# @classmethod
|
|
|
|
# def got(cls,
|
|
|
|
# key: str,
|
|
|
|
# prompt: Optional[str] = None,
|
|
|
|
# args_parser: Optional[Callable[[Event, dict], None]] = None):
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# def _decorator(func: Handler) -> Handler:
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# @wraps(func)
|
|
|
|
# def _handler(event: Event, state: dict):
|
|
|
|
# if key not in state:
|
|
|
|
# if state.get("__current_arg__", None) == key:
|
|
|
|
# state[key] = event.message
|
|
|
|
# del state["__current_arg__"]
|
|
|
|
# return func(event, state)
|
|
|
|
# state["__current_arg__"] = key
|
|
|
|
# cls._args_parser = args_parser
|
|
|
|
# raise RejectedException
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# return func(event, state)
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# cls.handlers.append(_handler)
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# return func
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# return _decorator
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
# @classmethod
|
|
|
|
# def finish(cls, prompt: Optional[str] = None):
|
|
|
|
# raise FinishedException
|
|
|
|
|
|
|
|
# @classmethod
|
|
|
|
# def reject(cls, prompt: Optional[str] = None):
|
|
|
|
# raise RejectedException
|
2020-05-02 20:03:36 +08:00
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
async def run(self, bot, event):
|
2020-05-05 16:11:05 +08:00
|
|
|
if not self.rule(event):
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2020-06-30 10:13:58 +08:00
|
|
|
# if self.parser:
|
|
|
|
# await self.parser(event, state) # type: ignore
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
|
|
for _ in range(len(self.handlers)):
|
|
|
|
handler = self.handlers.pop(0)
|
2020-06-30 10:13:58 +08:00
|
|
|
await handler(bot, event, self.state)
|
2020-05-05 16:11:05 +08:00
|
|
|
except RejectedException:
|
2020-06-30 10:13:58 +08:00
|
|
|
self.handlers.insert(0, handler) # type: ignore
|
|
|
|
matcher = Matcher.new(user(event.user_id) & self.rule,
|
2020-05-05 16:11:05 +08:00
|
|
|
self.handlers,
|
|
|
|
temp=True,
|
2020-06-30 10:13:58 +08:00
|
|
|
priority=0,
|
|
|
|
default_state=self.state)
|
|
|
|
matchers[0].append(matcher)
|
2020-05-05 16:11:05 +08:00
|
|
|
return
|
|
|
|
except PausedException:
|
2020-06-30 10:13:58 +08:00
|
|
|
matcher = Matcher.new(user(event.user_id) & self.rule,
|
2020-05-05 16:11:05 +08:00
|
|
|
self.handlers,
|
|
|
|
temp=True,
|
2020-06-30 10:13:58 +08:00
|
|
|
priority=0,
|
|
|
|
default_state=self.state)
|
|
|
|
matchers[0].append(matcher)
|
2020-05-05 16:11:05 +08:00
|
|
|
return
|
|
|
|
except FinishedException:
|
|
|
|
return
|