mirror of https://gitlab.com/bashrc2/epicyon
Tidying of newswire logic
parent
6f10986407
commit
a9e31baac5
|
@ -74,22 +74,26 @@ def _removeControlCharacters(content: str) -> str:
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
def _hashtagLogicalNot(tree: [], hashtags: [], moderated: bool,
|
||||||
content: str, url: str) -> 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
|
return False
|
||||||
|
|
||||||
if tree[0] == 'not':
|
|
||||||
if len(tree) == 2:
|
|
||||||
if isinstance(tree[1], str):
|
if isinstance(tree[1], str):
|
||||||
return tree[1] not in hashtags
|
return tree[1] not in hashtags
|
||||||
elif isinstance(tree[1], list):
|
elif isinstance(tree[1], list):
|
||||||
return not hashtagRuleResolve(tree[1], hashtags, moderated,
|
return not hashtagRuleResolve(tree[1], hashtags,
|
||||||
content, url)
|
moderated, content, url)
|
||||||
elif tree[0] == 'contains':
|
return False
|
||||||
if len(tree) == 2:
|
|
||||||
|
|
||||||
|
def _hashtagLogicalContains(tree: [], hashtags: [], moderated: bool,
|
||||||
|
content: str, url: str) -> bool:
|
||||||
|
""" Contains
|
||||||
|
"""
|
||||||
|
if len(tree) != 2:
|
||||||
|
return False
|
||||||
matchStr = None
|
matchStr = None
|
||||||
if isinstance(tree[1], str):
|
if isinstance(tree[1], str):
|
||||||
matchStr = tree[1]
|
matchStr = tree[1]
|
||||||
|
@ -102,8 +106,15 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
matchStrLower = matchStr.lower()
|
matchStrLower = matchStr.lower()
|
||||||
contentWithoutTags = content.replace('#' + matchStrLower, '')
|
contentWithoutTags = content.replace('#' + matchStrLower, '')
|
||||||
return matchStrLower in contentWithoutTags
|
return matchStrLower in contentWithoutTags
|
||||||
elif tree[0] == 'from':
|
return False
|
||||||
if len(tree) == 2:
|
|
||||||
|
|
||||||
|
def _hashtagLogicalFrom(tree: [], hashtags: [], moderated: bool,
|
||||||
|
content: str, url: str) -> bool:
|
||||||
|
""" FROM
|
||||||
|
"""
|
||||||
|
if len(tree) != 2:
|
||||||
|
return False
|
||||||
matchStr = None
|
matchStr = None
|
||||||
if isinstance(tree[1], str):
|
if isinstance(tree[1], str):
|
||||||
matchStr = tree[1]
|
matchStr = tree[1]
|
||||||
|
@ -114,8 +125,15 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
matchStr = matchStr[1:]
|
matchStr = matchStr[1:]
|
||||||
matchStr = matchStr[:len(matchStr) - 1]
|
matchStr = matchStr[:len(matchStr) - 1]
|
||||||
return matchStr.lower() in url
|
return matchStr.lower() in url
|
||||||
elif tree[0] == 'and':
|
return False
|
||||||
if len(tree) >= 3:
|
|
||||||
|
|
||||||
|
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)):
|
for argIndex in range(1, len(tree)):
|
||||||
argValue = False
|
argValue = False
|
||||||
if isinstance(tree[argIndex], str):
|
if isinstance(tree[argIndex], str):
|
||||||
|
@ -127,8 +145,14 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
if not argValue:
|
if not argValue:
|
||||||
return False
|
return False
|
||||||
return True
|
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)):
|
for argIndex in range(1, len(tree)):
|
||||||
argValue = False
|
argValue = False
|
||||||
if isinstance(tree[argIndex], str):
|
if isinstance(tree[argIndex], str):
|
||||||
|
@ -140,8 +164,14 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
if argValue:
|
if argValue:
|
||||||
return True
|
return True
|
||||||
return False
|
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
|
trueCtr = 0
|
||||||
for argIndex in range(1, len(tree)):
|
for argIndex in range(1, len(tree)):
|
||||||
argValue = False
|
argValue = False
|
||||||
|
@ -155,6 +185,28 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
trueCtr += 1
|
trueCtr += 1
|
||||||
if trueCtr == 1:
|
if trueCtr == 1:
|
||||||
return True
|
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:
|
elif tree[0].startswith('#') and len(tree) == 1:
|
||||||
return tree[0] in hashtags
|
return tree[0] in hashtags
|
||||||
elif tree[0].startswith('moderated'):
|
elif tree[0].startswith('moderated'):
|
||||||
|
|
Loading…
Reference in New Issue