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
;filters = spoiler boost
;# Operator to be used here
;# Options: "and", "or", "xor"
;operator = or
;# Options: "all", "any" or "single"
;operator = any

View File

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