<s> """<STR_LIT>""" <EOL> import os <EOL> import time <EOL> import sys <EOL> import string <EOL> import argparse <EOL> from collections import namedtuple <EOL> from enum import Enum <EOL> Result = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> HTTPStatus = Enum ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> POP20_CC = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . split ( ) <EOL> DEFAULT_CONCUR_REQ = <NUM_LIT:1> <EOL> MAX_CONCUR_REQ = <NUM_LIT:1> <EOL> SERVERS = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> DEFAULT_SERVER = '<STR_LIT>' <EOL> DEST_DIR = '<STR_LIT>' <EOL> COUNTRY_CODES_FILE = '<STR_LIT>' <EOL> def save_flag ( img , filename ) : <EOL> path = os . path . join ( DEST_DIR , filename ) <EOL> with open ( path , '<STR_LIT:wb>' ) as fp : <EOL> fp . write ( img ) <EOL> def initial_report ( cc_list , actual_req , server_label ) : <EOL> if len ( cc_list ) <= <NUM_LIT:10> : <EOL> cc_msg = '<STR_LIT:U+002CU+0020>' . join ( cc_list ) <EOL> else : <EOL> cc_msg = '<STR_LIT>' . format ( cc_list [ <NUM_LIT:0> ] , cc_list [ - <NUM_LIT:1> ] ) <EOL> print ( '<STR_LIT>' . format ( server_label , SERVERS [ server_label ] ) ) <EOL> msg = '<STR_LIT>' <EOL> plural = '<STR_LIT:s>' if len ( cc_list ) != <NUM_LIT:1> else '<STR_LIT>' <EOL> print ( msg . format ( len ( cc_list ) , plural , cc_msg ) ) <EOL> plural = '<STR_LIT:s>' if actual_req != <NUM_LIT:1> else '<STR_LIT>' <EOL> msg = '<STR_LIT>' <EOL> print ( msg . format ( actual_req , plural ) ) <EOL> def final_report ( cc_list , counter , start_time ) : <EOL> elapsed = time . time ( ) - start_time <EOL> print ( '<STR_LIT:->' * <NUM_LIT:20> ) <EOL> msg = '<STR_LIT>' <EOL> plural = '<STR_LIT:s>' if counter [ HTTPStatus . ok ] != <NUM_LIT:1> else '<STR_LIT>' <EOL> print ( msg . format ( counter [ HTTPStatus . ok ] , plural ) ) <EOL> if counter [ HTTPStatus . not_found ] : <EOL> print ( counter [ HTTPStatus . not_found ] , '<STR_LIT>' ) <EOL> if counter [ HTTPStatus . error ] : <EOL> plural = '<STR_LIT:s>' if counter [ HTTPStatus . error ] != <NUM_LIT:1> else '<STR_LIT>' <EOL> print ( '<STR_LIT>' . format ( counter [ HTTPStatus . error ] , plural ) ) <EOL> print ( '<STR_LIT>' . format ( elapsed ) ) <EOL> def expand_cc_args ( every_cc , all_cc , cc_args , limit ) : <EOL> codes = set ( ) <EOL> A_Z = string . ascii_uppercase <EOL> if every_cc : <EOL> codes . update ( a + b for a in A_Z for b in A_Z ) <EOL> elif all_cc : <EOL> with open ( COUNTRY_CODES_FILE ) as fp : <EOL> text = fp . read ( ) <EOL> codes . update ( text . split ( ) ) <EOL> else : <EOL> for cc in ( c . upper ( ) for c in cc_args ) : <EOL> if len ( cc ) == <NUM_LIT:1> and cc in A_Z : <EOL> codes . update ( cc + c for c in A_Z ) <EOL> elif len ( cc ) == <NUM_LIT:2> and all ( c in A_Z for c in cc ) : <EOL> codes . add ( cc ) <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> raise ValueError ( '<STR_LIT>' + msg ) <EOL> return sorted ( codes ) [ : limit ] <EOL> def process_args ( default_concur_req ) : <EOL> server_options = '<STR_LIT:U+002CU+0020>' . join ( sorted ( SERVERS ) ) <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , nargs = '<STR_LIT:*>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , metavar = '<STR_LIT:N>' , type = int , <EOL> help = '<STR_LIT>' , default = sys . maxsize ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , metavar = '<STR_LIT>' , type = int , <EOL> default = default_concur_req , <EOL> help = '<STR_LIT>' <EOL> . format ( default_concur_req ) ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> default = DEFAULT_SERVER , <EOL> help = '<STR_LIT>' <EOL> . format ( server_options , DEFAULT_SERVER ) ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> if args . max_req < <NUM_LIT:1> : <EOL> print ( '<STR_LIT>' ) <EOL> parser . print_usage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if args . limit < <NUM_LIT:1> : <EOL> print ( '<STR_LIT>' ) <EOL> parser . print_usage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> args . server = args . server . upper ( ) <EOL> if args . server not in SERVERS : <EOL> print ( '<STR_LIT>' , <EOL> server_options ) <EOL> parser . print_usage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> try : <EOL> cc_list = expand_cc_args ( args . every , args . all , args . cc , args . limit ) <EOL> except ValueError as exc : <EOL> print ( exc . args [ <NUM_LIT:0> ] ) <EOL> parser . print_usage ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if not cc_list : <EOL> cc_list = sorted ( POP20_CC ) <EOL> return args , cc_list <EOL> def main ( download_many , default_concur_req , max_concur_req ) : <EOL> args , cc_list = process_args ( default_concur_req ) <EOL> actual_req = min ( args . max_req , max_concur_req , len ( cc_list ) ) <EOL> initial_report ( cc_list , actual_req , args . server ) <EOL> base_url = SERVERS [ args . server ] <EOL> t0 = time . time ( ) <EOL> counter = download_many ( cc_list , base_url , args . verbose , actual_req ) <EOL> assert sum ( counter . values ( ) ) == len ( cc_list ) , '<STR_LIT>' <EOL> final_report ( cc_list , counter , t0 ) </s>
