<s> import simplejson <EOL> import cgi <EOL> class JSONFilter ( object ) : <EOL> def __init__ ( self , app , mime_type = '<STR_LIT>' ) : <EOL> self . app = app <EOL> self . mime_type = mime_type <EOL> def __call__ ( self , environ , start_response ) : <EOL> response = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : [ ] } <EOL> def json_start_response ( status , headers ) : <EOL> response [ '<STR_LIT:status>' ] = status <EOL> response [ '<STR_LIT>' ] . extend ( headers ) <EOL> environ [ '<STR_LIT>' ] = self . mime_type <EOL> if environ . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT:POST>' : <EOL> if environ . get ( '<STR_LIT>' , '<STR_LIT>' ) == self . mime_type : <EOL> args = [ _ for _ in [ environ . get ( '<STR_LIT>' ) ] if _ ] <EOL> data = environ [ '<STR_LIT>' ] . read ( * map ( int , args ) ) <EOL> environ [ '<STR_LIT>' ] = simplejson . loads ( data ) <EOL> res = simplejson . dumps ( self . app ( environ , json_start_response ) ) <EOL> jsonp = cgi . parse_qs ( environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) . get ( '<STR_LIT>' ) <EOL> if jsonp : <EOL> content_type = '<STR_LIT>' <EOL> res = '<STR_LIT>' . join ( jsonp + [ '<STR_LIT:(>' , res , '<STR_LIT:)>' ] ) <EOL> elif '<STR_LIT>' in environ . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> content_type = '<STR_LIT>' <EOL> else : <EOL> content_type = self . mime_type <EOL> headers = [ <EOL> ( '<STR_LIT>' , content_type ) , <EOL> ( '<STR_LIT>' , len ( res ) ) , <EOL> ] <EOL> headers . extend ( response [ '<STR_LIT>' ] ) <EOL> start_response ( response [ '<STR_LIT:status>' ] , headers ) <EOL> return [ res ] <EOL> def factory ( app , global_conf , ** kw ) : <EOL> return JSONFilter ( app , ** kw ) </s>
