def from_structure(cls, structure: JsonStructure) -> "Predicate": operators = tuple(filter(Predicate.Operator.has_value, structure.keys())) if len(operators) != 1: raise Predicate.InvalidPredicateOperator( "Each predicate must define exactly one operator." ) operator = operators[0] predicate = cls(operator=operator, case_sensitive=structure.get("caseSensitive", True)) predicate.fields_from_structure(structure[operator]) if "xpath" in structure: predicate.xpath = structure["xpath"]["selector"] return predicate
def from_structure(structure: JsonStructure) -> "BasePredicate": if "and" in structure: return AndPredicate.from_structure(structure) elif "or" in structure: return OrPredicate.from_structure(structure) elif "contains" in structure and "data" in structure["contains"]: return TcpPredicate.from_structure(structure) elif "inject" in structure: return InjectionPredicate.from_structure(structure) elif set(structure.keys()).intersection( {o.value for o in Predicate.Operator}): # pragma: no cover return Predicate.from_structure(structure) raise NotImplementedError() # pragma: no cover