From 01a384161c30dc57f73939f56f6e393d359db963 Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 30 Aug 2022 21:20:15 +0300 Subject: [PATCH] Changed some filters --- config.ini | 4 ++-- mastoposter/filters/combined.py | 18 ++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/config.ini b/config.ini index 63750f7..279b0e4 100644 --- a/config.ini +++ b/config.ini @@ -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 diff --git a/mastoposter/filters/combined.py b/mastoposter/filters/combined.py index 1752930..1fe32bd 100644 --- a/mastoposter/filters/combined.py +++ b/mastoposter/filters/combined.py @@ -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 (