mirror of https://gitlab.com/bashrc2/epicyon
Fix tag resolver
parent
b5541563eb
commit
2132b01950
|
@ -108,39 +108,31 @@ def hashtagRuleResolve(tree: [], hashtags: [], moderated: bool,
|
||||||
matchStr = matchStr[:len(matchStr) - 1]
|
matchStr = matchStr[:len(matchStr) - 1]
|
||||||
return matchStr.lower() in content
|
return matchStr.lower() in content
|
||||||
elif tree[0] == 'and':
|
elif tree[0] == 'and':
|
||||||
if len(tree) == 3:
|
if len(tree) >= 3:
|
||||||
|
for argIndex in range(1, len(tree)):
|
||||||
firstArg = False
|
argValue = False
|
||||||
if isinstance(tree[1], str):
|
if isinstance(tree[argIndex], str):
|
||||||
firstArg = (tree[1] in hashtags)
|
argValue = (tree[argIndex] in hashtags)
|
||||||
elif isinstance(tree[1], list):
|
elif isinstance(tree[argIndex], list):
|
||||||
firstArg = (hashtagRuleResolve(tree[1], hashtags, moderated,
|
argValue = hashtagRuleResolve(tree[argIndex],
|
||||||
content))
|
hashtags, moderated,
|
||||||
|
content)
|
||||||
secondArg = False
|
if not argValue:
|
||||||
if isinstance(tree[2], str):
|
return False
|
||||||
secondArg = (tree[2] in hashtags)
|
return True
|
||||||
elif isinstance(tree[2], list):
|
|
||||||
secondArg = (hashtagRuleResolve(tree[2], hashtags, moderated,
|
|
||||||
content))
|
|
||||||
return firstArg and secondArg
|
|
||||||
elif tree[0] == 'or':
|
elif tree[0] == 'or':
|
||||||
if len(tree) == 3:
|
if len(tree) >= 3:
|
||||||
|
for argIndex in range(1, len(tree)):
|
||||||
firstArg = False
|
argValue = False
|
||||||
if isinstance(tree[1], str):
|
if isinstance(tree[argIndex], str):
|
||||||
firstArg = (tree[1] in hashtags)
|
argValue = (tree[argIndex] in hashtags)
|
||||||
elif isinstance(tree[1], list):
|
elif isinstance(tree[argIndex], list):
|
||||||
firstArg = (hashtagRuleResolve(tree[1], hashtags, moderated,
|
argValue = hashtagRuleResolve(tree[argIndex],
|
||||||
content))
|
hashtags, moderated,
|
||||||
|
content)
|
||||||
secondArg = False
|
if argValue:
|
||||||
if isinstance(tree[2], str):
|
return True
|
||||||
secondArg = (tree[2] in hashtags)
|
return False
|
||||||
elif isinstance(tree[2], list):
|
|
||||||
secondArg = (hashtagRuleResolve(tree[2], hashtags, moderated,
|
|
||||||
content))
|
|
||||||
return firstArg or secondArg
|
|
||||||
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'):
|
||||||
|
@ -190,14 +182,20 @@ def hashtagRuleTree(operators: [],
|
||||||
ctr = 0
|
ctr = 0
|
||||||
while ctr < len(operators):
|
while ctr < len(operators):
|
||||||
op = operators[ctr]
|
op = operators[ctr]
|
||||||
if op not in conditionsStr:
|
opMatch = ' ' + op + ' '
|
||||||
|
if opMatch not in conditionsStr and \
|
||||||
|
not conditionsStr.startswith(op + ' '):
|
||||||
ctr += 1
|
ctr += 1
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
tree = [op]
|
tree = [op]
|
||||||
sections = conditionsStr.split(op)
|
if opMatch in conditionsStr:
|
||||||
|
sections = conditionsStr.split(opMatch)
|
||||||
|
else:
|
||||||
|
sections = conditionsStr.split(op + ' ', 1)
|
||||||
for subConditionStr in sections:
|
for subConditionStr in sections:
|
||||||
result = hashtagRuleTree(operators[ctr + 1:], subConditionStr,
|
result = hashtagRuleTree(operators[ctr + 1:],
|
||||||
|
subConditionStr,
|
||||||
tagsInConditions, moderated)
|
tagsInConditions, moderated)
|
||||||
if result:
|
if result:
|
||||||
tree.append(result)
|
tree.append(result)
|
||||||
|
|
14
tests.py
14
tests.py
|
@ -2179,6 +2179,18 @@ def testHashtagRuleTree():
|
||||||
print('testHashtagRuleTree')
|
print('testHashtagRuleTree')
|
||||||
operators = ('not', 'and', 'or', 'contains')
|
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'
|
content = 'This is a test'
|
||||||
moderated = True
|
moderated = True
|
||||||
conditionsStr = '#foo or #bar'
|
conditionsStr = '#foo or #bar'
|
||||||
|
@ -2270,6 +2282,8 @@ def testHashtagRuleTree():
|
||||||
tagsInConditions, moderated)
|
tagsInConditions, moderated)
|
||||||
assert str(tree) == str(['and', ['or', ['#foo'], ['#bar']], ['#a']])
|
assert str(tree) == str(['and', ['or', ['#foo'], ['#bar']], ['#a']])
|
||||||
assert str(tagsInConditions) == str(['#foo', '#bar', '#a'])
|
assert str(tagsInConditions) == str(['#foo', '#bar', '#a'])
|
||||||
|
hashtags = ['#foo', '#bar', '#a']
|
||||||
|
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||||
hashtags = ['#bar', '#a']
|
hashtags = ['#bar', '#a']
|
||||||
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
assert hashtagRuleResolve(tree, hashtags, moderated, content)
|
||||||
hashtags = ['#foo', '#a']
|
hashtags = ['#foo', '#a']
|
||||||
|
|
Loading…
Reference in New Issue