def tag_delta(expression, tag_list):
"""Take in a tag expression and a list of tags and give the delta of tags to meet the expression
:return tuple( list( "required tags" ), list( "tuple of options" ) )
"""
if tag_list is None:
tag_list = []
required_tags = []
optional_tags = []
def parse_and(tokens):
args = tokens[0][0::2]
extend_list = filter(lambda x: isinstance(x, str) and x not in tag_list, args)
required_tags.extend(extend_list)
def parse_or(tokens):
args = tokens[0][0::2]
append_list = filter(lambda x: isinstance(x, str) and x not in tag_list, args)
if append_list == args:
optional_tags.append(tuple(append_list))
identifier = pp.Word(pp.alphanums + "_" + "-" + "'")
expr = pp.infixNotation(identifier, [
("AND", 2, pp.opAssoc.LEFT, parse_and),
("OR", 2, pp.opAssoc.LEFT, parse_or),
("and", 2, pp.opAssoc.LEFT, parse_and),
("or", 2, pp.opAssoc.LEFT, parse_or),
])
expr.parseString(expression)
if expression and not required_tags and expression not in tag_list:
# single tag in the expression
required_tags.append(expression)
return required_tags, optional_tags
评论列表
文章目录