#NEWLINE# def program( args , env , log_error ) : #NEWLINE# #INDENT# '\n    The main program without error handling\n\n    :param args: parsed args (argparse.Namespace)\n    :type env: Environment\n    :param log_error: error log function\n    :return: status code\n\n    ' #NEWLINE# exit_status = ExitStatus . OK #NEWLINE# downloader = None #NEWLINE# show_traceback = ( args . debug or args . traceback ) #NEWLINE# try : #NEWLINE# #INDENT# if args . download : #NEWLINE# #INDENT# args . follow = True #NEWLINE# downloader = Downloader ( output_file = args . output_file , progress_file = env . stderr , resume = args . download_resume ) #NEWLINE# downloader . pre_request ( args . headers ) #NEWLINE# #UNINDENT# final_response = get_response ( args , config_dir = env . config . directory ) #NEWLINE# if args . all : #NEWLINE# #INDENT# responses = ( final_response . history + [ final_response ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# responses = [ final_response ] #NEWLINE# #UNINDENT# for response in responses : #NEWLINE# #INDENT# if ( args . check_status or downloader ) : #NEWLINE# #INDENT# exit_status = get_exit_status ( http_status = response . status_code , follow = args . follow ) #NEWLINE# if ( ( not env . stdout_isatty ) and ( exit_status != ExitStatus . OK ) ) : #NEWLINE# #INDENT# log_error ( 'HTTP %s %s' , response . raw . status , response . raw . reason , level = 'warning' ) #NEWLINE# #UNINDENT# #UNINDENT# write_stream_kwargs = { #NEWLINE# #INDENT# 'stream' : build_output_stream ( args = args , env = env , request = response . request , response = response , output_options = ( args . output_options if ( response is final_response ) else args . output_options_others ) ) , #NEWLINE# 'outfile' : env . stdout , #NEWLINE# 'flush' : ( env . stdout_isatty or args . stream ) , #NEWLINE# #UNINDENT# } #NEWLINE# try : #NEWLINE# #INDENT# if ( env . is_windows and is_py3 and ( 'colors' in args . prettify ) ) : #NEWLINE# #INDENT# write_stream_with_colors_win_py3 ( ** write_stream_kwargs ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# write_stream ( ** write_stream_kwargs ) #NEWLINE# #UNINDENT# #UNINDENT# except IOError as e : #NEWLINE# #INDENT# if ( ( not e ) and ( e . errno == errno . EPIPE ) ) : #NEWLINE# #INDENT# env . stderr . write ( '\n' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( downloader and ( exit_status == ExitStatus . OK ) ) : #NEWLINE# #INDENT# ( download_stream , download_to ) = downloader . start ( final_response ) #NEWLINE# write_stream ( stream = download_stream , outfile = download_to , flush = False ) #NEWLINE# downloader . finish ( ) #NEWLINE# if downloader . interrupted : #NEWLINE# #INDENT# exit_status = ExitStatus . ERROR #NEWLINE# log_error ( ( 'Incomplete download: size=%d; downloaded=%d' % ( downloader . status . total_size , downloader . status . downloaded ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# return exit_status #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# if ( downloader and ( not downloader . finished ) ) : #NEWLINE# #INDENT# downloader . failed ( ) #NEWLINE# #UNINDENT# if ( ( not isinstance ( args , list ) ) and args . output_file and args . output_file_specified ) : #NEWLINE# #INDENT# args . output_file . close ( )