Use decorator to add filters

This commit is contained in:
Richard Chien 2017-01-02 13:38:58 +08:00
parent fa893332d9
commit b60a7a16eb
7 changed files with 21 additions and 24 deletions

View File

@ -10,5 +10,13 @@ def apply_filters(ctx_msg):
return True return True
def add_filter(func, priority=0): def add_filter(func, priority=10):
_filters.append((priority, func)) _filters.append((priority, func))
def as_filter(priority=10):
def decorator(func):
add_filter(func, priority)
return func
return decorator

View File

@ -4,7 +4,7 @@ import importlib
import interactive import interactive
from config import config from config import config
from filter import add_filter from filter import as_filter
from command import CommandNotExistsError, CommandScopeError, CommandPermissionError from command import CommandNotExistsError, CommandScopeError, CommandPermissionError
from little_shit import * from little_shit import *
from commands import core from commands import core
@ -29,8 +29,8 @@ def _load_commands():
print('Failed to load command module "' + mod_name + '.py".', file=sys.stderr) print('Failed to load command module "' + mod_name + '.py".', file=sys.stderr)
@as_filter(priority=0)
def _dispatch_command(ctx_msg): def _dispatch_command(ctx_msg):
# noinspection PyBroadException
try: try:
text = ctx_msg.get('text', '').lstrip() text = ctx_msg.get('text', '').lstrip()
if not text: if not text:
@ -78,4 +78,3 @@ def _dispatch_command(ctx_msg):
_load_commands() _load_commands()
add_filter(_dispatch_command, 0)

View File

@ -1,7 +1,8 @@
from filter import add_filter from filter import as_filter
from commands import core from commands import core
@as_filter(priority=1)
def _print_help_message(ctx_msg): def _print_help_message(ctx_msg):
a = ['help', '怎么用', '怎么用啊', '你好', '你好啊', '帮助', a = ['help', '怎么用', '怎么用啊', '你好', '你好啊', '帮助',
'用法', '使用帮助', '使用指南', '使用说明', '使用方法', '用法', '使用帮助', '使用指南', '使用说明', '使用方法',
@ -11,6 +12,3 @@ def _print_help_message(ctx_msg):
core.help('', ctx_msg) core.help('', ctx_msg)
return False return False
return True return True
add_filter(_print_help_message, priority=1)

View File

@ -2,9 +2,10 @@
This filter intercepts messages that contains content not allowed and move text content to 'text' field. This filter intercepts messages that contains content not allowed and move text content to 'text' field.
""" """
from filter import add_filter from filter import as_filter
@as_filter(priority=100)
def _filter(ctx_msg): def _filter(ctx_msg):
if ctx_msg.get('via') == 'wx': if ctx_msg.get('via') == 'wx':
msg_format = ctx_msg.get('format') msg_format = ctx_msg.get('format')
@ -17,6 +18,3 @@ def _filter(ctx_msg):
elif ctx_msg.get('via') == 'qq': elif ctx_msg.get('via') == 'qq':
ctx_msg['text'] = ctx_msg.get('content') ctx_msg['text'] = ctx_msg.get('content')
return True return True
add_filter(_filter, 100)

View File

@ -2,14 +2,12 @@
This filter just log message to stdout. This filter just log message to stdout.
""" """
from filter import add_filter from filter import as_filter
@as_filter(priority=1000)
def _log_message(ctx_msg): def _log_message(ctx_msg):
print(ctx_msg.get('sender', '') print(ctx_msg.get('sender', '')
+ (('@' + ctx_msg.get('group')) if ctx_msg.get('type') == 'group_message' else '') + (('@' + ctx_msg.get('group')) if ctx_msg.get('type') == 'group_message' else '')
+ (('@' + ctx_msg.get('discuss')) if ctx_msg.get('type') == 'discuss_message' else '') + (('@' + ctx_msg.get('discuss')) if ctx_msg.get('type') == 'discuss_message' else '')
+ ': ' + ctx_msg.get('content')) + ': ' + ctx_msg.get('content'))
add_filter(_log_message, 1000)

View File

@ -10,7 +10,7 @@ import requests
from pydub import AudioSegment from pydub import AudioSegment
import speech_recognition as sr import speech_recognition as sr
from filter import add_filter from filter import as_filter
from commands import core from commands import core
@ -55,6 +55,7 @@ def _recognize_bing(wav_path, api_key, language='zh-CN'):
return None return None
@as_filter(priority=90)
def _filter(ctx_msg): def _filter(ctx_msg):
if ctx_msg.get('via') == 'wx' and ctx_msg.get('format') == 'media' and ctx_msg.get('media_type') == 'voice': if ctx_msg.get('via') == 'wx' and ctx_msg.get('format') == 'media' and ctx_msg.get('media_type') == 'voice':
m = re.match('\[语音\]\(([/_A-Za-z0-9]+\.mp3)\)', ctx_msg.get('content')) m = re.match('\[语音\]\(([/_A-Za-z0-9]+\.mp3)\)', ctx_msg.get('content'))
@ -83,6 +84,3 @@ def _filter(ctx_msg):
reply = '抱歉哦,没有识别出你说的是什么' reply = '抱歉哦,没有识别出你说的是什么'
core.echo(reply, ctx_msg) core.echo(reply, ctx_msg)
os.remove(wav_path) os.remove(wav_path)
add_filter(_filter, 90)

View File

@ -2,9 +2,10 @@
This filter intercepts messages not intended to the bot and removes the beginning "@xxx". This filter intercepts messages not intended to the bot and removes the beginning "@xxx".
""" """
from filter import add_filter from filter import as_filter
@as_filter(priority=50)
def _split_at_xiaokai(ctx_msg): def _split_at_xiaokai(ctx_msg):
if ctx_msg.get('type') == 'group_message' or ctx_msg.get('type') == 'discuss_message': if ctx_msg.get('type') == 'group_message' or ctx_msg.get('type') == 'discuss_message':
text = ctx_msg.get('text', '') text = ctx_msg.get('text', '')
@ -21,6 +22,3 @@ def _split_at_xiaokai(ctx_msg):
return False return False
ctx_msg['text'] = text.lstrip() ctx_msg['text'] = text.lstrip()
return True return True
add_filter(_split_at_xiaokai, priority=50)