# qiuwenbot, a bot to contribute to qiuwen.wiki# Copyright (C) 2022 Jinzhe Zeng## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see <https://www.gnu.org/licenses/>.#importrefromabcimportABCMeta,abstractmethodfromtypingimportList,Union
[docs]@abstractmethoddeffilter(self,text:str)->str:"""Filter text. Parameters ---------- text : str Text to filter. Returns ------- str Filtered text. """
@propertydeflog(self)->Union[str,None]:"""Log of the filter. Returns ------- str Log of the filter. """
[docs]classTextReplaceFilter(Filter):"""Filter to replace texts. Parameters ---------- pattern : str Pattern to replace. repl : str Replacement. """def__init__(self,pattern:str,repl:str)->None:super().__init__()self.prog=re.compile(pattern)self.repl=repl
[docs]deffilter(self,text:str)->str:"""Filter text. Parameters ---------- text : str Text to filter. Returns ------- str Filtered text. """returnself.prog.sub(self.repl,text)
@propertydeflog(self)->str:"""Log of the filter. Returns ------- str Log of the filter. """returnf"替换{self.prog.pattern}为{self.repl}"
[docs]classFilterChain(Filter):"""Filter chain. Parameters ---------- filters : list of Filter Filters to apply. """def__init__(self,filters:List[Filter])->None:super().__init__()self.filters=[filter()forfilterinfilters]self.active_filters=[]
[docs]deffilter(self,text:str)->str:"""Filter text. Parameters ---------- text : str Text to filter. Returns ------- str Filtered text. """active_filters=[]forfilterinself.filters:old_text=texttext=filter.filter(text)iftext!=old_text:active_filters.append(filter)self.active_filters=active_filtersreturntext
@propertydeflog(self)->str:"""Log of the filter. Returns ------- str Log of the filter. """logs=[]forfilterinself.active_filters:iffilter.logisnotNone:logs.append(filter.log)return"综合治理:"+";".join(logs)
default_filters=[]
[docs]defregister_filter(cls:Filter)->Filter:"""Return a decorator to register filter. The filter should not have any parameters in its constructor. Parameters ---------- cls : Filter Filter to register. Returns ------- Filter Registered filter. Examples -------- >>> @register_filter() ... class Filter1(Filter): ... pass """default_filters.append(cls)returncls