<s> from six import StringIO <EOL> from greplin import scales <EOL> from greplin . scales import formats , util <EOL> from bottle import abort , request , response , run , Bottle <EOL> import functools <EOL> def bottlestats ( server_name , path = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> path = path . lstrip ( '<STR_LIT:/>' ) <EOL> parts = path . split ( '<STR_LIT:/>' ) <EOL> if not parts [ <NUM_LIT:0> ] : <EOL> parts = parts [ <NUM_LIT:1> : ] <EOL> stat_dict = util . lookup ( scales . getStats ( ) , parts ) <EOL> if stat_dict is None : <EOL> abort ( <NUM_LIT> , "<STR_LIT>" ) <EOL> return <EOL> output = StringIO ( ) <EOL> output_format = request . query . get ( '<STR_LIT>' , '<STR_LIT:html>' ) <EOL> query = request . query . get ( '<STR_LIT>' , None ) <EOL> if output_format == '<STR_LIT>' : <EOL> response . content_type = "<STR_LIT:application/json>" <EOL> formats . jsonFormat ( output , stat_dict , query ) <EOL> elif output_format == '<STR_LIT>' : <EOL> formats . jsonFormat ( output , stat_dict , query , pretty = True ) <EOL> response . content_type = "<STR_LIT:application/json>" <EOL> else : <EOL> formats . htmlHeader ( output , '<STR_LIT:/>' + path , server_name , query ) <EOL> formats . htmlFormat ( output , tuple ( parts ) , stat_dict , query ) <EOL> response . content_type = "<STR_LIT>" <EOL> return output . getvalue ( ) <EOL> def register_stats_handler ( app , server_name , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not prefix . endswith ( '<STR_LIT:/>' ) : <EOL> prefix += '<STR_LIT:/>' <EOL> handler = functools . partial ( bottlestats , server_name ) <EOL> app . get ( prefix , callback = handler ) <EOL> app . get ( prefix + '<STR_LIT>' , callback = handler ) </s>
