Beispiel #1
0
 def __init__(self, expression, delimiter):
     # expression is a list of at least three tuples, of which first element
     # is a string tag, second is subelement value; other tuples apparently
     # are not used.
     # expression[0][1] effectively contains export path and apparently must
     # be treated as such, also left hand operand in comparison
     # expression[1][1] appa holds commparison operator == or !=
     # expression[2][1] is the righhand operand
     super(EqualityTest, self).__init__(delimiter)
     # TODO: this double sommersault must be cleaned
     _ = self._get_vars(expression[2][1], *self._get_vars(expression[0][1]))
     self._export_path, self._parameter_path, self._parameter_value = _
     try:
         self._export_path.drop_first()
     except AttributeError:
         raise ExpressionError('No export')
     try:
         self._compare = self.known_operators[expression[1][1]]
     except KeyError as e:
         msg = 'Unknown test {0}'.format(expression[1][1])
         raise ExpressionError(msg, tbFlag=False)
     self.inv_refs = [self._export_path]
     if self._parameter_path is not None:
         self._parameter_path.drop_first()
         self.refs = [str(self._parameter_path)]
Beispiel #2
0
 def render(self, context, inventory):
     if self._expr_type == parser_funcs.VALUE:
         return self._value_expression(inventory)
     elif self._expr_type == parser_funcs.TEST:
         return self._test_expression(context, inventory)
     elif self._expr_type == parser_funcs.LIST_TEST:
         return self._list_test_expression(context, inventory)
     raise ExpressionError('Failed to render %s' % str(self), tbFlag=False)
Beispiel #3
0
    def _test_expression(self, context, inventory):
        if self._value_path is None:
            ExpressionError('Failed to render %s' % str(self), tbFlag=False)

        results = {}
        for (node, items) in iteritems(inventory):
            if self._question.value(context, items) and self._value_path.exists_in(items):
                results[node] = copy.deepcopy(self._resolve(self._value_path, items))
        return results
Beispiel #4
0
 def value(self, context, items):
     if self._parameter_path is not None:
         self._parameter_value = self._resolve(self._parameter_path,
                                               context)
     if self._parameter_value is None:
         raise ExpressionError('Failed to render %s' % str(self),
                               tbFlag=False)
     if self._export_path.exists_in(items):
         export_value = self._resolve(self._export_path, items)
         return self._compare(export_value, self._parameter_value)
     return False
Beispiel #5
0
    def value(self, context, items):
        if self._parameter_path is not None:
            self._parameter_value = self._resolve(self._parameter_path, context)

        if self._parameter_value is None or self._test is None:
            raise ExpressionError('Failed to render %s' % str(self), tbFlag=False)

        if self._export_path.exists_in(items):
            result = False
            export_value = self._resolve(self._export_path, items)
            if self._test == _EQUAL:
                if export_value == self._parameter_value:
                    result = True
            elif self._test == _NOT_EQUAL:
                if export_value != self._parameter_value:
                    result = True
            else:
                raise ExpressionError('Unknown test {0}'.format(self._test), tbFlag=False)
            return result
        else:
            return False
