Tidying of newswire logic

main
Bob Mottram 2021-07-04 10:24:35 +01:00
parent 6f10986407
commit a9e31baac5
1 changed files with 120 additions and 68 deletions

View File

@ -74,22 +74,26 @@ def _removeControlCharacters(content: str) -> str:
return content
def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
def _hashtagLogicalNot(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
"""Returns whether the tree for a hashtag rule evaluates to true or false
""" NOT
"""
if not tree:
if len(tree) != 2:
return False
if tree[0] == 'not':
if len(tree) == 2:
if isinstance(tree[1], str):
return tree[1] not in hashtags
elif isinstance(tree[1], list):
return not hashtagRuleResolve(tree[1], hashtags, moderated,
content, url)
elif tree[0] == 'contains':
if len(tree) == 2:
return not hashtagRuleResolve(tree[1], hashtags,
moderated, content, url)
return False
def _hashtagLogicalContains(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
""" Contains
"""
if len(tree) != 2:
return False
matchStr = None
if isinstance(tree[1], str):
matchStr = tree[1]
@ -102,8 +106,15 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
matchStrLower = matchStr.lower()
contentWithoutTags = content.replace('#' + matchStrLower, '')
return matchStrLower in contentWithoutTags
elif tree[0] == 'from':
if len(tree) == 2:
return False
def _hashtagLogicalFrom(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
""" FROM
"""
if len(tree) != 2:
return False
matchStr = None
if isinstance(tree[1], str):
matchStr = tree[1]
@ -114,8 +125,15 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
matchStr = matchStr[1:]
matchStr = matchStr[:len(matchStr) - 1]
return matchStr.lower() in url
elif tree[0] == 'and':
if len(tree) >= 3:
return False
def _hashtagLogicalAnd(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
""" AND
"""
if len(tree) < 3:
return False
for argIndex in range(1, len(tree)):
argValue = False
if isinstance(tree[argIndex], str):
@ -127,8 +145,14 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
if not argValue:
return False
return True
elif tree[0] == 'or':
if len(tree) >= 3:
def _hashtagLogicalOr(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
""" OR
"""
if len(tree) < 3:
return False
for argIndex in range(1, len(tree)):
argValue = False
if isinstance(tree[argIndex], str):
@ -140,8 +164,14 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
if argValue:
return True
return False
elif tree[0] == 'xor':
if len(tree) >= 3:
def _hashtagLogicalXor(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
""" XOR
"""
if len(tree) < 3:
return False
trueCtr = 0
for argIndex in range(1, len(tree)):
argValue = False
@ -155,6 +185,28 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
trueCtr += 1
if trueCtr == 1:
return True
return False
def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
content: str, url: str) -> bool:
"""Returns whether the tree for a hashtag rule evaluates to true or false
"""
if not tree:
return False
if tree[0] == 'not':
return _hashtagLogicalNot(tree, hashtags, moderated, content, url)
elif tree[0] == 'contains':
return _hashtagLogicalContains(tree, hashtags, moderated, content, url)
elif tree[0] == 'from':
return _hashtagLogicalFrom(tree, hashtags, moderated, content, url)
elif tree[0] == 'and':
return _hashtagLogicalAnd(tree, hashtags, moderated, content, url)
elif tree[0] == 'or':
return _hashtagLogicalOr(tree, hashtags, moderated, content, url)
elif tree[0] == 'xor':
return _hashtagLogicalXor(tree, hashtags, moderated, content, url)
elif tree[0].startswith('#') and len(tree) == 1:
return tree[0] in hashtags
elif tree[0].startswith('moderated'):