<s> from __future__ import with_statement <EOL> import os <EOL> import optparse as op <EOL> import sys <EOL> try : <EOL> import pygments <EOL> from pygments . lexers import get_lexer_for_mimetype <EOL> from pygments . formatters import TerminalFormatter <EOL> except ImportError : <EOL> pygments = False <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> json = False <EOL> from restkit import __version__ , request , set_logging <EOL> from restkit . util import popen3 , locate_program <EOL> __usage__ = "<STR_LIT>" <EOL> pretties = { <EOL> '<STR_LIT:application/json>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def external ( cmd , data ) : <EOL> try : <EOL> ( child_stdin , child_stdout , child_stderr ) = popen3 ( cmd ) <EOL> err = child_stderr . read ( ) <EOL> if err : <EOL> return data <EOL> return child_stdout . read ( ) <EOL> except : <EOL> return data <EOL> def indent_xml ( data ) : <EOL> tidy_cmd = locate_program ( "<STR_LIT>" ) <EOL> if tidy_cmd : <EOL> cmd = "<STR_LIT:U+0020>" . join ( [ tidy_cmd , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , data ] ) <EOL> return external ( cmd , data ) <EOL> return data <EOL> def indent_json ( data ) : <EOL> if not json : <EOL> return data <EOL> info = json . loads ( data ) <EOL> return json . dumps ( info , indent = <NUM_LIT:2> , sort_keys = True ) <EOL> common_indent = { <EOL> '<STR_LIT:application/json>' : indent_json , <EOL> '<STR_LIT>' : indent_xml , <EOL> '<STR_LIT>' : indent_xml , <EOL> '<STR_LIT>' : indent_xml , <EOL> '<STR_LIT>' : indent_xml , <EOL> '<STR_LIT>' : indent_xml , <EOL> '<STR_LIT>' : indent_xml , <EOL> '<STR_LIT>' : indent_xml , <EOL> '<STR_LIT>' : indent_xml , <EOL> '<STR_LIT>' : indent_xml <EOL> } <EOL> def indent ( mimetype , data ) : <EOL> if mimetype in common_indent : <EOL> return common_indent [ mimetype ] ( data ) <EOL> return data <EOL> def prettify ( response , cli = True ) : <EOL> if not pygments or not '<STR_LIT>' in response . headers : <EOL> return response . body_string ( ) <EOL> ctype = response . headers [ '<STR_LIT>' ] <EOL> try : <EOL> mimetype , encoding = ctype . split ( "<STR_LIT:;>" ) <EOL> except ValueError : <EOL> mimetype = ctype . split ( "<STR_LIT:;>" ) [ <NUM_LIT:0> ] <EOL> body = indent ( mimetype , response . body_string ( ) ) <EOL> mimetype = pretties . get ( mimetype , mimetype ) <EOL> try : <EOL> lexer = get_lexer_for_mimetype ( mimetype ) <EOL> body = pygments . highlight ( body , lexer , TerminalFormatter ( ) ) <EOL> return body <EOL> except : <EOL> return body <EOL> def as_bool ( value ) : <EOL> if value . lower ( ) in ( '<STR_LIT:true>' , '<STR_LIT:1>' ) : <EOL> return True <EOL> return False <EOL> def update_defaults ( defaults ) : <EOL> config = os . path . expanduser ( '<STR_LIT>' ) <EOL> if os . path . isfile ( config ) : <EOL> for line in open ( config ) : <EOL> key , value = line . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> key = key . lower ( ) . strip ( ) <EOL> key = key . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> if key . startswith ( '<STR_LIT>' ) : <EOL> key = '<STR_LIT>' <EOL> value = value . strip ( ) <EOL> if key in defaults : <EOL> default = defaults [ key ] <EOL> if default in ( True , False ) : <EOL> value = as_bool ( value ) <EOL> elif isinstance ( default , list ) : <EOL> default . append ( value ) <EOL> value = default <EOL> defaults [ key ] = value <EOL> def options ( ) : <EOL> """<STR_LIT>""" <EOL> defaults = dict ( <EOL> headers = [ ] , <EOL> request = '<STR_LIT:GET>' , <EOL> follow_redirect = False , <EOL> server_response = False , <EOL> prettify = False , <EOL> log_level = None , <EOL> input = None , <EOL> output = None , <EOL> ) <EOL> update_defaults ( defaults ) <EOL> def opt_args ( option , * help ) : <EOL> help = '<STR_LIT:U+0020>' . join ( help ) <EOL> help = help . strip ( ) <EOL> default = defaults . get ( option ) <EOL> if default is not None : <EOL> help += '<STR_LIT>' % default <EOL> return dict ( default = defaults . get ( option ) , help = help ) <EOL> return [ <EOL> op . make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> ** opt_args ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) , <EOL> op . make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> ** opt_args ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> op . make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , ** opt_args ( '<STR_LIT>' ) ) , <EOL> op . make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> ** opt_args ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> op . make_option ( '<STR_LIT>' , '<STR_LIT>' , dest = "<STR_LIT>" , action = '<STR_LIT:store_true>' , <EOL> ** opt_args ( '<STR_LIT>' , "<STR_LIT>" ) ) , <EOL> op . make_option ( '<STR_LIT>' , dest = "<STR_LIT>" , <EOL> ** opt_args ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) ) , <EOL> op . make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT:input>' , <EOL> metavar = '<STR_LIT>' , <EOL> ** opt_args ( '<STR_LIT:input>' , '<STR_LIT>' ) ) , <EOL> op . make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> ** opt_args ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> op . make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> ] <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> parser = op . OptionParser ( usage = __usage__ , option_list = options ( ) , <EOL> version = "<STR_LIT>" + __version__ ) <EOL> opts , args = parser . parse_args ( ) <EOL> args_len = len ( args ) <EOL> if opts . shell : <EOL> try : <EOL> from restkit . contrib import ipython_shell as shell <EOL> shell . main ( options = opts , * args ) <EOL> except Exception , e : <EOL> print >> sys . stderr , str ( e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return <EOL> if args_len < <NUM_LIT:1> : <EOL> return parser . error ( '<STR_LIT>' ) <EOL> if opts . log_level is not None : <EOL> set_logging ( opts . log_level ) <EOL> body = None <EOL> headers = [ ] <EOL> if opts . input : <EOL> if opts . input == '<STR_LIT:->' : <EOL> body = sys . stdin . read ( ) <EOL> headers . append ( ( "<STR_LIT>" , str ( len ( body ) ) ) ) <EOL> else : <EOL> fname = os . path . normpath ( os . path . join ( os . getcwd ( ) , opts . input ) ) <EOL> body = open ( fname , '<STR_LIT:r>' ) <EOL> if opts . headers : <EOL> for header in opts . headers : <EOL> try : <EOL> k , v = header . split ( '<STR_LIT::>' ) <EOL> headers . append ( ( k , v ) ) <EOL> except ValueError : <EOL> pass <EOL> try : <EOL> if len ( args ) == <NUM_LIT:2> : <EOL> if args [ <NUM_LIT:1> ] == "<STR_LIT:->" and not opts . input : <EOL> body = sys . stdin . read ( ) <EOL> headers . append ( ( "<STR_LIT>" , str ( len ( body ) ) ) ) <EOL> if not opts . method and opts . input : <EOL> method = '<STR_LIT:POST>' <EOL> else : <EOL> method = opts . method . upper ( ) <EOL> resp = request ( args [ <NUM_LIT:0> ] , method = method , body = body , <EOL> headers = headers , follow_redirect = opts . follow_redirect ) <EOL> if opts . output and opts . output != '<STR_LIT:->' : <EOL> with open ( opts . output , '<STR_LIT:wb>' ) as f : <EOL> if opts . server_response : <EOL> f . write ( "<STR_LIT>" % resp . final_url ) <EOL> for k , v in resp . headerslist : <EOL> f . write ( "<STR_LIT>" % ( k , v ) ) <EOL> else : <EOL> with resp . body_stream ( ) as body : <EOL> for block in body : <EOL> f . write ( block ) <EOL> else : <EOL> if opts . server_response : <EOL> if opts . prettify : <EOL> print "<STR_LIT>" % ( <EOL> resp . final_url ) <EOL> for k , v in resp . headerslist : <EOL> print "<STR_LIT>" % ( k , v ) <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" % ( resp . final_url ) <EOL> for k , v in resp . headerslist : <EOL> print "<STR_LIT>" % ( k , v ) <EOL> print "<STR_LIT>" <EOL> if opts . output == '<STR_LIT:->' : <EOL> if opts . prettify : <EOL> print prettify ( resp ) <EOL> else : <EOL> print resp . body_string ( ) <EOL> else : <EOL> if opts . prettify : <EOL> print prettify ( resp ) <EOL> else : <EOL> print resp . body_string ( ) <EOL> except Exception , e : <EOL> sys . stderr . write ( "<STR_LIT>" % str ( e ) ) <EOL> sys . stderr . flush ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> sys . exit ( <NUM_LIT:0> ) </s>
