#NEWLINE# def main( args = sys . argv [ 1 : ] , env = Environment ( ) , custom_log_error = None ) : #NEWLINE# #INDENT# '\n    The main function.\n\n    Pre-process args, handle some special types of invocations,\n    and run the main program with error handling.\n\n    Return exit status code.\n\n    ' #NEWLINE# args = decode_args ( args , env . stdin_encoding ) #NEWLINE# plugin_manager . load_installed_plugins ( ) #NEWLINE# def log_error( msg , * args , **kwargs ) : #NEWLINE# #INDENT# msg = ( msg % args ) #NEWLINE# level = kwargs . get ( 'level' , 'error' ) #NEWLINE# assert ( level in [ 'error' , 'warning' ] ) #NEWLINE# env . stderr . write ( ( '\nhttp: %s: %s\n' % ( level , msg ) ) ) #NEWLINE# #UNINDENT# from httpie.cli import parser #NEWLINE# if env . config . default_options : #NEWLINE# #INDENT# args = ( env . config . default_options + args ) #NEWLINE# #UNINDENT# if custom_log_error : #NEWLINE# #INDENT# log_error = custom_log_error #NEWLINE# #UNINDENT# include_debug_info = ( '--debug' in args ) #NEWLINE# include_traceback = ( include_debug_info or ( '--traceback' in args ) ) #NEWLINE# if include_debug_info : #NEWLINE# #INDENT# print_debug_info ( env ) #NEWLINE# if ( args == [ '--debug' ] ) : #NEWLINE# #INDENT# return ExitStatus . OK #NEWLINE# #UNINDENT# #UNINDENT# exit_status = ExitStatus . OK #NEWLINE# try : #NEWLINE# #INDENT# parsed_args = parser . parse_args ( args = args , env = env ) #NEWLINE# #UNINDENT# except KeyboardInterrupt : #NEWLINE# #INDENT# env . stderr . write ( '\n' ) #NEWLINE# if include_traceback : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# exit_status = ExitStatus . ERROR #NEWLINE# #UNINDENT# except SystemExit as e : #NEWLINE# #INDENT# if ( e . code != ExitStatus . OK ) : #NEWLINE# #INDENT# env . stderr . write ( '\n' ) #NEWLINE# if include_traceback : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# exit_status = ExitStatus . ERROR #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# exit_status = program ( args = parsed_args , env = env , log_error = log_error ) #NEWLINE# #UNINDENT# except KeyboardInterrupt : #NEWLINE# #INDENT# env . stderr . write ( '\n' ) #NEWLINE# if include_traceback : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# exit_status = ExitStatus . ERROR #NEWLINE# #UNINDENT# except SystemExit as e : #NEWLINE# #INDENT# if ( e . code != ExitStatus . OK ) : #NEWLINE# #INDENT# env . stderr . write ( '\n' ) #NEWLINE# if include_traceback : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# exit_status = ExitStatus . ERROR #NEWLINE# #UNINDENT# #UNINDENT# except requests . Timeout : #NEWLINE# #INDENT# exit_status = ExitStatus . ERROR_TIMEOUT #NEWLINE# log_error ( 'Request timed out (%ss).' , parsed_args . timeout ) #NEWLINE# #UNINDENT# except requests . TooManyRedirects : #NEWLINE# #INDENT# exit_status = ExitStatus . ERROR_TOO_MANY_REDIRECTS #NEWLINE# log_error ( 'Too many redirects (--max-redirects=%s).' , parsed_args . max_redirects ) #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# msg = str ( e ) #NEWLINE# if hasattr ( e , 'request' ) : #NEWLINE# #INDENT# request = e . request #NEWLINE# if hasattr ( request , 'url' ) : #NEWLINE# #INDENT# msg += ( ' while doing %s request to URL: %s' % ( request . method , request . url ) ) #NEWLINE# #UNINDENT# #UNINDENT# log_error ( '%s: %s' , type ( e ) . __name__ , msg ) #NEWLINE# if include_traceback : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# exit_status = ExitStatus . ERROR #NEWLINE# #UNINDENT# #UNINDENT# return exit_status