Beispiel #6
0
    def _parse_expression(self, expr):
        parser = parser_funcs.get_expression_parser()
        try:
            tokens = parser.parseString(expr).asList()
        except pp.ParseException as e:
            raise ParseError(e.msg, e.line, e.col, e.lineno)

        if len(tokens) == 2:  # options are set
            passed_opts = [x[1] for x in tokens.pop(0)]
            self.ignore_failed_render = parser_funcs.IGNORE_ERRORS in passed_opts
            self.needs_all_envs = parser_funcs.ALL_ENVS in passed_opts
        elif len(tokens) > 2:
            raise ExpressionError('Failed to parse %s' % str(tokens),
                                  tbFlag=False)
        self._expr_type = tokens[0][0]
        self._expr = list(tokens[0][1])

        if self._expr_type == parser_funcs.VALUE:
            self._value_path = DictPath(self._settings.delimiter,
                                        self._expr[0][1]).drop_first()
            self._question = LogicTest([], self._settings.delimiter)
            self.refs = []
            self.inv_refs = [self._value_path]
        elif self._expr_type == parser_funcs.TEST:
            self._value_path = DictPath(self._settings.delimiter,
                                        self._expr[0][1]).drop_first()
            self._question = LogicTest(self._expr[2:],
                                       self._settings.delimiter)
            self.refs = self._question.refs
            self.inv_refs = self._question.inv_refs
            self.inv_refs.append(self._value_path)
        elif self._expr_type == parser_funcs.LIST_TEST:
            self._value_path = None
            self._question = LogicTest(self._expr[1:],
                                       self._settings.delimiter)
            self.refs = self._question.refs
            self.inv_refs = self._question.inv_refs
        else:
            msg = 'Unknown expression type: %s'
            raise ExpressionError(msg % self._expr_type, tbFlag=False)
Beispiel #7
0
    def _test_expression(self, context, inventory):
        if self._value_path is None:
            msg = 'Failed to render %s'
            raise ExpressionError(msg % str(self), tbFlag=False)

        results = {}
        for name, node in iteritems(inventory):
            if self.needs_all_envs or node.env_matches:
                if (self._question.value(context, node.items)
                        and self._value_path.exists_in(node.items)):
                    answer = self._resolve(self._value_path, node.items)
                    results[name] = copy.deepcopy(answer)
        return results
Beispiel #8
0
    def _parse_expression(self, expr):
        try:
            tokens = InvItem._parser.parseString(expr).asList()
        except pp.ParseException as e:
            raise ParseError(e.msg, e.line, e.col, e.lineno)

        if len(tokens) == 1:
            self._expr_type = tokens[0][0]
            self._expr = list(tokens[0][1])
        elif len(tokens) == 2:
            for opt in tokens[0]:
                if opt[1] == _IGNORE_ERRORS:
                    self._ignore_failed_render = True
                elif opt[1] == _ALL_ENVS:
                    self._needs_all_envs = True
            self._expr_type = tokens[1][0]
            self._expr = list(tokens[1][1])
        else:
            raise ExpressionError('Failed to parse %s' % str(tokens), tbFlag=False)

        if self._expr_type == _VALUE:
            self._value_path = DictPath(self._settings.delimiter, self._expr[0][1]).drop_first()
            self._question = Question([], self._settings.delimiter)
            self._refs = []
            self._inv_refs = [ self._value_path ]
        elif self._expr_type == _TEST:
            self._value_path = DictPath(self._settings.delimiter, self._expr[0][1]).drop_first()
            self._question = Question(self._expr[2:], self._settings.delimiter)
            self._refs = self._question.refs()
            self._inv_refs = self._question.inv_refs()
            self._inv_refs.append(self._value_path)
        elif self._expr_type == _LIST_TEST:
            self._value_path = None
            self._question = Question(self._expr[1:], self._settings.delimiter)
            self._refs = self._question.refs()
            self._inv_refs = self._question.inv_refs()
        else:
            raise ExpressionError('Unknown expression type: %s' % self._expr_type, tbFlag=False)
Beispiel #9
0
 def __init__(self, expr, delimiter):
     super(LogicTest, self).__init__(delimiter)
     subtests = list(it.compress(expr, it.cycle([1, 1, 1, 0])))
     self._els = [
         EqualityTest(subtests[j:j + 3], self._delimiter)
         for j in range(0, len(subtests), 3)
     ]
     for x in self._els:
         self.refs.extend(x.refs)
         self.inv_refs.extend(x.inv_refs)
     try:
         self._ops = [self.known_operators[x[1]] for x in expr[3::4]]
     except KeyError as e:
         msg = 'Unknown operator {0} {1}'.format(e.messsage, self._els)
         raise ExpressionError(msg, tbFlag=False)
