1
0
Fork 0

Changed some filters

This commit is contained in:
Casey 2022-08-30 21:20:15 +03:00
parent 26c23643c8
commit 01a384161c
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
2 changed files with 10 additions and 12 deletions

View File

@ -130,5 +130,5 @@ webhook = url
;# List of filters inside of itself ;# List of filters inside of itself
;filters = spoiler boost ;filters = spoiler boost
;# Operator to be used here ;# Operator to be used here
;# Options: "and", "or", "xor" ;# Options: "all", "any" or "single"
;operator = or ;operator = any

View File

@ -1,15 +1,14 @@
from configparser import ConfigParser, SectionProxy from configparser import ConfigParser, SectionProxy
from typing import Callable, ClassVar, Dict, List from typing import Callable, ClassVar, Dict, List, Sequence
from functools import reduce
from mastoposter.filters.base import BaseFilter, FilterInstance from mastoposter.filters.base import BaseFilter, FilterInstance
from mastoposter.types import Status from mastoposter.types import Status
class CombinedFilter(BaseFilter, filter_name="combined"): class CombinedFilter(BaseFilter, filter_name="combined"):
OPERATORS: ClassVar[Dict[str, Callable]] = { OPERATORS: ClassVar[Dict[str, Callable[[Sequence[bool]], bool]]] = {
"and": lambda a, b: a and b, "all": lambda d: all(d),
"or": lambda a, b: a or b, "any": lambda d: any(d),
"xor": lambda a, b: a ^ b, "single": lambda d: sum(d) == 1,
} }
def __init__(self, section: SectionProxy): def __init__(self, section: SectionProxy):
@ -27,11 +26,10 @@ class CombinedFilter(BaseFilter, filter_name="combined"):
for name in self.filter_names for name in self.filter_names
] ]
def __call__(self, status: Status) -> bool: def __call__(self, post: Status) -> bool:
results = [fil.filter(status) ^ fil.inverse for fil in self.filters]
if self.OPERATORS[self._operator_name] is not self.operator: if self.OPERATORS[self._operator_name] is not self.operator:
self._operator_name = "N/A" self._operator_name = str(self.operator)
return reduce(self.operator, results) return self.operator([f[1](post) ^ f[0] for f in self.filters])
def __repr__(self): def __repr__(self):
return ( return (