<s> """<STR_LIT>""" <EOL> from BaseHTTPServer import BaseHTTPRequestHandler , HTTPServer <EOL> from SocketServer import ThreadingMixIn <EOL> import json <EOL> import traceback <EOL> import urlparse <EOL> from porkchop . plugin import PorkchopPluginHandler <EOL> class GetHandler ( BaseHTTPRequestHandler ) : <EOL> def format_output ( self , fmt , data ) : <EOL> if fmt == '<STR_LIT>' : <EOL> return json . dumps ( data ) <EOL> else : <EOL> return '<STR_LIT:\n>' . join ( self . json_path ( data ) ) <EOL> def json_path ( self , data ) : <EOL> results = [ ] <EOL> def path_helper ( data , path , results ) : <EOL> for key , val in data . iteritems ( ) : <EOL> if isinstance ( val , dict ) : <EOL> path_helper ( val , '<STR_LIT:/>' . join ( ( path , key ) ) , results ) <EOL> else : <EOL> results . append ( ( '<STR_LIT>' % ( ( '<STR_LIT:/>' . join ( ( path , key ) ) ) . replace ( '<STR_LIT:.>' , '<STR_LIT:_>' ) , val ) ) ) <EOL> path_helper ( data , '<STR_LIT>' , results ) <EOL> return results <EOL> def do_GET ( self ) : <EOL> data = { } <EOL> formats = { '<STR_LIT>' : '<STR_LIT:application/json>' , '<STR_LIT:text>' : '<STR_LIT>' } <EOL> request = urlparse . urlparse ( self . path ) <EOL> try : <EOL> ( path , fmt ) = request . path . split ( '<STR_LIT:.>' ) <EOL> if fmt not in formats . keys ( ) : <EOL> fmt = '<STR_LIT:text>' <EOL> except ValueError : <EOL> path = request . path <EOL> if self . headers . get ( '<STR_LIT>' , False ) == '<STR_LIT:application/json>' : <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> fmt = '<STR_LIT:text>' <EOL> if self . headers . get ( '<STR_LIT>' , False ) : <EOL> force_refresh = True <EOL> else : <EOL> force_refresh = False <EOL> module = path . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> ] <EOL> try : <EOL> if module : <EOL> plugin = PorkchopPluginHandler . plugins [ module ] ( self ) <EOL> plugin . force_refresh = force_refresh <EOL> self . log_message ( '<STR_LIT>' % ( module , force_refresh ) ) <EOL> data . update ( { module : plugin . data } ) <EOL> else : <EOL> for plugin_name , plugin in PorkchopPluginHandler . plugins . iteritems ( ) : <EOL> try : <EOL> plugin . force_refresh = force_refresh <EOL> self . log_message ( '<STR_LIT>' % ( plugin_name , force_refresh ) ) <EOL> result = plugin ( self ) . data <EOL> if len ( result ) > <NUM_LIT:1> : <EOL> data . update ( { plugin_name : result } ) <EOL> except Exception , e : <EOL> self . log_error ( '<STR_LIT>' , plugin_name , e , <EOL> traceback . format_exc ( ) ) <EOL> if len ( data ) : <EOL> self . send_response ( <NUM_LIT:200> ) <EOL> self . send_header ( '<STR_LIT:Content-Type>' , formats [ fmt ] ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( self . format_output ( fmt , data ) + '<STR_LIT:\n>' ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> except Exception , e : <EOL> self . log_error ( '<STR_LIT>' , e , traceback . format_exc ( ) ) <EOL> self . send_response ( <NUM_LIT> ) <EOL> self . send_header ( '<STR_LIT:Content-Type>' , formats [ fmt ] ) <EOL> self . end_headers ( ) <EOL> self . wfile . write ( self . format_output ( fmt , { } ) ) <EOL> class ThreadedHTTPServer ( ThreadingMixIn , HTTPServer ) : <EOL> pass </s>
