-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.py
110 lines (88 loc) · 2.85 KB
/
filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# NPC list filter
# Copyright (C) 2021 yamahubuki <[email protected]>
import re
import constants
import globalVars
enableFilters=[]
class FilterBase():
def __init__(self):
pass
#フィルタの有効/無効を切り替える
def enable(self,status):
global enableFilters
if status:
enableFilters.append(self)
else:
enableFilters = [x for x in enableFilters if type(x)!= type(self)]
#現在有効になっていればTrue
def isEnable(self):
for f in getFilterList():
if type(self)==type(f):
return True
return False
#フィルタが有効な場合にのみ呼び出される
#与えられたanswerを画面に表示すべきならTrue
def test(self,**args):
raise NotImplementedError
#自動質問を非表示にする
class AutoQuestionFilter(FilterBase):
def test(self,**args):
return args["answerFlag"]&constants.FLG_ANSWER_AUTOQUESTION==0
#バトン質問を非表示にする
class BatonFilter(FilterBase):
def test(self,**args):
return args["answerFlag"]&constants.FLG_ANSWER_BATON_QUESTION==0
#特定ユーザの質問のみを表示
class UserFilter(FilterBase):
def __init__(self,user=None):
if user != None:
self.targetUserId = user.id
else:
self.targetUserId = -1
def test(self,**args):
if self.targetUserId > 0:
return args["userId"]==self.targetUserId
else:
return True
#検索条件に一致する回答のみを表示
class SearchFilter(FilterBase):
def __init__(self,keyword="",type=0,isRe=False):
self.keyword = keyword
self.type = type
self.isRe = isRe
if isRe:
self.ptn = re.compile(keyword)
def test(self,**args):
if self.isRe:
if self.type == 0 and not self.ptn.search(args["q"]):
return False
elif self.type == 1 and not self.ptn.search(args["a"]):
return False
elif self.type == 2 and not (self.ptn.search(args["a"])!=None or self.ptn.search(args["q"])!=None):
return False
else:
if self.type == 0 and not self.keyword in args["q"]:
return False
elif self.type == 1 and not self.keyword in args["a"]:
return False
elif self.type == 2 and not (self.keyword in args["q"] or self.keyword in args["a"]):
return False
return True
def getFilterList():
return enableFilters
# 適用状況を設定から読込
def loadStatus():
if globalVars.app.config.getboolean("filter_status","auto_question",False):
AutoQuestionFilter().enable(True)
if globalVars.app.config.getboolean("filter_status","baton",False):
BatonFilter().enable(True)
# 適用状況を保存
def saveStatus():
# いったんすべてFalseにしておく
globalVars.app.config["filter_status"]["auto_question"]=False
globalVars.app.config["filter_status"]["baton"]=False
for f in getFilterList():
if type(f)==AutoQuestionFilter:
globalVars.app.config["filter_status"]["auto_question"]=True
elif type(f)==BatonFilter:
globalVars.app.config["filter_status"]["baton"]=True