Skip to content

Commit

Permalink
[Feature] Fire 解析器 #11
Browse files Browse the repository at this point in the history
  • Loading branch information
luxuncang committed Feb 28, 2022
1 parent 393a08b commit 6b1ccf9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
70 changes: 66 additions & 4 deletions Alice/parse/AliceParse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
Plain
)
from abc import ABC, abstractmethod
from functools import lru_cache
from typing import Dict, Iterable, Any, Tuple, Union
import re
from parse import parse, compile
from typefire import typeswitch, typefire, TypeFire
from pydantic import create_model, BaseModel
from ..exception import ParseException

TypeFire.capture_fire()

class Match(ABC):

@abstractmethod
Expand Down Expand Up @@ -79,12 +82,49 @@ def __init__(self, pattern: str) -> None:

def match(self, message: str) -> Iterable[str]:
res = self.pattern.parse(message)
if res:
return res

class FireMatch(Match):
'''fire解析'''

def __init__(self, signature: Dict[str, tuple]) -> None:
self.f = self.generat_func(signature)

def match(self, message: str):
res = typefire(self.f)(message)
if res:
return res

def generat_func(self, signature: dict):
s = 'def _('
def hint(v):
if len(v) <= 1:
v = (..., ...)
h = ''
if v[0] == ... or v[0] == None:
...
else:
h += f': {v[0] if isinstance(v[0], str) else v[0].__name__}'
if v[1] == ...:
...
else:
h += f' = {v[1]}'
h += ','
return h

for k,v in signature.items():
s += k + hint(v)
s += '):\n return locals()'
s += '\nTemp.f = _'
class Temp:
...
exec(s)
return Temp.f

class ParseRusult:
'''解析结果'''
def __init__(self) -> None:
def __init__(self, message: MessageChain = None) -> None:
self.command = []
self.least = []
self.options = []
Expand All @@ -93,6 +133,8 @@ def __init__(self) -> None:
self.FullMatch = []
self.ArgumentMatch = []
self.ParseMatch = []
self.FireMatch = []
self.message = message

def __getattr__(self, name):
if name == 'cmd':
Expand All @@ -115,7 +157,27 @@ def __getattr__(self, name):
for i in self.ParseMatch:
if name in i.named:
return i[name]
raise AttributeError(name)
for i in self.FireMatch:
if name in i:
return i[name]
# raise AttributeError(name)
return None

def get_dict(self):
data = {}
for i in self.ParseMatch:
data.update(i.named)
return {
'cmd': self.cmd,
'lat': self.lat,
'opt': self.opt,
're': self.re,
'ele': self.ele,
'full': self.full,
'arg': self.arg,
'par': self.par,
**data
}

@staticmethod
def _reduction(parse_result):
Expand Down Expand Up @@ -171,7 +233,7 @@ def match(self, message: MessageChain):

@staticmethod
def distribution(pattern: Match, message: MessageChain):
if isinstance(pattern, (RegexMatch, ParseMatch)):
if isinstance(pattern, (RegexMatch, ParseMatch, FireMatch)):
return '\n'.join([i.text for i in message[Plain]])
elif isinstance(pattern, (FullMatch, ArgumentMatch)):
if pattern.type:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ win32-setctime==1.0.3
wincertstore==0.2
wrapt==1.13.3
yarl==1.7.2
bilibili_api
bilibili_api
typefire

0 comments on commit 6b1ccf9

Please sign in to comment.