<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import six <EOL> from six import moves <EOL> from six . moves . configparser import Error as ConfigParserError , SafeConfigParser <EOL> import functools <EOL> import os <EOL> import sys <EOL> import tempfile <EOL> import textwrap <EOL> import time <EOL> import traceback <EOL> from swiftly import VERSION <EOL> from swiftly . cli . context import CLIContext <EOL> from swiftly . cli . iomanager import IOManager <EOL> from swiftly . cli . optionparser import OptionParser <EOL> from swiftly . client import ClientManager , DirectClient , LocalClient , StandardClient <EOL> COMMANDS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> TRUE_VALUES = [ '<STR_LIT:1>' , '<STR_LIT>' , '<STR_LIT:t>' , '<STR_LIT:true>' , '<STR_LIT:y>' , '<STR_LIT:yes>' ] <EOL> """<STR_LIT>""" <EOL> class CLI ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , commands = None ) : <EOL> self . context = CLIContext ( ) <EOL> self . context . verbose = None <EOL> self . context . io_manager = IOManager ( ) <EOL> self . commands = { } <EOL> if commands is None : <EOL> commands = COMMANDS <EOL> for command in commands : <EOL> mod , cls = command . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> cls = getattr ( __import__ ( mod , fromlist = [ cls ] ) , cls ) <EOL> inst = cls ( self ) <EOL> self . commands [ inst . name ] = inst <EOL> self . option_parser = OptionParser ( <EOL> version = VERSION , <EOL> usage = """<STR_LIT>""" . strip ( ) , <EOL> io_manager = self . context . io_manager ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . option_parser . raw_epilog = '<STR_LIT>' <EOL> for name in sorted ( self . commands ) : <EOL> command = self . commands [ name ] <EOL> lines = command . option_parser . get_usage ( ) . split ( '<STR_LIT:\n>' ) <EOL> main_line = '<STR_LIT:U+0020>' + lines [ <NUM_LIT:0> ] . split ( '<STR_LIT:]>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] . strip ( ) <EOL> for x in moves . range ( <NUM_LIT:4> ) : <EOL> lines . pop ( <NUM_LIT:0> ) <EOL> for x , line in enumerate ( lines ) : <EOL> if not line : <EOL> lines = lines [ : x ] <EOL> break <EOL> if len ( main_line ) < <NUM_LIT> : <EOL> initial_indent = main_line + '<STR_LIT:U+0020>' * ( <NUM_LIT> - len ( main_line ) ) <EOL> else : <EOL> self . option_parser . raw_epilog += main_line + '<STR_LIT:\n>' <EOL> initial_indent = '<STR_LIT:U+0020>' * <NUM_LIT> <EOL> self . option_parser . raw_epilog += textwrap . fill ( <EOL> '<STR_LIT:U+0020>' . join ( lines ) , width = <NUM_LIT> , initial_indent = initial_indent , <EOL> subsequent_indent = '<STR_LIT:U+0020>' * <NUM_LIT> ) + '<STR_LIT:\n>' <EOL> def __call__ ( self , args = None ) : <EOL> options , args = self . _parse_args ( args ) <EOL> if not options : <EOL> return <NUM_LIT:1> <EOL> return self . _perform_command ( args ) <EOL> def _parse_args ( self , args = None ) : <EOL> self . context . original_begin = time . time ( ) <EOL> self . context . original_args = args if args is not None else sys . argv [ <NUM_LIT:1> : ] <EOL> self . option_parser . disable_interspersed_args ( ) <EOL> try : <EOL> options , args = self . option_parser . parse_args ( <EOL> self . context . original_args ) <EOL> except UnboundLocalError : <EOL> pass <EOL> self . option_parser . enable_interspersed_args ( ) <EOL> if self . option_parser . error_encountered : <EOL> return None , None <EOL> if options . version : <EOL> self . option_parser . print_version ( ) <EOL> return None , None <EOL> if not args or options . help : <EOL> self . option_parser . print_help ( ) <EOL> return None , None <EOL> self . context . original_main_args = self . context . original_args [ <EOL> : - len ( args ) ] <EOL> self . context . conf = { } <EOL> if options . conf is None : <EOL> options . conf = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> conf_parser = SafeConfigParser ( ) <EOL> conf_parser . read ( os . path . expanduser ( options . conf ) ) <EOL> for section in conf_parser . sections ( ) : <EOL> self . context . conf [ section ] = dict ( conf_parser . items ( section ) ) <EOL> except ConfigParserError : <EOL> pass <EOL> for option_name in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _resolve_option ( options , option_name , '<STR_LIT>' ) <EOL> for option_name in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if isinstance ( getattr ( options , option_name ) , six . string_types ) : <EOL> setattr ( <EOL> options , option_name , <EOL> getattr ( options , option_name ) . lower ( ) in TRUE_VALUES ) <EOL> for option_name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if isinstance ( getattr ( options , option_name ) , six . string_types ) : <EOL> setattr ( <EOL> options , option_name , int ( getattr ( options , option_name ) ) ) <EOL> if options . snet is None : <EOL> options . snet = False <EOL> if options . no_snet is None : <EOL> options . no_snet = False <EOL> if options . retries is None : <EOL> options . retries = <NUM_LIT:4> <EOL> if options . cache_auth is None : <EOL> options . cache_auth = False <EOL> if options . no_cache_auth is None : <EOL> options . no_cache_auth = False <EOL> if options . cdn is None : <EOL> options . cdn = False <EOL> if options . no_cdn is None : <EOL> options . no_cdn = False <EOL> if options . concurrency is None : <EOL> options . concurrency = <NUM_LIT:1> <EOL> if options . eventlet is None : <EOL> options . eventlet = False <EOL> if options . no_eventlet is None : <EOL> options . no_eventlet = False <EOL> if options . verbose is None : <EOL> options . verbose = False <EOL> if options . no_verbose is None : <EOL> options . no_verbose = False <EOL> self . context . eventlet = None <EOL> if options . eventlet : <EOL> self . context . eventlet = True <EOL> if options . no_eventlet : <EOL> self . context . eventlet = False <EOL> if self . context . eventlet is None : <EOL> self . context . eventlet = False <EOL> try : <EOL> import eventlet <EOL> if eventlet . __version__ >= '<STR_LIT>' : <EOL> self . context . eventlet = True <EOL> except ImportError : <EOL> pass <EOL> subprocess_module = None <EOL> if self . context . eventlet : <EOL> try : <EOL> import eventlet . green . subprocess <EOL> subprocess_module = eventlet . green . subprocess <EOL> except ImportError : <EOL> pass <EOL> if subprocess_module is None : <EOL> import subprocess <EOL> subprocess_module = subprocess <EOL> self . context . io_manager . subprocess_module = subprocess_module <EOL> if options . verbose : <EOL> self . context . verbosity = <NUM_LIT:1> <EOL> self . context . verbose = self . _verbose <EOL> self . context . io_manager . verbose = functools . partial ( <EOL> self . _verbose , skip_sub_command = True ) <EOL> options . retries = int ( options . retries ) <EOL> if args and args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return options , args <EOL> elif options . local : <EOL> self . context . client_manager = ClientManager ( <EOL> LocalClient , local_path = options . local , verbose = self . _verbose ) <EOL> elif options . direct : <EOL> self . context . client_manager = ClientManager ( <EOL> DirectClient , swift_proxy_storage_path = options . direct , <EOL> attempts = options . retries + <NUM_LIT:1> , eventlet = self . context . eventlet , <EOL> verbose = self . _verbose , <EOL> direct_object_ring = options . direct_object_ring ) <EOL> else : <EOL> auth_cache_path = None <EOL> if options . cache_auth : <EOL> auth_cache_path = os . path . join ( <EOL> tempfile . gettempdir ( ) , <EOL> '<STR_LIT>' % os . environ . get ( '<STR_LIT>' , '<STR_LIT:user>' ) ) <EOL> if not options . auth_url : <EOL> with self . context . io_manager . with_stderr ( ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> fp . flush ( ) <EOL> return None , None <EOL> self . context . client_manager = ClientManager ( <EOL> StandardClient , auth_methods = options . auth_methods , <EOL> auth_url = options . auth_url , auth_tenant = options . auth_tenant , <EOL> auth_user = options . auth_user , auth_key = options . auth_key , <EOL> auth_cache_path = auth_cache_path , region = options . region , <EOL> snet = options . snet , attempts = options . retries + <NUM_LIT:1> , <EOL> eventlet = self . context . eventlet , verbose = self . _verbose , <EOL> http_proxy = options . proxy ) <EOL> self . context . cdn = options . cdn <EOL> self . context . concurrency = int ( options . concurrency ) <EOL> return options , args <EOL> def _resolve_option ( self , options , option_name , section_name ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( options , option_name , None ) is not None : <EOL> return <EOL> if option_name . startswith ( section_name + '<STR_LIT:_>' ) : <EOL> environ_name = option_name . upper ( ) <EOL> conf_name = option_name [ len ( section_name ) + <NUM_LIT:1> : ] <EOL> else : <EOL> environ_name = ( section_name + '<STR_LIT:_>' + option_name ) . upper ( ) <EOL> conf_name = option_name <EOL> setattr ( <EOL> options , option_name , <EOL> os . environ . get ( <EOL> environ_name , <EOL> ( self . context . conf . get ( section_name , { } ) ) . get ( conf_name ) ) ) <EOL> def _perform_command ( self , args ) : <EOL> command = args [ <NUM_LIT:0> ] <EOL> if command == '<STR_LIT>' : <EOL> command = '<STR_LIT>' <EOL> if command not in self . commands : <EOL> with self . context . io_manager . with_stderr ( ) as fp : <EOL> fp . write ( '<STR_LIT>' % args [ <NUM_LIT:0> ] ) <EOL> fp . flush ( ) <EOL> return <NUM_LIT:1> <EOL> try : <EOL> self . commands [ command ] ( args [ <NUM_LIT:1> : ] ) <EOL> except Exception as err : <EOL> if hasattr ( err , '<STR_LIT:text>' ) : <EOL> if err . text : <EOL> with self . context . io_manager . with_stderr ( ) as fp : <EOL> fp . write ( '<STR_LIT>' ) <EOL> fp . write ( err . text ) <EOL> fp . write ( '<STR_LIT:\n>' ) <EOL> fp . flush ( ) <EOL> else : <EOL> with self . context . io_manager . with_stderr ( ) as fp : <EOL> fp . write ( traceback . format_exc ( ) ) <EOL> fp . write ( '<STR_LIT:\n>' ) <EOL> fp . flush ( ) <EOL> return getattr ( err , '<STR_LIT:code>' , <NUM_LIT:1> ) <EOL> return <NUM_LIT:0> <EOL> def _verbose ( self , msg , * args , ** kwargs ) : <EOL> if self . context . verbosity : <EOL> skip_sub_command = kwargs . get ( '<STR_LIT>' , False ) <EOL> with self . context . io_manager . with_debug ( <EOL> skip_sub_command = skip_sub_command ) as fp : <EOL> try : <EOL> fp . write ( <EOL> '<STR_LIT>' % <EOL> ( time . time ( ) - self . context . original_begin ) ) <EOL> fp . write ( msg % args ) <EOL> except TypeError as err : <EOL> raise TypeError ( '<STR_LIT>' % ( err , msg , args ) ) <EOL> fp . write ( '<STR_LIT:\n>' ) <EOL> fp . flush ( ) </s>
