def _simple_expr_to_dto(expr_el: Element) -> CibRuleExpressionDto: string_parts = [] if "value" in expr_el.attrib: # "attribute" and "operation" are defined as mandatory in CIB schema string_parts.extend( [expr_el.get("attribute", ""), expr_el.get("operation", "")]) if "type" in expr_el.attrib: string_parts.append(expr_el.get("type", "")) string_parts.append(quote(expr_el.get("value", ""), " ")) else: # "attribute" and "operation" are defined as mandatory in CIB schema string_parts.extend( [expr_el.get("operation", ""), expr_el.get("attribute", "")]) return _common_expr_to_dto(expr_el, " ".join(string_parts))
def _simple_expr_to_str(self, expr_el: _Element) -> str: # pylint - all *_to_str methods must have the same interface # pylint: disable=no-self-use string_parts = [] if "value" in expr_el.attrib: # "attribute" and "operation" are defined as mandatory in CIB schema string_parts.extend([ str(expr_el.get("attribute", "")), str(expr_el.get("operation", "")), ]) if "type" in expr_el.attrib: string_parts.append(str(expr_el.get("type", ""))) string_parts.append(quote(str(expr_el.get("value", "")), " ")) else: # "attribute" and "operation" are defined as mandatory in CIB schema string_parts.extend([ str(expr_el.get("operation", "")), str(expr_el.get("attribute", "")), ]) return " ".join(string_parts)
def test_escape(self): self.assertEqual('''"st\\"r i'ng"''', tools.quote("st\"r i'ng", " "))
def test_alternative_quote(self): self.assertEqual("""'st"r i"ng'""", tools.quote('st"r i"ng', " "))
def test_quote(self): self.assertEqual('"str ing"', tools.quote("str ing", " =")) self.assertEqual('"str=ing"', tools.quote("str=ing", " ="))
def test_no_quote(self): self.assertEqual("string", tools.quote("string", " ")) self.assertEqual("string", tools.quote("string", " ="))