mirror of https://gitlab.com/bashrc2/epicyon
Wildcards can be used within filters
parent
7f88c850e2
commit
54ad8667c1
19
filters.py
19
filters.py
|
@ -8,6 +8,7 @@ __status__ = "Production"
|
||||||
__module_group__ = "Moderation"
|
__module_group__ = "Moderation"
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import fnmatch
|
||||||
from utils import data_dir
|
from utils import data_dir
|
||||||
from utils import acct_dir
|
from utils import acct_dir
|
||||||
from utils import text_in_file
|
from utils import text_in_file
|
||||||
|
@ -123,6 +124,18 @@ def _is_twitter_post(content: str) -> bool:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def filtered_match(filter_str: str, content: str) -> bool:
|
||||||
|
"""Does the given filter match the content?
|
||||||
|
"""
|
||||||
|
if '*' not in filter_str:
|
||||||
|
if filter_str in content:
|
||||||
|
return True
|
||||||
|
elif fnmatch.fnmatchcase(content, filter_str):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _is_filtered_base(filename: str, content: str,
|
def _is_filtered_base(filename: str, content: str,
|
||||||
system_language: str) -> bool:
|
system_language: str) -> bool:
|
||||||
"""Uses the given file containing filtered words to check
|
"""Uses the given file containing filtered words to check
|
||||||
|
@ -146,12 +159,12 @@ def _is_filtered_base(filename: str, content: str,
|
||||||
if len(filter_str) < 2:
|
if len(filter_str) < 2:
|
||||||
continue
|
continue
|
||||||
if '+' not in filter_str:
|
if '+' not in filter_str:
|
||||||
if filter_str in content:
|
if filtered_match(filter_str, content):
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
filter_words = filter_str.replace('"', '').split('+')
|
filter_words = filter_str.replace('"', '').split('+')
|
||||||
for word in filter_words:
|
for filter_wrd in filter_words:
|
||||||
if word not in content:
|
if not filtered_match(filter_wrd, content):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
|
|
11
tests.py
11
tests.py
|
@ -231,6 +231,7 @@ from conversation import convthread_id_to_conversation_tag
|
||||||
from webapp_utils import add_emoji_to_display_name
|
from webapp_utils import add_emoji_to_display_name
|
||||||
from blocking import is_blocked_nickname
|
from blocking import is_blocked_nickname
|
||||||
from blocking import is_blocked_domain
|
from blocking import is_blocked_domain
|
||||||
|
from filters import filtered_match
|
||||||
|
|
||||||
|
|
||||||
TEST_SERVER_GROUP_RUNNING = False
|
TEST_SERVER_GROUP_RUNNING = False
|
||||||
|
@ -9251,6 +9252,15 @@ def _test_blocking_domain(base_dir: str) -> None:
|
||||||
block_federated)
|
block_federated)
|
||||||
|
|
||||||
|
|
||||||
|
def _test_filter_match() -> None:
|
||||||
|
print('filter match')
|
||||||
|
assert not filtered_match('cycle', 'Some text.')
|
||||||
|
assert filtered_match('text', 'Some text.')
|
||||||
|
assert filtered_match('* text.', 'Some text.')
|
||||||
|
assert not filtered_match('* text', 'Some text.')
|
||||||
|
assert filtered_match('* text*', 'Some text.')
|
||||||
|
|
||||||
|
|
||||||
def run_all_tests():
|
def run_all_tests():
|
||||||
base_dir = os.getcwd()
|
base_dir = os.getcwd()
|
||||||
data_dir_testing(base_dir)
|
data_dir_testing(base_dir)
|
||||||
|
@ -9269,6 +9279,7 @@ def run_all_tests():
|
||||||
_test_checkbox_names()
|
_test_checkbox_names()
|
||||||
_test_thread_functions()
|
_test_thread_functions()
|
||||||
_test_functions()
|
_test_functions()
|
||||||
|
_test_filter_match()
|
||||||
_test_blocking_domain(base_dir)
|
_test_blocking_domain(base_dir)
|
||||||
_test_blocking_nick(base_dir)
|
_test_blocking_nick(base_dir)
|
||||||
_test_conversation_to_convthread()
|
_test_conversation_to_convthread()
|
||||||
|
|
Loading…
Reference in New Issue