<s> import os <EOL> import json <EOL> import clastic <EOL> from clastic import Application <EOL> from clastic . render import JSONRender <EOL> from clastic . middleware import GetParamMiddleware <EOL> from clastic import Response <EOL> from clastic . sinter import getargspec <EOL> from boltons . tableutils import Table <EOL> _DATA = json . load ( open ( '<STR_LIT>' ) ) <EOL> _CUR_PATH = os . path . dirname ( os . path . abspath ( clastic . __file__ ) ) <EOL> _CA_PATH = _CUR_PATH + '<STR_LIT>' <EOL> _CSS_PATH = _CA_PATH + '<STR_LIT>' <EOL> _STYLE = open ( _CSS_PATH ) . read ( ) <EOL> def fetch_json ( url ) : <EOL> import urllib2 <EOL> response = urllib2 . urlopen ( url ) <EOL> content = response . read ( ) <EOL> data = json . loads ( content ) <EOL> return data <EOL> class AutoTableRenderer ( object ) : <EOL> _html_doctype = '<STR_LIT>' <EOL> _html_wrapper , _html_wrapper_close = '<STR_LIT>' , '<STR_LIT>' <EOL> _html_table_tag = '<STR_LIT>' <EOL> _html_style_content = _STYLE <EOL> def __init__ ( self , max_depth = <NUM_LIT:4> , orientation = '<STR_LIT>' ) : <EOL> self . max_depth = max_depth <EOL> self . orientation = orientation <EOL> def _html_format_ep ( self , route ) : <EOL> module_name = route . endpoint . __module__ <EOL> try : <EOL> func_name = route . endpoint . func_name <EOL> except : <EOL> func_name = repr ( route . endpoint ) <EOL> args , _ , _ , _ = getargspec ( route . endpoint ) <EOL> argstr = '<STR_LIT:U+002CU+0020>' . join ( args ) <EOL> title = ( '<STR_LIT>' <EOL> % ( module_name , func_name , argstr ) ) <EOL> return title <EOL> def __call__ ( self , context , _route ) : <EOL> content_parts = [ self . _html_wrapper ] <EOL> if self . _html_style_content : <EOL> content_parts . extend ( [ '<STR_LIT>' , <EOL> self . _html_style_content , <EOL> '<STR_LIT>' ] ) <EOL> content_parts . append ( '<STR_LIT>' ) <EOL> title = self . _html_format_ep ( _route ) <EOL> content_parts . append ( title ) <EOL> table = Table . from_data ( context , max_depth = self . max_depth ) <EOL> table . _html_table_tag = self . _html_table_tag <EOL> content = table . to_html ( max_depth = self . max_depth , <EOL> orientation = self . orientation ) <EOL> content_parts . append ( content ) <EOL> content_parts . append ( '<STR_LIT>' ) <EOL> content_parts . append ( self . _html_wrapper_close ) <EOL> return Response ( '<STR_LIT:\n>' . join ( content_parts ) , mimetype = '<STR_LIT>' ) <EOL> class BasicRender ( object ) : <EOL> _default_mime = '<STR_LIT:application/json>' <EOL> _format_mime_map = { '<STR_LIT:html>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> def __init__ ( self , dev_mode = True , qp_name = '<STR_LIT>' ) : <EOL> self . qp_name = qp_name <EOL> self . json_render = JSONRender ( dev_mode = dev_mode ) <EOL> self . autotable_render = AutoTableRenderer ( ) <EOL> def render_response ( self , request , context , _route ) : <EOL> from collections import Sized <EOL> if isinstance ( context , basestring ) : <EOL> if self . _guess_json ( context ) : <EOL> return Response ( context , mimetype = "<STR_LIT:application/json>" ) <EOL> elif '<STR_LIT>' in context [ : <NUM_LIT> ] : <EOL> return Response ( context , mimetype = "<STR_LIT>" ) <EOL> else : <EOL> return Response ( context , mimetype = "<STR_LIT>" ) <EOL> if not isinstance ( context , Sized ) : <EOL> return Response ( unicode ( context ) , mimetype = "<STR_LIT>" ) <EOL> return self . _serialize_to_resp ( context , request , _route ) <EOL> __call__ = render_response <EOL> def _serialize_to_resp ( self , context , request , _route ) : <EOL> req_format = request . args . get ( self . qp_name ) <EOL> if req_format and req_format not in self . _format_mime_map : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % ( self . formats , req_format ) ) <EOL> resp_mime = self . _format_mime_map . get ( req_format ) <EOL> if not resp_mime and request . accept_mimetypes : <EOL> resp_mime = request . accept_mimetypes . best_match ( self . mimetypes ) <EOL> if resp_mime not in self . _mime_format_map : <EOL> resp_mime = self . _default_mime <EOL> if resp_mime == '<STR_LIT:application/json>' : <EOL> return self . json_render ( context ) <EOL> elif resp_mime == '<STR_LIT>' : <EOL> return self . autotable_render ( context , _route ) <EOL> return Response ( unicode ( context ) , mimetype = "<STR_LIT>" ) <EOL> @ property <EOL> def _mime_format_map ( self ) : <EOL> return dict ( [ ( v , k ) for k , v in self . _format_mime_map . items ( ) ] ) <EOL> @ property <EOL> def formats ( self ) : <EOL> return self . _format_mime_map . keys ( ) <EOL> @ property <EOL> def mimetypes ( self ) : <EOL> return self . _format_mime_map . values ( ) <EOL> @ staticmethod <EOL> def _guess_json ( text ) : <EOL> if not text : <EOL> return False <EOL> elif text [ <NUM_LIT:0> ] == '<STR_LIT:{>' and text [ - <NUM_LIT:1> ] == '<STR_LIT:}>' : <EOL> return True <EOL> elif text [ <NUM_LIT:0> ] == '<STR_LIT:[>' and text [ - <NUM_LIT:1> ] == '<STR_LIT:]>' : <EOL> return True <EOL> else : <EOL> return False <EOL> @ classmethod <EOL> def factory ( cls , * a , ** kw ) : <EOL> def basic_render_factory ( render_arg ) : <EOL> return cls ( * a , ** kw ) <EOL> return basic_render_factory <EOL> def ident_ep ( data ) : <EOL> return data <EOL> def main ( ) : <EOL> rsc = { '<STR_LIT:data>' : _DATA } <EOL> gpm = GetParamMiddleware ( '<STR_LIT:url>' ) <EOL> atr = AutoTableRenderer ( max_depth = <NUM_LIT:5> ) <EOL> render_basic = BasicRender ( ) <EOL> app = Application ( [ ( '<STR_LIT:/>' , ident_ep , render_basic ) , <EOL> ( '<STR_LIT>' , ident_ep , render_basic ) , <EOL> ( '<STR_LIT>' , fetch_json , render_basic ) ] , rsc , [ gpm ] ) <EOL> app . serve ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
