sdcclient/_monitor.py
def convert_scope_string_to_expression(scope):
        '''**Description**
            Internal function to convert a filter string to a filter object to be used with dashboards.
        '''
        #
        # NOTE: The supported grammar is not perfectly aligned with the grammar supported by the Sysdig backend.
        # Proper grammar implementation will happen soon.
        # For practical purposes, the parsing will have equivalent results.
        #

        if scope is None or not scope:
            return [True, []]

        expressions = []
        string_expressions = scope.strip(' \t\n\r').split(' and ')
        expression_re = re.compile('^(?P<not>not )?(?P<operand>[^ ]+) (?P<operator>=|!=|in|contains|starts with) (?P<value>(:?"[^"]+"|\'[^\']+\'|\(.+\)|.+))$')

        for string_expression in string_expressions:
            matches = expression_re.match(string_expression)

            if matches is None:
                return [False, 'invalid scope format']

            is_not_operator = matches.group('not') is not None

            if matches.group('operator') == 'in':
                list_value = matches.group('value').strip(' ()')
                value_matches = re.findall('(:?\'[^\',]+\')|(:?"[^",]+")|(:?[,]+)', list_value)

                if len(value_matches) == 0:
                    return [False, 'invalid scope value list format']

                value_matches = map(lambda v: v[0] if v[0] else v[1], value_matches)
                values = map(lambda v: v.strip(' "\''), value_matches)
            else:
                values = [matches.group('value').strip('"\'')]

            operator_parse_dict = {
                'in': 'in' if not is_not_operator else 'notIn',
                '=': 'equals' if not is_not_operator else 'notEquals',
                '!=': 'notEquals' if not is_not_operator else 'equals',
                'contains': 'contains' if not is_not_operator else 'notContains',
                'starts with': 'startsWith'
            }

            operator = operator_parse_dict.get(matches.group('operator'), None)
            if operator is None:
                return [False, 'invalid scope operator']

            expressions.append({
                'operand': matches.group('operand'),
                'operator': operator,
                'value': values
            })

        return [True, expressions]
def convert_scope_string_to_expression(scope):
        '''**Description**
            Internal function to convert a filter string to a filter object to be used with dashboards.
        '''
        #
        # NOTE: The supported grammar is not perfectly aligned with the grammar supported by the Sysdig backend.
        # Proper grammar implementation will happen soon.
        # For practical purposes, the parsing will have equivalent results.
        #

        if scope is None or not scope:
            return [True, []]

        expressions = []
        string_expressions = scope.strip(' \t\n\r').split(' and ')
        expression_re = re.compile('^(?P<not>not )?(?P<operand>[^ ]+) (?P<operator>=|!=|in|contains|starts with) (?P<value>(:?"[^"]+"|\'[^\']+\'|\(.+\)|.+))$')

        for string_expression in string_expressions:
            matches = expression_re.match(string_expression)

            if matches is None:
                return [False, 'invalid scope format']

            is_not_operator = matches.group('not') is not None

            if matches.group('operator') == 'in':
                list_value = matches.group('value').strip(' ()')
                value_matches = re.findall('(:?\'[^\',]+\')|(:?"[^",]+")|(:?[,]+)', list_value)

                if len(value_matches) == 0:
                    return [False, 'invalid scope value list format']

                value_matches = map(lambda v: v[0] if v[0] else v[1], value_matches)
                values = map(lambda v: v.strip(' "\''), value_matches)
            else:
                values = [matches.group('value').strip('"\'')]

            operator_parse_dict = {
                'in': 'in' if not is_not_operator else 'notIn',
                '=': 'equals' if not is_not_operator else 'notEquals',
                '!=': 'notEquals' if not is_not_operator else 'equals',
                'contains': 'contains' if not is_not_operator else 'notContains',
                'starts with': 'startsWith'
            }

            operator = operator_parse_dict.get(matches.group('operator'), None)
            if operator is None:
                return [False, 'invalid scope operator']

            expressions.append({
                'operand': matches.group('operand'),
                'operator': operator,
                'value': values
            })

        return [True, expressions]
def convert_scope_string_to_expression ( scope ) : # # NOTE: The supported grammar is not perfectly aligned with the grammar supported by the Sysdig backend. # Proper grammar implementation will happen soon. # For practical purposes, the parsing will have equivalent results. # if scope is None or not scope : return [ True , [ ] ] expressions = [ ] string_expressions = scope . strip ( ' \t\n\r' ) . split ( ' and ' ) expression_re = re . compile ( '^(?P<not>not )?(?P<operand>[^ ]+) (?P<operator>=|!=|in|contains|starts with) (?P<value>(:?"[^"]+"|\'[^\']+\'|\(.+\)|.+))$' ) for string_expression in string_expressions : matches = expression_re . match ( string_expression ) if matches is None : return [ False , 'invalid scope format' ] is_not_operator = matches . group ( 'not' ) is not None if matches . group ( 'operator' ) == 'in' : list_value = matches . group ( 'value' ) . strip ( ' ()' ) value_matches = re . findall ( '(:?\'[^\',]+\')|(:?"[^",]+")|(:?[,]+)' , list_value ) if len ( value_matches ) == 0 : return [ False , 'invalid scope value list format' ] value_matches = map ( lambda v : v [ 0 ] if v [ 0 ] else v [ 1 ] , value_matches ) values = map ( lambda v : v . strip ( ' "\'' ) , value_matches ) else : values = [ matches . group ( 'value' ) . strip ( '"\'' ) ] operator_parse_dict = { 'in' : 'in' if not is_not_operator else 'notIn' , '=' : 'equals' if not is_not_operator else 'notEquals' , '!=' : 'notEquals' if not is_not_operator else 'equals' , 'contains' : 'contains' if not is_not_operator else 'notContains' , 'starts with' : 'startsWith' } operator = operator_parse_dict . get ( matches . group ( 'operator' ) , None ) if operator is None : return [ False , 'invalid scope operator' ] expressions . append ( { 'operand' : matches . group ( 'operand' ) , 'operator' : operator , 'value' : values } ) return [ True , expressions ]
**Description**
            Internal function to convert a filter string to a filter object to be used with dashboards.
** Description ** Internal function to convert a filter string to a filter object to be used with dashboards .