From 2132b01950e3f366875d578ab0bc22066b71f6cc Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 18 Oct 2020 16:10:36 +0100 Subject: [PATCH] Fix tag resolver --- newsdaemon.py | 68 +++++++++++++++++++++++++-------------------------- tests.py | 14 +++++++++++ 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/newsdaemon.py b/newsdaemon.py index bc75e7bc..e8c41da0 100644 --- a/newsdaemon.py +++ b/newsdaemon.py @@ -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) diff --git a/tests.py b/tests.py index 9daf3fb2..1f9d5b6e 100644 --- a/tests.py +++ b/tests.py @@ -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']