Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../lib/python2..../site-pac.../pip
File: baseparser.py
"""Base option parser setup"""
[0] Fix | Delete
from __future__ import absolute_import
[1] Fix | Delete
[2] Fix | Delete
import sys
[3] Fix | Delete
import optparse
[4] Fix | Delete
import os
[5] Fix | Delete
import re
[6] Fix | Delete
import textwrap
[7] Fix | Delete
from distutils.util import strtobool
[8] Fix | Delete
[9] Fix | Delete
from pip._vendor.six import string_types
[10] Fix | Delete
from pip._vendor.six.moves import configparser
[11] Fix | Delete
from pip.locations import (
[12] Fix | Delete
legacy_config_file, config_basename, running_under_virtualenv,
[13] Fix | Delete
site_config_files
[14] Fix | Delete
)
[15] Fix | Delete
from pip.utils import appdirs, get_terminal_size
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
_environ_prefix_re = re.compile(r"^PIP_", re.I)
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
class PrettyHelpFormatter(optparse.IndentedHelpFormatter):
[22] Fix | Delete
"""A prettier/less verbose help formatter for optparse."""
[23] Fix | Delete
[24] Fix | Delete
def __init__(self, *args, **kwargs):
[25] Fix | Delete
# help position must be aligned with __init__.parseopts.description
[26] Fix | Delete
kwargs['max_help_position'] = 30
[27] Fix | Delete
kwargs['indent_increment'] = 1
[28] Fix | Delete
kwargs['width'] = get_terminal_size()[0] - 2
[29] Fix | Delete
optparse.IndentedHelpFormatter.__init__(self, *args, **kwargs)
[30] Fix | Delete
[31] Fix | Delete
def format_option_strings(self, option):
[32] Fix | Delete
return self._format_option_strings(option, ' <%s>', ', ')
[33] Fix | Delete
[34] Fix | Delete
def _format_option_strings(self, option, mvarfmt=' <%s>', optsep=', '):
[35] Fix | Delete
"""
[36] Fix | Delete
Return a comma-separated list of option strings and metavars.
[37] Fix | Delete
[38] Fix | Delete
:param option: tuple of (short opt, long opt), e.g: ('-f', '--format')
[39] Fix | Delete
:param mvarfmt: metavar format string - evaluated as mvarfmt % metavar
[40] Fix | Delete
:param optsep: separator
[41] Fix | Delete
"""
[42] Fix | Delete
opts = []
[43] Fix | Delete
[44] Fix | Delete
if option._short_opts:
[45] Fix | Delete
opts.append(option._short_opts[0])
[46] Fix | Delete
if option._long_opts:
[47] Fix | Delete
opts.append(option._long_opts[0])
[48] Fix | Delete
if len(opts) > 1:
[49] Fix | Delete
opts.insert(1, optsep)
[50] Fix | Delete
[51] Fix | Delete
if option.takes_value():
[52] Fix | Delete
metavar = option.metavar or option.dest.lower()
[53] Fix | Delete
opts.append(mvarfmt % metavar.lower())
[54] Fix | Delete
[55] Fix | Delete
return ''.join(opts)
[56] Fix | Delete
[57] Fix | Delete
def format_heading(self, heading):
[58] Fix | Delete
if heading == 'Options':
[59] Fix | Delete
return ''
[60] Fix | Delete
return heading + ':\n'
[61] Fix | Delete
[62] Fix | Delete
def format_usage(self, usage):
[63] Fix | Delete
"""
[64] Fix | Delete
Ensure there is only one newline between usage and the first heading
[65] Fix | Delete
if there is no description.
[66] Fix | Delete
"""
[67] Fix | Delete
msg = '\nUsage: %s\n' % self.indent_lines(textwrap.dedent(usage), " ")
[68] Fix | Delete
return msg
[69] Fix | Delete
[70] Fix | Delete
def format_description(self, description):
[71] Fix | Delete
# leave full control over description to us
[72] Fix | Delete
if description:
[73] Fix | Delete
if hasattr(self.parser, 'main'):
[74] Fix | Delete
label = 'Commands'
[75] Fix | Delete
else:
[76] Fix | Delete
label = 'Description'
[77] Fix | Delete
# some doc strings have initial newlines, some don't
[78] Fix | Delete
description = description.lstrip('\n')
[79] Fix | Delete
# some doc strings have final newlines and spaces, some don't
[80] Fix | Delete
description = description.rstrip()
[81] Fix | Delete
# dedent, then reindent
[82] Fix | Delete
description = self.indent_lines(textwrap.dedent(description), " ")
[83] Fix | Delete
description = '%s:\n%s\n' % (label, description)
[84] Fix | Delete
return description
[85] Fix | Delete
else:
[86] Fix | Delete
return ''
[87] Fix | Delete
[88] Fix | Delete
def format_epilog(self, epilog):
[89] Fix | Delete
# leave full control over epilog to us
[90] Fix | Delete
if epilog:
[91] Fix | Delete
return epilog
[92] Fix | Delete
else:
[93] Fix | Delete
return ''
[94] Fix | Delete
[95] Fix | Delete
def indent_lines(self, text, indent):
[96] Fix | Delete
new_lines = [indent + line for line in text.split('\n')]
[97] Fix | Delete
return "\n".join(new_lines)
[98] Fix | Delete
[99] Fix | Delete
[100] Fix | Delete
class UpdatingDefaultsHelpFormatter(PrettyHelpFormatter):
[101] Fix | Delete
"""Custom help formatter for use in ConfigOptionParser.
[102] Fix | Delete
[103] Fix | Delete
This is updates the defaults before expanding them, allowing
[104] Fix | Delete
them to show up correctly in the help listing.
[105] Fix | Delete
"""
[106] Fix | Delete
[107] Fix | Delete
def expand_default(self, option):
[108] Fix | Delete
if self.parser is not None:
[109] Fix | Delete
self.parser._update_defaults(self.parser.defaults)
[110] Fix | Delete
return optparse.IndentedHelpFormatter.expand_default(self, option)
[111] Fix | Delete
[112] Fix | Delete
[113] Fix | Delete
class CustomOptionParser(optparse.OptionParser):
[114] Fix | Delete
[115] Fix | Delete
def insert_option_group(self, idx, *args, **kwargs):
[116] Fix | Delete
"""Insert an OptionGroup at a given position."""
[117] Fix | Delete
group = self.add_option_group(*args, **kwargs)
[118] Fix | Delete
[119] Fix | Delete
self.option_groups.pop()
[120] Fix | Delete
self.option_groups.insert(idx, group)
[121] Fix | Delete
[122] Fix | Delete
return group
[123] Fix | Delete
[124] Fix | Delete
@property
[125] Fix | Delete
def option_list_all(self):
[126] Fix | Delete
"""Get a list of all options, including those in option groups."""
[127] Fix | Delete
res = self.option_list[:]
[128] Fix | Delete
for i in self.option_groups:
[129] Fix | Delete
res.extend(i.option_list)
[130] Fix | Delete
[131] Fix | Delete
return res
[132] Fix | Delete
[133] Fix | Delete
[134] Fix | Delete
class ConfigOptionParser(CustomOptionParser):
[135] Fix | Delete
"""Custom option parser which updates its defaults by checking the
[136] Fix | Delete
configuration files and environmental variables"""
[137] Fix | Delete
[138] Fix | Delete
isolated = False
[139] Fix | Delete
[140] Fix | Delete
def __init__(self, *args, **kwargs):
[141] Fix | Delete
self.config = configparser.RawConfigParser()
[142] Fix | Delete
self.name = kwargs.pop('name')
[143] Fix | Delete
self.isolated = kwargs.pop("isolated", False)
[144] Fix | Delete
self.files = self.get_config_files()
[145] Fix | Delete
if self.files:
[146] Fix | Delete
self.config.read(self.files)
[147] Fix | Delete
assert self.name
[148] Fix | Delete
optparse.OptionParser.__init__(self, *args, **kwargs)
[149] Fix | Delete
[150] Fix | Delete
def get_config_files(self):
[151] Fix | Delete
# the files returned by this method will be parsed in order with the
[152] Fix | Delete
# first files listed being overridden by later files in standard
[153] Fix | Delete
# ConfigParser fashion
[154] Fix | Delete
config_file = os.environ.get('PIP_CONFIG_FILE', False)
[155] Fix | Delete
if config_file == os.devnull:
[156] Fix | Delete
return []
[157] Fix | Delete
[158] Fix | Delete
# at the base we have any site-wide configuration
[159] Fix | Delete
files = list(site_config_files)
[160] Fix | Delete
[161] Fix | Delete
# per-user configuration next
[162] Fix | Delete
if not self.isolated:
[163] Fix | Delete
if config_file and os.path.exists(config_file):
[164] Fix | Delete
files.append(config_file)
[165] Fix | Delete
else:
[166] Fix | Delete
# This is the legacy config file, we consider it to be a lower
[167] Fix | Delete
# priority than the new file location.
[168] Fix | Delete
files.append(legacy_config_file)
[169] Fix | Delete
[170] Fix | Delete
# This is the new config file, we consider it to be a higher
[171] Fix | Delete
# priority than the legacy file.
[172] Fix | Delete
files.append(
[173] Fix | Delete
os.path.join(
[174] Fix | Delete
appdirs.user_config_dir("pip"),
[175] Fix | Delete
config_basename,
[176] Fix | Delete
)
[177] Fix | Delete
)
[178] Fix | Delete
[179] Fix | Delete
# finally virtualenv configuration first trumping others
[180] Fix | Delete
if running_under_virtualenv():
[181] Fix | Delete
venv_config_file = os.path.join(
[182] Fix | Delete
sys.prefix,
[183] Fix | Delete
config_basename,
[184] Fix | Delete
)
[185] Fix | Delete
if os.path.exists(venv_config_file):
[186] Fix | Delete
files.append(venv_config_file)
[187] Fix | Delete
[188] Fix | Delete
return files
[189] Fix | Delete
[190] Fix | Delete
def check_default(self, option, key, val):
[191] Fix | Delete
try:
[192] Fix | Delete
return option.check_value(key, val)
[193] Fix | Delete
except optparse.OptionValueError as exc:
[194] Fix | Delete
print("An error occurred during configuration: %s" % exc)
[195] Fix | Delete
sys.exit(3)
[196] Fix | Delete
[197] Fix | Delete
def _update_defaults(self, defaults):
[198] Fix | Delete
"""Updates the given defaults with values from the config files and
[199] Fix | Delete
the environ. Does a little special handling for certain types of
[200] Fix | Delete
options (lists)."""
[201] Fix | Delete
# Then go and look for the other sources of configuration:
[202] Fix | Delete
config = {}
[203] Fix | Delete
# 1. config files
[204] Fix | Delete
for section in ('global', self.name):
[205] Fix | Delete
config.update(
[206] Fix | Delete
self.normalize_keys(self.get_config_section(section))
[207] Fix | Delete
)
[208] Fix | Delete
# 2. environmental variables
[209] Fix | Delete
if not self.isolated:
[210] Fix | Delete
config.update(self.normalize_keys(self.get_environ_vars()))
[211] Fix | Delete
# Accumulate complex default state.
[212] Fix | Delete
self.values = optparse.Values(self.defaults)
[213] Fix | Delete
late_eval = set()
[214] Fix | Delete
# Then set the options with those values
[215] Fix | Delete
for key, val in config.items():
[216] Fix | Delete
# ignore empty values
[217] Fix | Delete
if not val:
[218] Fix | Delete
continue
[219] Fix | Delete
[220] Fix | Delete
option = self.get_option(key)
[221] Fix | Delete
# Ignore options not present in this parser. E.g. non-globals put
[222] Fix | Delete
# in [global] by users that want them to apply to all applicable
[223] Fix | Delete
# commands.
[224] Fix | Delete
if option is None:
[225] Fix | Delete
continue
[226] Fix | Delete
[227] Fix | Delete
if option.action in ('store_true', 'store_false', 'count'):
[228] Fix | Delete
val = strtobool(val)
[229] Fix | Delete
elif option.action == 'append':
[230] Fix | Delete
val = val.split()
[231] Fix | Delete
val = [self.check_default(option, key, v) for v in val]
[232] Fix | Delete
elif option.action == 'callback':
[233] Fix | Delete
late_eval.add(option.dest)
[234] Fix | Delete
opt_str = option.get_opt_string()
[235] Fix | Delete
val = option.convert_value(opt_str, val)
[236] Fix | Delete
# From take_action
[237] Fix | Delete
args = option.callback_args or ()
[238] Fix | Delete
kwargs = option.callback_kwargs or {}
[239] Fix | Delete
option.callback(option, opt_str, val, self, *args, **kwargs)
[240] Fix | Delete
else:
[241] Fix | Delete
val = self.check_default(option, key, val)
[242] Fix | Delete
[243] Fix | Delete
defaults[option.dest] = val
[244] Fix | Delete
[245] Fix | Delete
for key in late_eval:
[246] Fix | Delete
defaults[key] = getattr(self.values, key)
[247] Fix | Delete
self.values = None
[248] Fix | Delete
return defaults
[249] Fix | Delete
[250] Fix | Delete
def normalize_keys(self, items):
[251] Fix | Delete
"""Return a config dictionary with normalized keys regardless of
[252] Fix | Delete
whether the keys were specified in environment variables or in config
[253] Fix | Delete
files"""
[254] Fix | Delete
normalized = {}
[255] Fix | Delete
for key, val in items:
[256] Fix | Delete
key = key.replace('_', '-')
[257] Fix | Delete
if not key.startswith('--'):
[258] Fix | Delete
key = '--%s' % key # only prefer long opts
[259] Fix | Delete
normalized[key] = val
[260] Fix | Delete
return normalized
[261] Fix | Delete
[262] Fix | Delete
def get_config_section(self, name):
[263] Fix | Delete
"""Get a section of a configuration"""
[264] Fix | Delete
if self.config.has_section(name):
[265] Fix | Delete
return self.config.items(name)
[266] Fix | Delete
return []
[267] Fix | Delete
[268] Fix | Delete
def get_environ_vars(self):
[269] Fix | Delete
"""Returns a generator with all environmental vars with prefix PIP_"""
[270] Fix | Delete
for key, val in os.environ.items():
[271] Fix | Delete
if _environ_prefix_re.search(key):
[272] Fix | Delete
yield (_environ_prefix_re.sub("", key).lower(), val)
[273] Fix | Delete
[274] Fix | Delete
def get_default_values(self):
[275] Fix | Delete
"""Overriding to make updating the defaults after instantiation of
[276] Fix | Delete
the option parser possible, _update_defaults() does the dirty work."""
[277] Fix | Delete
if not self.process_default_values:
[278] Fix | Delete
# Old, pre-Optik 1.5 behaviour.
[279] Fix | Delete
return optparse.Values(self.defaults)
[280] Fix | Delete
[281] Fix | Delete
defaults = self._update_defaults(self.defaults.copy()) # ours
[282] Fix | Delete
for option in self._get_all_options():
[283] Fix | Delete
default = defaults.get(option.dest)
[284] Fix | Delete
if isinstance(default, string_types):
[285] Fix | Delete
opt_str = option.get_opt_string()
[286] Fix | Delete
defaults[option.dest] = option.check_value(opt_str, default)
[287] Fix | Delete
return optparse.Values(defaults)
[288] Fix | Delete
[289] Fix | Delete
def error(self, msg):
[290] Fix | Delete
self.print_usage(sys.stderr)
[291] Fix | Delete
self.exit(2, "%s\n" % msg)
[292] Fix | Delete
[293] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function