Beispiel #10
0
 def value(self, context, items):
     if len(self._elements) == 0:
         return True
     elif len(self._elements) == 1:
         return self._elements[0].value(context, items)
     else:
         result = self._elements[0].value(context, items)
         for i in range(0, len(self._elements)-1):
             next_result = self._elements[i+1].value(context, items)
             if self._operators[i] == _AND:
                 result = result and next_result
             elif self._operators[i] == _OR:
                 result = result or next_result
             else:
                 raise ExpressionError('Unknown operator {0} {1}'.format(self._operators[i], self.elements), tbFlag=False)
         return result
Beispiel #11
0
    def _parse_expression(self, expr):
        try:
            tokens = InvItem._parser.parseString(expr).asList()
        except pp.ParseException as e:
            raise ParseError(e.msg, e.line, e.col, e.lineno)

        if len(tokens) == 1:
            self._expr_type = tokens[0][0]
            self._expr = list(tokens[0][1])
        else:
            raise ExpressionError('Failed to parse %s' % str(expr))

        if self._expr_type == _TEST:
            export, parameter, value = self._get_vars(self._expr[2][1], None, None, None)
            export, parameter, value = self._get_vars(self._expr[4][1], export, parameter, value)
            if parameter is not None:
                path = parameter
                path.drop_first()
                self._refs.append(str(path))
Beispiel #12
0
    def __init__(self, expression, delimiter):
        self._delimiter = delimiter
        self._export_path = None
        self._parameter_path = None
        self._parameter_value = None
        self._export_path, self._parameter_path, self._parameter_value = self._get_vars(expression[0][1], self._export_path, self._parameter_path, self._parameter_value)
        self._export_path, self._parameter_path, self._parameter_value = self._get_vars(expression[2][1], self._export_path, self._parameter_path, self._parameter_value)

        try:
            self._export_path.drop_first()
        except AttributeError:
            raise ExpressionError('No export')

        self._inv_refs = [ self._export_path ]
        self._test = expression[1][1]

        if self._parameter_path is not None:
            self._parameter_path.drop_first()
            self._refs = [ str(self._parameter_path) ]
        else:
            self._refs = []
Beispiel #13
0
    def _test_expression(self, context, inventory):
        export_path = None
        parameter_path = None
        parameter_value = None
        test = None
        value_path = DictPath(self._delimiter, self._expr[0][1])

        if self._expr[3][1] == _EQUAL:
            test = _EQUAL
        elif self._expr[3][1] == _NOT_EQUAL:
            test = _NOT_EQUAL

        export_path, parameter_path, parameter_value = self._get_vars(self._expr[2][1], export_path, parameter_path, parameter_value)
        export_path, parameter_path, parameter_value = self._get_vars(self._expr[4][1], export_path, parameter_path, parameter_value)

        if parameter_path is not None:
            parameter_path.drop_first()
            parameter_value = self._resolve(parameter_path, context)

        if export_path is None or parameter_value is None or test is None or value_path is None:
            ExpressionError('Failed to render %s' % str(self))

        export_path.drop_first()
        value_path.drop_first()

        results = {}
        for node, items in inventory.iteritems():
            if export_path.exists_in(items):
                export_value = self._resolve(export_path, items)
                test_passed = False
                if test == _EQUAL and export_value == parameter_value:
                    test_passed = True
                elif test == _NOT_EQUAL and export_value != parameter_value:
                    test_passed = True
                if test_passed:
                    results[node] = copy.deepcopy(self._resolve(value_path, items))
        return results
Beispiel #14
0
 def render(self, context, inventory):
     if self._expr_type == _VALUE:
         return self._value_expression(inventory)
     elif self._expr_type == _TEST:
         return self._test_expression(context, inventory)
     raise ExpressionError('Failed to render %s' % str(self))