<s> import functools <EOL> import datetime <EOL> from . exceptions import EmitterValidationError <EOL> REDUCE_TYPES = set ( [ '<STR_LIT>' , '<STR_LIT:count>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class base_emitter ( object ) : <EOL> fluff_emitter = '<STR_LIT>' <EOL> def __init__ ( self , reduce_type = '<STR_LIT>' ) : <EOL> assert reduce_type in REDUCE_TYPES , '<STR_LIT>' <EOL> self . reduce_type = reduce_type <EOL> def __call__ ( self , fn ) : <EOL> @ functools . wraps ( fn ) <EOL> def wrapped_f ( * args ) : <EOL> generator = fn ( * args ) <EOL> for v in generator : <EOL> if isinstance ( v , dict ) : <EOL> if '<STR_LIT:value>' not in v : <EOL> v [ '<STR_LIT:value>' ] = <NUM_LIT:1> <EOL> if v . get ( '<STR_LIT>' ) is None : <EOL> v [ '<STR_LIT>' ] = None <EOL> elif isinstance ( v [ '<STR_LIT>' ] , tuple ) : <EOL> v [ '<STR_LIT>' ] = list ( v [ '<STR_LIT>' ] ) <EOL> elif not isinstance ( v [ '<STR_LIT>' ] , list ) : <EOL> v [ '<STR_LIT>' ] = [ v [ '<STR_LIT>' ] ] <EOL> elif isinstance ( v , list ) : <EOL> v = dict ( date = v [ <NUM_LIT:0> ] , value = v [ <NUM_LIT:1> ] , group_by = None ) <EOL> else : <EOL> v = dict ( date = v , value = <NUM_LIT:1> , group_by = None ) <EOL> try : <EOL> self . validate ( v ) <EOL> except EmitterValidationError as e : <EOL> generator . throw ( e ) <EOL> yield v <EOL> wrapped_f . _reduce_type = self . reduce_type <EOL> wrapped_f . _fluff_emitter = self . fluff_emitter <EOL> return wrapped_f <EOL> def validate ( self , value ) : <EOL> pass <EOL> class custom_date_emitter ( base_emitter ) : <EOL> fluff_emitter = '<STR_LIT:date>' <EOL> def validate ( self , value ) : <EOL> def validate_date ( dateval ) : <EOL> if not isinstance ( dateval , ( datetime . date , datetime . datetime ) ) : <EOL> raise EmitterValidationError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( <EOL> dateval <EOL> ) <EOL> ) <EOL> validate_date ( value . get ( '<STR_LIT:date>' ) ) <EOL> if isinstance ( value [ '<STR_LIT:date>' ] , datetime . datetime ) : <EOL> value [ '<STR_LIT:date>' ] = value [ '<STR_LIT:date>' ] . date ( ) <EOL> class custom_null_emitter ( base_emitter ) : <EOL> fluff_emitter = '<STR_LIT:null>' <EOL> def validate ( self , value ) : <EOL> if isinstance ( value , dict ) : <EOL> if '<STR_LIT:date>' not in value : <EOL> value [ '<STR_LIT:date>' ] = None <EOL> else : <EOL> if value [ '<STR_LIT:date>' ] is not None : <EOL> raise EmitterValidationError ( <EOL> '<STR_LIT>' . format ( value [ '<STR_LIT:date>' ] ) ) <EOL> date_emitter = custom_date_emitter ( ) <EOL> null_emitter = custom_null_emitter ( ) </s>
