def _ParseTaggingFile(self, tag_file_path): """Parses tag definitions from the source. Args: tag_file_path (str): path to the tag file. Returns: efilter.ast.Expression: efilter abstract syntax tree (AST), containing the tagging rules. """ tags = [] for label_name, rules in self._ParseDefinitions(tag_file_path): if not rules: logging.warning( u'All rules for label "{0:s}" are invalid.'.format( label_name)) continue tag = efilter_ast.IfElse( # Union will be true if any of the 'rules' match. efilter_ast.Union(*[rule.root for rule in rules]), # If so then evaluate to a string with the name of the tag. efilter_ast.Literal(label_name), # Otherwise don't return anything. efilter_ast.Literal(None)) tags.append(tag) # Generate a repeated value with all the tags (None will be skipped). return efilter_ast.Repeat(*tags)
def testIfElse(self): # Missing else: q = query.Query( ast.IfElse( ast.Pair(ast.Literal(True), ast.Literal("foo")), ast.Pair(ast.Literal(True), ast.Literal("bar")))) with self.assertRaises(errors.EfilterLogicError): validate.validate(q)
def testIfElse(self): self.assertQueryMatches( "if true then 'foo'", ast.IfElse(ast.Literal(True), ast.Literal("foo"), ast.Literal(None))) self.assertQueryMatches( "if true then 'foo' else 'bar'", ast.IfElse(ast.Literal(True), ast.Literal("foo"), ast.Literal("bar"))) self.assertQueryMatches( "if true then 'foo' else if 5 + 5 then 'bar' else 'baz'", ast.IfElse(ast.Literal(True), ast.Literal("foo"), ast.Sum(ast.Literal(5), ast.Literal(5)), ast.Literal("bar"), ast.Literal("baz"))) # Missing then blows up: self.assertQueryRaises("if (true) bar") # Colon blows up: self.assertQueryRaises("if true: bar")
def parse(self): tags = [] for tag_name, rules in self._parse_tagfile(): tag = ast.IfElse( # Union will be true if any of the 'rules' match. ast.Union(*[rule.root for rule in rules]), # If so then evaluate to a string with the name of the tag. ast.Literal(tag_name), # Otherwise don't return anything. ast.Literal(None)) tags.append(tag) self.original.close() # Generate a repeated value with all the tags (None will be skipped). return ast.Repeat(*tags)
def if_if(self): start = self.tokens.matched.start # Even-numbered children are conditions; odd-numbered are results. # Last child is the else expression. children = [self.expression()] self.tokens.expect(grammar.if_then) children.append(self.expression()) while self.tokens.accept(grammar.if_else_if): children.append(self.expression()) self.tokens.expect(grammar.if_then) children.append(self.expression()) if self.tokens.accept(grammar.if_else): children.append(self.expression()) else: children.append(ast.Literal(None)) return ast.IfElse(*children, start=start, end=self.tokens.matched.end, source=self.original)
def GetEventTaggingRules(self): """Retrieves the event tagging rules from the tagging file. Returns: efilter.ast.Expression: efilter abstract syntax tree (AST), containing the tagging rules. """ tags = [] for label_name, rules in self._ParseDefinitions(self._path): if not rules: continue tag = efilter_ast.IfElse( # Union will be true if any of the 'rules' match. efilter_ast.Union(*[rule.root for rule in rules]), # If so then evaluate to a string with the name of the tag. efilter_ast.Literal(label_name), # Otherwise don't return anything. efilter_ast.Literal(None)) tags.append(tag) # Generate a repeated value with all the tags (None will be skipped). return efilter_ast.Repeat(*tags)