<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import sys <EOL> import optparse <EOL> import os <EOL> import re <EOL> import textwrap <EOL> from distutils . util import strtobool <EOL> from pip . _vendor . six import string_types <EOL> from pip . _vendor . six . moves import configparser <EOL> from pip . locations import ( <EOL> legacy_config_file , config_basename , running_under_virtualenv , <EOL> site_config_files <EOL> ) <EOL> from pip . utils import appdirs , get_terminal_size <EOL> _environ_prefix_re = re . compile ( r"<STR_LIT>" , re . I ) <EOL> class PrettyHelpFormatter ( optparse . IndentedHelpFormatter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = <NUM_LIT:30> <EOL> kwargs [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> kwargs [ '<STR_LIT:width>' ] = get_terminal_size ( ) [ <NUM_LIT:0> ] - <NUM_LIT:2> <EOL> optparse . IndentedHelpFormatter . __init__ ( self , * args , ** kwargs ) <EOL> def format_option_strings ( self , option ) : <EOL> return self . _format_option_strings ( option , '<STR_LIT>' , '<STR_LIT:U+002CU+0020>' ) <EOL> def _format_option_strings ( self , option , mvarfmt = '<STR_LIT>' , optsep = '<STR_LIT:U+002CU+0020>' ) : <EOL> """<STR_LIT>""" <EOL> opts = [ ] <EOL> if option . _short_opts : <EOL> opts . append ( option . _short_opts [ <NUM_LIT:0> ] ) <EOL> if option . _long_opts : <EOL> opts . append ( option . _long_opts [ <NUM_LIT:0> ] ) <EOL> if len ( opts ) > <NUM_LIT:1> : <EOL> opts . insert ( <NUM_LIT:1> , optsep ) <EOL> if option . takes_value ( ) : <EOL> metavar = option . metavar or option . dest . lower ( ) <EOL> opts . append ( mvarfmt % metavar . lower ( ) ) <EOL> return '<STR_LIT>' . join ( opts ) <EOL> def format_heading ( self , heading ) : <EOL> if heading == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> return heading + '<STR_LIT>' <EOL> def format_usage ( self , usage ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' % self . indent_lines ( textwrap . dedent ( usage ) , "<STR_LIT:U+0020>" ) <EOL> return msg <EOL> def format_description ( self , description ) : <EOL> if description : <EOL> if hasattr ( self . parser , '<STR_LIT>' ) : <EOL> label = '<STR_LIT>' <EOL> else : <EOL> label = '<STR_LIT>' <EOL> description = description . lstrip ( '<STR_LIT:\n>' ) <EOL> description = description . rstrip ( ) <EOL> description = self . indent_lines ( textwrap . dedent ( description ) , "<STR_LIT:U+0020>" ) <EOL> description = '<STR_LIT>' % ( label , description ) <EOL> return description <EOL> else : <EOL> return '<STR_LIT>' <EOL> def format_epilog ( self , epilog ) : <EOL> if epilog : <EOL> return epilog <EOL> else : <EOL> return '<STR_LIT>' <EOL> def indent_lines ( self , text , indent ) : <EOL> new_lines = [ indent + line for line in text . split ( '<STR_LIT:\n>' ) ] <EOL> return "<STR_LIT:\n>" . join ( new_lines ) <EOL> class UpdatingDefaultsHelpFormatter ( PrettyHelpFormatter ) : <EOL> """<STR_LIT>""" <EOL> def expand_default ( self , option ) : <EOL> if self . parser is not None : <EOL> self . parser . _update_defaults ( self . parser . defaults ) <EOL> return optparse . IndentedHelpFormatter . expand_default ( self , option ) <EOL> class CustomOptionParser ( optparse . OptionParser ) : <EOL> def insert_option_group ( self , idx , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> group = self . add_option_group ( * args , ** kwargs ) <EOL> self . option_groups . pop ( ) <EOL> self . option_groups . insert ( idx , group ) <EOL> return group <EOL> @ property <EOL> def option_list_all ( self ) : <EOL> """<STR_LIT>""" <EOL> res = self . option_list [ : ] <EOL> for i in self . option_groups : <EOL> res . extend ( i . option_list ) <EOL> return res <EOL> class ConfigOptionParser ( CustomOptionParser ) : <EOL> """<STR_LIT>""" <EOL> isolated = False <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . config = configparser . RawConfigParser ( ) <EOL> self . name = kwargs . pop ( '<STR_LIT:name>' ) <EOL> self . isolated = kwargs . pop ( "<STR_LIT>" , False ) <EOL> self . files = self . get_config_files ( ) <EOL> if self . files : <EOL> self . config . read ( self . files ) <EOL> assert self . name <EOL> optparse . OptionParser . __init__ ( self , * args , ** kwargs ) <EOL> def get_config_files ( self ) : <EOL> config_file = os . environ . get ( '<STR_LIT>' , False ) <EOL> if config_file == os . devnull : <EOL> return [ ] <EOL> files = list ( site_config_files ) <EOL> if not self . isolated : <EOL> if config_file and os . path . exists ( config_file ) : <EOL> files . append ( config_file ) <EOL> else : <EOL> files . append ( legacy_config_file ) <EOL> files . append ( <EOL> os . path . join ( <EOL> appdirs . user_config_dir ( "<STR_LIT>" ) , <EOL> config_basename , <EOL> ) <EOL> ) <EOL> if running_under_virtualenv ( ) : <EOL> venv_config_file = os . path . join ( <EOL> sys . prefix , <EOL> config_basename , <EOL> ) <EOL> if os . path . exists ( venv_config_file ) : <EOL> files . append ( venv_config_file ) <EOL> return files <EOL> def check_default ( self , option , key , val ) : <EOL> try : <EOL> return option . check_value ( key , val ) <EOL> except optparse . OptionValueError as exc : <EOL> print ( "<STR_LIT>" % exc ) <EOL> sys . exit ( <NUM_LIT:3> ) <EOL> def _update_defaults ( self , defaults ) : <EOL> """<STR_LIT>""" <EOL> config = { } <EOL> for section in ( '<STR_LIT>' , self . name ) : <EOL> config . update ( <EOL> self . normalize_keys ( self . get_config_section ( section ) ) <EOL> ) <EOL> if not self . isolated : <EOL> config . update ( self . normalize_keys ( self . get_environ_vars ( ) ) ) <EOL> self . values = optparse . Values ( self . defaults ) <EOL> late_eval = set ( ) <EOL> for key , val in config . items ( ) : <EOL> if not val : <EOL> continue <EOL> option = self . get_option ( key ) <EOL> if option is None : <EOL> continue <EOL> if option . action in ( '<STR_LIT:store_true>' , '<STR_LIT>' , '<STR_LIT:count>' ) : <EOL> val = strtobool ( val ) <EOL> elif option . action == '<STR_LIT>' : <EOL> val = val . split ( ) <EOL> val = [ self . check_default ( option , key , v ) for v in val ] <EOL> elif option . action == '<STR_LIT>' : <EOL> late_eval . add ( option . dest ) <EOL> opt_str = option . get_opt_string ( ) <EOL> val = option . convert_value ( opt_str , val ) <EOL> args = option . callback_args or ( ) <EOL> kwargs = option . callback_kwargs or { } <EOL> option . callback ( option , opt_str , val , self , * args , ** kwargs ) <EOL> else : <EOL> val = self . check_default ( option , key , val ) <EOL> defaults [ option . dest ] = val <EOL> for key in late_eval : <EOL> defaults [ key ] = getattr ( self . values , key ) <EOL> self . values = None <EOL> return defaults <EOL> def normalize_keys ( self , items ) : <EOL> """<STR_LIT>""" <EOL> normalized = { } <EOL> for key , val in items : <EOL> key = key . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> if not key . startswith ( '<STR_LIT>' ) : <EOL> key = '<STR_LIT>' % key <EOL> normalized [ key ] = val <EOL> return normalized <EOL> def get_config_section ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if self . config . has_section ( name ) : <EOL> return self . config . items ( name ) <EOL> return [ ] <EOL> def get_environ_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> for key , val in os . environ . items ( ) : <EOL> if _environ_prefix_re . search ( key ) : <EOL> yield ( _environ_prefix_re . sub ( "<STR_LIT>" , key ) . lower ( ) , val ) <EOL> def get_default_values ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . process_default_values : <EOL> return optparse . Values ( self . defaults ) <EOL> defaults = self . _update_defaults ( self . defaults . copy ( ) ) <EOL> for option in self . _get_all_options ( ) : <EOL> default = defaults . get ( option . dest ) <EOL> if isinstance ( default , string_types ) : <EOL> opt_str = option . get_opt_string ( ) <EOL> defaults [ option . dest ] = option . check_value ( opt_str , default ) <EOL> return optparse . Values ( defaults ) <EOL> def error ( self , msg ) : <EOL> self . print_usage ( sys . stderr ) <EOL> self . exit ( <NUM_LIT:2> , "<STR_LIT>" % msg ) </s>
