Fix tag resolver

merge-requests/8/head
Bob Mottram 2020-10-18 16:10:36 +01:00
parent b5541563eb
commit 2132b01950
2 changed files with 47 additions and 35 deletions

View File

@ -108,39 +108,31 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
matchStr = matchStr[:len(matchStr) - 1]
return matchStr.lower() in content
elif tree[0] == 'and':
if len(tree) == 3:
firstArg = False
if isinstance(tree[1], str):
firstArg = (tree[1] in hashtags)
elif isinstance(tree[1], list):
firstArg = (hashtagRuleResolve(tree[1], hashtags, moderated,
content))
secondArg = False
if isinstance(tree[2], str):
secondArg = (tree[2] in hashtags)
elif isinstance(tree[2], list):
secondArg = (hashtagRuleResolve(tree[2], hashtags, moderated,
content))
return firstArg and secondArg
if len(tree) >= 3:
for argIndex in range(1, len(tree)):
argValue = False
if isinstance(tree[argIndex], str):
argValue = (tree[argIndex] in hashtags)
elif isinstance(tree[argIndex], list):
argValue = hashtagRuleResolve(tree[argIndex],
hashtags, moderated,
content)
if not argValue:
return False
return True
elif tree[0] == 'or':
if len(tree) == 3:
firstArg = False
if isinstance(tree[1], str):
firstArg = (tree[1] in hashtags)
elif isinstance(tree[1], list):
firstArg = (hashtagRuleResolve(tree[1], hashtags, moderated,
content))
secondArg = False
if isinstance(tree[2], str):
secondArg = (tree[2] in hashtags)
elif isinstance(tree[2], list):
secondArg = (hashtagRuleResolve(tree[2], hashtags, moderated,
content))
return firstArg or secondArg
if len(tree) >= 3:
for argIndex in range(1, len(tree)):
argValue = False
if isinstance(tree[argIndex], str):
argValue = (tree[argIndex] in hashtags)
elif isinstance(tree[argIndex], list):
argValue = hashtagRuleResolve(tree[argIndex],
hashtags, moderated,
content)
if argValue:
return True
return False
elif tree[0].startswith('#') and len(tree) == 1:
return tree[0] in hashtags
elif tree[0].startswith('moderated'):
@ -190,14 +182,20 @@ def hashtagRuleTree(operators: [],
ctr = 0
while ctr < len(operators):
op = operators[ctr]
if op not in conditionsStr:
opMatch = ' ' + op + ' '
if opMatch not in conditionsStr and \
not conditionsStr.startswith(op + ' '):
ctr += 1
continue
else:
tree = [op]
sections = conditionsStr.split(op)
if opMatch in conditionsStr:
sections = conditionsStr.split(opMatch)
else:
sections = conditionsStr.split(op + ' ', 1)
for subConditionStr in sections:
result = hashtagRuleTree(operators[ctr + 1:], subConditionStr,
result = hashtagRuleTree(operators[ctr + 1:],
subConditionStr,
tagsInConditions, moderated)
if result:
tree.append(result)

View File

@ -2179,6 +2179,18 @@ def testHashtagRuleTree():
print('testHashtagRuleTree')
operators = ('not', 'and', 'or', 'contains')
moderated = True
conditionsStr = \
'contains "Cat" or contains "Corvid" or ' + \
'contains "Dormouse" or contains "Buzzard"'
tagsInConditions = []
tree = hashtagRuleTree(operators, conditionsStr,
tagsInConditions, moderated)
assert str(tree) == str(['or', ['contains', ['"Cat"']],
['contains', ['"Corvid"']],
['contains', ['"Dormouse"']],
['contains', ['"Buzzard"']]])
content = 'This is a test'
moderated = True
conditionsStr = '#foo or #bar'
@ -2270,6 +2282,8 @@ def testHashtagRuleTree():
tagsInConditions, moderated)
assert str(tree) == str(['and', ['or', ['#foo'], ['#bar']], ['#a']])
assert str(tagsInConditions) == str(['#foo', '#bar', '#a'])
hashtags = ['#foo', '#bar', '#a']
assert hashtagRuleResolve(tree, hashtags, moderated, content)
hashtags = ['#bar', '#a']
assert hashtagRuleResolve(tree, hashtags, moderated, content)
hashtags = ['#foo', '#a']