def make_expr(prop_set, rhs, boolean_properties=None): """Create an AST that returns the value ``status`` given all the properties in prop_set match. :param prop_set: tuple of (property name, value) pairs for each property in this expression and the value it must match :param status: Status on RHS when all the given properties match :param boolean_properties: Set of properties in property_order that should be treated as boolean. """ root = ConditionalNode() assert len(prop_set) > 0 expressions = [] for prop, value in prop_set: number_types = (int, float, long) value_cls = (NumberNode if type(value) in number_types else StringNode) if prop not in boolean_properties: expressions.append( BinaryExpressionNode( BinaryOperatorNode("=="), VariableNode(prop), value_cls(unicode(value)) )) else: if value: expressions.append(VariableNode(prop)) else: expressions.append( UnaryExpressionNode( UnaryOperatorNode("not"), VariableNode(prop) )) if len(expressions) > 1: prev = expressions[-1] for curr in reversed(expressions[:-1]): node = BinaryExpressionNode( BinaryOperatorNode("and"), curr, prev) prev = node else: node = expressions[0] root.append(node) if type(rhs) in number_types: rhs_node = NumberNode(rhs) else: rhs_node = StringNode(rhs) root.append(rhs_node) return root
def make_expr(prop_set, rhs): """Create an AST that returns the value ``status`` given all the properties in prop_set match. :param prop_set: tuple of (property name, value) pairs for each property in this expression and the value it must match :param status: Status on RHS when all the given properties match """ root = ConditionalNode() assert len(prop_set) > 0 expressions = [] for prop, value in prop_set: if value not in (True, False): expressions.append( BinaryExpressionNode( BinaryOperatorNode("=="), VariableNode(prop), make_node(value)) ) else: if value: expressions.append(VariableNode(prop)) else: expressions.append( UnaryExpressionNode( UnaryOperatorNode("not"), VariableNode(prop) )) if len(expressions) > 1: prev = expressions[-1] for curr in reversed(expressions[:-1]): node = BinaryExpressionNode( BinaryOperatorNode("and"), curr, prev) prev = node else: node = expressions[0] root.append(node) rhs_node = make_value_node(rhs) root.append(rhs_node) return root
def make_expr(prop_set, status): """Create an AST that returns the value ``status`` given all the properties in prop_set match.""" root = ConditionalNode() assert len(prop_set) > 0 no_value_props = set(["debug"]) expressions = [] for prop, value in prop_set: number_types = (int, float, long) value_cls = (NumberNode if type(value) in number_types else StringNode) if prop not in no_value_props: expressions.append( BinaryExpressionNode( BinaryOperatorNode("=="), VariableNode(prop), value_cls(unicode(value)) )) else: if value: expressions.append(VariableNode(prop)) else: expressions.append( UnaryExpressionNode( UnaryOperatorNode("not"), VariableNode(prop) )) if len(expressions) > 1: prev = expressions[-1] for curr in reversed(expressions[:-1]): node = BinaryExpressionNode( BinaryOperatorNode("and"), curr, prev) prev = node else: node = expressions[0] root.append(node) root.append(StringNode(status)) return root