Example #1
0
    def _ParseEventTaggingRule(self, event_tagging_expression):
        """Parses an event tagging expression.

    This method attempts to detect whether the event tagging expression is valid
    objectfilter or dottysql syntax.

    Example:
      _ParseEventTaggingRule('5 + 5')
      # Returns Sum(Literal(5), Literal(5))

    Args:
      event_tagging_expression (str): event tagging experssion either in
          objectfilter or dottysql syntax.

    Returns:
      efilter.query.Query: efilter query of the event tagging expression.

    Raises:
      TaggingFileError: when the tagging file cannot be correctly parsed.
    """
        if self._OBJECTFILTER_WORDS.search(event_tagging_expression):
            syntax = 'objectfilter'
        else:
            syntax = 'dottysql'

        try:
            return efilter_query.Query(event_tagging_expression, syntax=syntax)

        except efilter_errors.EfilterParseError as exception:
            stripped_expression = event_tagging_expression.rstrip()
            raise errors.TaggingFileError((
                'Unable to parse event tagging expressoin: "{0:s}" with error: '
                '{1!s}').format(stripped_expression, exception))
Example #2
0
  def GetEventTaggingRules(self):
    """Retrieves the event tagging rules from the tagging file.

    Returns:
      dict[str, EventObjectFilter]: tagging rules, that consists of one or more
          filter objects per label.

    Raises:
      TaggingFileError: if a filter expression cannot be compiled.
    """
    rules_per_label = {}

    label_name = None
    with io.open(self._path, 'r', encoding='utf-8') as tagging_file:
      for line in tagging_file.readlines():
        line = line.rstrip()

        stripped_line = line.lstrip()
        if not stripped_line:
          label_name = None
          continue

        if stripped_line[0] == '#':
          continue

        if not line[0].isspace():
          label_name = line
          rules_per_label[label_name] = []

        elif label_name:
          rules_per_label[label_name].append(stripped_line)

    filter_objects_per_label = {}

    for label_name, rules in rules_per_label.items():
      filter_object = event_filter.EventObjectFilter()

      try:
        filter_rule = ' OR '.join(['({0:s})'.format(rule) for rule in rules])
        filter_object.CompileFilter(filter_rule)
      except errors.ParseError as exception:
        raise errors.TaggingFileError((
            'Unable to compile filter for label: {0:s} with error: '
            '{1!s}').format(label_name, exception))

      # TODO: change other code remove list around filter_object
      filter_objects_per_label[label_name] = [filter_object]

    return filter_objects_per_label
    def GetEventTaggingRules(self):
        """Retrieves the event tagging rules from the tagging file.

    Returns:
      dict[str, FilterObject]: tagging rules, that consists of one or more
          filter objects per label.

    Raises:
      TaggingFileError: if a filter expression cannot be compiled.
    """
        tagging_rules = {}

        label_name = None
        with io.open(self._path, 'r', encoding='utf-8') as tagging_file:
            for line in tagging_file.readlines():
                line = line.rstrip()

                stripped_line = line.lstrip()
                if not stripped_line or stripped_line[0] == '#':
                    continue

                if not line[0].isspace():
                    label_name = line
                    tagging_rules[label_name] = []
                    continue

                if not label_name:
                    continue

                filter_object = event_filter.EventObjectFilter()

                try:
                    filter_object.CompileFilter(stripped_line)
                except errors.ParseError as exception:
                    raise errors.TaggingFileError((
                        'Unable to compile filter for label: {0:s} with error: '
                        '{1!s}').format(label_name, exception))

                if filter_object not in tagging_rules[label_name]:
                    tagging_rules[label_name].append(filter_object)

        return tagging_rules