<s> import json <EOL> import warnings <EOL> from django . utils . translation import ugettext as _ <EOL> from jsonobject . exceptions import BadValueError , WrappingAttributeError <EOL> from corehq . apps . userreports . expressions . factory import ExpressionFactory <EOL> from corehq . apps . userreports . filters . specs import ( PropertyMatchFilterSpec , NotFilterSpec , NamedFilterSpec , <EOL> BooleanExpressionFilterSpec ) <EOL> from corehq . apps . userreports . exceptions import BadSpecError <EOL> from corehq . apps . userreports . filters import ANDFilter , ORFilter , NOTFilter , SinglePropertyValueFilter <EOL> from corehq . apps . userreports . operators import equal , get_operator <EOL> def _build_compound_filter ( spec , context ) : <EOL> compound_type_map = { <EOL> '<STR_LIT>' : ORFilter , <EOL> '<STR_LIT>' : ANDFilter , <EOL> } <EOL> if spec [ '<STR_LIT:type>' ] not in compound_type_map : <EOL> raise BadSpecError ( _ ( '<STR_LIT>' ) . format ( <EOL> spec [ '<STR_LIT:type>' ] , <EOL> '<STR_LIT:U+002CU+0020>' . join ( compound_type_map . keys ( ) ) <EOL> ) ) <EOL> elif not isinstance ( spec . get ( '<STR_LIT>' ) , list ) : <EOL> raise BadSpecError ( _ ( '<STR_LIT>' . format ( spec [ '<STR_LIT:type>' ] ) ) ) <EOL> filters = [ FilterFactory . from_spec ( subspec , context ) for subspec in spec [ '<STR_LIT>' ] ] <EOL> return compound_type_map [ spec [ '<STR_LIT:type>' ] ] ( filters ) <EOL> def _build_not_filter ( spec , context ) : <EOL> wrapped = NotFilterSpec . wrap ( spec ) <EOL> return NOTFilter ( FilterFactory . from_spec ( wrapped . filter , context ) ) <EOL> def _build_property_match_filter ( spec , context ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , <EOL> ) <EOL> wrapped = PropertyMatchFilterSpec . wrap ( spec ) <EOL> return SinglePropertyValueFilter ( <EOL> expression = wrapped . getter , <EOL> operator = equal , <EOL> reference_expression = ExpressionFactory . from_spec ( wrapped . property_value ) , <EOL> ) <EOL> def _build_boolean_expression_filter ( spec , context ) : <EOL> wrapped = BooleanExpressionFilterSpec . wrap ( spec ) <EOL> return SinglePropertyValueFilter ( <EOL> expression = ExpressionFactory . from_spec ( wrapped . expression , context ) , <EOL> operator = get_operator ( wrapped . operator ) , <EOL> reference_expression = ExpressionFactory . from_spec ( wrapped . property_value , context ) , <EOL> ) <EOL> def _build_named_filter ( spec , context ) : <EOL> wrapped = NamedFilterSpec . wrap ( spec ) <EOL> return context . named_filters [ wrapped . name ] <EOL> class FilterFactory ( object ) : <EOL> constructor_map = { <EOL> '<STR_LIT>' : _build_property_match_filter , <EOL> '<STR_LIT>' : _build_boolean_expression_filter , <EOL> '<STR_LIT>' : _build_compound_filter , <EOL> '<STR_LIT>' : _build_compound_filter , <EOL> '<STR_LIT>' : _build_not_filter , <EOL> '<STR_LIT>' : _build_named_filter , <EOL> } <EOL> @ classmethod <EOL> def from_spec ( cls , spec , context = None ) : <EOL> cls . validate_spec ( spec ) <EOL> try : <EOL> return cls . constructor_map [ spec [ '<STR_LIT:type>' ] ] ( spec , context ) <EOL> except ( AssertionError , BadValueError , WrappingAttributeError ) as e : <EOL> raise BadSpecError ( _ ( '<STR_LIT>' ) . format ( <EOL> json . dumps ( spec , indent = <NUM_LIT:2> ) , <EOL> str ( e ) , <EOL> ) ) <EOL> @ classmethod <EOL> def validate_spec ( self , spec ) : <EOL> if spec . get ( '<STR_LIT:type>' ) not in self . constructor_map : <EOL> raise BadSpecError ( <EOL> _ ( '<STR_LIT>' . format ( <EOL> spec . get ( '<STR_LIT:type>' ) , <EOL> '<STR_LIT:U+002CU+0020>' . join ( self . constructor_map . keys ( ) ) <EOL> ) ) <EOL> ) </s>
