<s> import os , sys , time <EOL> import simplejson as json <EOL> from paste . request import parse_formvars <EOL> import zohmg . data <EOL> from zohmg . config import Config <EOL> def json_of ( error_msg = '<STR_LIT>' , status_code = <NUM_LIT> , callback = None ) : <EOL> structure = { '<STR_LIT>' : error_msg , '<STR_LIT>' : status_code } <EOL> jsondata = json . dumps ( structure ) <EOL> if callback : <EOL> return callback + "<STR_LIT:(>" + jsondata + "<STR_LIT:)>" <EOL> else : <EOL> return jsondata <EOL> class data ( object ) : <EOL> def __init__ ( self , dataset = None , projections = None ) : <EOL> if dataset == None or projections == None : <EOL> config = Config ( ) <EOL> if dataset == None : <EOL> self . table = config . dataset ( ) <EOL> else : <EOL> self . table = dataset <EOL> if projections == None : <EOL> self . projections = config . projections ( ) <EOL> else : <EOL> self . projections = projections <EOL> def __call__ ( self , environ , start_response ) : <EOL> mime_type = '<STR_LIT>' <EOL> content_type = [ ( '<STR_LIT>' , mime_type ) ] <EOL> params = parse_formvars ( environ ) <EOL> try : errorcallback = params [ '<STR_LIT>' ] <EOL> except : errorcallback = None <EOL> try : <EOL> start = time . time ( ) <EOL> json = zohmg . data . query ( self . table , self . projections , params ) <EOL> except zohmg . data . DataNotFound , ( instance ) : <EOL> print >> sys . stderr , "<STR_LIT>" , instance . error <EOL> start_response ( '<STR_LIT>' , content_type ) <EOL> return json_of ( "<STR_LIT>" + instance . error , <NUM_LIT:200> , errorcallback ) <EOL> except zohmg . data . MissingArguments , ( instance ) : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> start_response ( '<STR_LIT>' , content_type ) <EOL> return json_of ( "<STR_LIT>" + instance . error , <NUM_LIT> , errorcallback ) <EOL> except zohmg . data . NoSuitableProjection , ( instance ) : <EOL> print >> sys . stderr , "<STR_LIT>" , instance . error <EOL> start_response ( '<STR_LIT>' , content_type ) <EOL> return json_of ( "<STR_LIT:U+0020>" + instance . error , <NUM_LIT> , errorcallback ) <EOL> except Exception , e : <EOL> print >> sys . stderr , "<STR_LIT>" , e <EOL> start_response ( '<STR_LIT>' , content_type ) <EOL> return json_of ( "<STR_LIT>" + str ( e ) , <NUM_LIT> , errorcallback ) <EOL> elapsed = ( time . time ( ) - start ) <EOL> sys . stderr . write ( "<STR_LIT>" % elapsed ) <EOL> start_response ( '<STR_LIT>' , content_type ) <EOL> return json </s>
