예제 #1
0
    def _NegateExpression(self, **unused_kwargs):
        """Reverses the logic of (negates) the current expression.

    Raises:
      ParseError: when the negation keyword (not) is expressed more than once,
          used after an argument or before an operator that does not support
          negation.
    """
        if self._have_negate_keyword:
            raise errors.ParseError(
                'Negation keyword (not) can only be expressed once.')

        if self._current_expression.args:
            raise errors.ParseError(
                'Negation keyword (not) cannot be used after an argument.')

        operator = self._current_expression.operator
        if operator and operator.lower() not in self._OPERATORS_WITH_NEGATION:
            raise errors.ParseError(
                'Operator: {0:s} does not support negation.'.format(operator))

        self._have_negate_keyword = True

        logger.debug('Negating expression')
        self._current_expression.Negate()
예제 #2
0
    def _AddArgument(self, value):
        """Adds an argument to the current expression.

    Args:
      value (object): argument value.

    Returns:
      str: state or None if the argument could not be added to the current
          expression.

    Raises:
      ParseError: if the operator does not support negation.
    """
        logger.debug('Storing argument: {0!s}'.format(value))

        if self._have_negate_keyword:
            operator = self._current_expression.operator
            if operator and operator.lower(
            ) not in self._OPERATORS_WITH_NEGATION:
                raise errors.ParseError(
                    'Operator: {0:s} does not support negation (not).'.format(
                        operator))

        # This expression is complete
        if self._current_expression.AddArgument(value):
            self._stack.append(self._current_expression)
            self._current_expression = expressions.EventExpression()
            # We go to the BINARY state, to find if there's an AND or OR operator
            return self._STATE_BINARY_OPERATOR

        return None
예제 #3
0
    def __init__(self, arguments=None):
        """Initializes a filter.

    Implementations expanders are provided by subclassing ValueExpander.

    Args:
      arguments (Optional[object]): arguments.
    """
        logger.debug('Adding {0!s}'.format(arguments))

        super(Filter, self).__init__()
        self.args = arguments or []
예제 #4
0
    def _PushState(self, **unused_kwargs):
        """Pushes the current state on the state stack.

    Note that this function is used as a callback by _GetNextToken.

    Returns:
      str: next state, which is None.
    """
        logger.debug('Storing state {0:s}'.format(repr(self._state)))
        self._state_stack.append(self._state)

        return None
예제 #5
0
    def _SetOperator(self, string='', **unused_kwargs):
        """Sets the operator in the current expression.

    Note that this function is used as a callback by _GetNextToken.

    Args:
      string (Optional[str]): operator.

    Returns:
      str: next state, which is None.
    """
        logger.debug('Storing operator {0:s}'.format(repr(string)))
        self._current_expression.SetOperator(string)

        return None
예제 #6
0
    def _SetAttribute(self, string='', **unused_kwargs):
        """Sets the attribute in the current expression.

    Note that this function is used as a callback by _GetNextToken.

    Args:
      string (Optional[str]): attribute.

    Returns:
      str: next state, which is the operator state.
    """
        logger.debug('Storing attribute {0:s}'.format(repr(string)))

        self._current_expression.SetAttribute(string)

        self._have_negate_keyword = False

        return self._STATE_OPERATOR
예제 #7
0
    def _PopState(self, **unused_kwargs):
        """Pops the previous state from the stack.

    Returns:
      str: next state, which is the previous state on the stack.

    Raises:
      ParseError: if the stack is empty.
    """
        try:
            self._state = self._state_stack.pop()
        except IndexError:
            raise errors.ParseError((
                'Tried to pop state from an empty stack - possible recursion error '
                'at position {0!s}: {1!s} <---> {2!s} )').format(
                    len(self._processed_buffer), self._processed_buffer,
                    self._buffer))

        logger.debug('Returned state to {0:s}'.format(self._state))
        return self._state
예제 #8
0
    def HexEscape(self, string, match, **unused_kwargs):
        """Converts a hex escaped string.

    Note that this function is used as a callback by _GetNextToken.

    Returns:
      str: next state, which is None.

    Raises:
      ParseError: if the string is not hex escaped.
    """
        logger.debug('HexEscape matched {0:s}.'.format(string))
        hex_string = match.group(1)
        try:
            hex_string = binascii.unhexlify(hex_string)
            hex_string = codecs.decode(hex_string, 'utf-8')
            self._string += hex_string
        except (TypeError, binascii.Error):
            raise errors.ParseError(
                'Invalid hex escape {0!s}.'.format(hex_string))

        return None
예제 #9
0
    def __init__(self, arguments=None, **kwargs):
        """Initializes a regular expression operator.

    This operator uses case insensitive comparison.

    Args:
      arguments (Optional[object]): operands of the filter.

    Raises:
      ValueError: if the regular expression is malformed.
    """
        super(RegexpInsensitive, self).__init__(arguments=arguments, **kwargs)

        # Note that right_operand is not necessarily a string.
        logger.debug('Compiled: {0!s}'.format(self.right_operand))

        try:
            expression = self._CopyValueToString(self.right_operand)
            compiled_re = re.compile(expression, re.I | re.DOTALL)
        except re.error:
            raise ValueError('Regular expression "{0!s}" is malformed.'.format(
                self.right_operand))

        self.compiled_re = compiled_re
예제 #10
0
 def FlipBool(self):
     """Negates the internal boolean value attribute."""
     logger.debug('Negative matching.')
     self._bool_value = not self._bool_value
예제 #11
0
    def _NoOperation(self, **kwarg):
        """No operation.

    Note that this function is used as a callback by _GetNextToken.
    """
        logger.debug('Default handler: {0!s}'.format(kwarg))