diff options
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/configuration.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/configuration.py | 378 |
1 files changed, 0 insertions, 378 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/configuration.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/configuration.py deleted file mode 100644 index 07af373..0000000 --- a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/configuration.py +++ /dev/null | |||
@@ -1,378 +0,0 @@ | |||
1 | """Configuration management setup | ||
2 | |||
3 | Some terminology: | ||
4 | - name | ||
5 | As written in config files. | ||
6 | - value | ||
7 | Value associated with a name | ||
8 | - key | ||
9 | Name combined with it's section (section.name) | ||
10 | - variant | ||
11 | A single word describing where the configuration key-value pair came from | ||
12 | """ | ||
13 | |||
14 | import locale | ||
15 | import logging | ||
16 | import os | ||
17 | |||
18 | from pip._vendor import six | ||
19 | from pip._vendor.six.moves import configparser | ||
20 | |||
21 | from pip._internal.exceptions import ConfigurationError | ||
22 | from pip._internal.locations import ( | ||
23 | legacy_config_file, new_config_file, running_under_virtualenv, | ||
24 | site_config_files, venv_config_file, | ||
25 | ) | ||
26 | from pip._internal.utils.misc import ensure_dir, enum | ||
27 | from pip._internal.utils.typing import MYPY_CHECK_RUNNING | ||
28 | |||
29 | if MYPY_CHECK_RUNNING: | ||
30 | from typing import Any, Dict, Iterable, List, NewType, Optional, Tuple | ||
31 | |||
32 | RawConfigParser = configparser.RawConfigParser # Shorthand | ||
33 | Kind = NewType("Kind", str) | ||
34 | |||
35 | logger = logging.getLogger(__name__) | ||
36 | |||
37 | |||
38 | # NOTE: Maybe use the optionx attribute to normalize keynames. | ||
39 | def _normalize_name(name): | ||
40 | # type: (str) -> str | ||
41 | """Make a name consistent regardless of source (environment or file) | ||
42 | """ | ||
43 | name = name.lower().replace('_', '-') | ||
44 | if name.startswith('--'): | ||
45 | name = name[2:] # only prefer long opts | ||
46 | return name | ||
47 | |||
48 | |||
49 | def _disassemble_key(name): | ||
50 | # type: (str) -> List[str] | ||
51 | return name.split(".", 1) | ||
52 | |||
53 | |||
54 | # The kinds of configurations there are. | ||
55 | kinds = enum( | ||
56 | USER="user", # User Specific | ||
57 | GLOBAL="global", # System Wide | ||
58 | VENV="venv", # Virtual Environment Specific | ||
59 | ENV="env", # from PIP_CONFIG_FILE | ||
60 | ENV_VAR="env-var", # from Environment Variables | ||
61 | ) | ||
62 | |||
63 | |||
64 | class Configuration(object): | ||
65 | """Handles management of configuration. | ||
66 | |||
67 | Provides an interface to accessing and managing configuration files. | ||
68 | |||
69 | This class converts provides an API that takes "section.key-name" style | ||
70 | keys and stores the value associated with it as "key-name" under the | ||
71 | section "section". | ||
72 | |||
73 | This allows for a clean interface wherein the both the section and the | ||
74 | key-name are preserved in an easy to manage form in the configuration files | ||
75 | and the data stored is also nice. | ||
76 | """ | ||
77 | |||
78 | def __init__(self, isolated, load_only=None): | ||
79 | # type: (bool, Kind) -> None | ||
80 | super(Configuration, self).__init__() | ||
81 | |||
82 | _valid_load_only = [kinds.USER, kinds.GLOBAL, kinds.VENV, None] | ||
83 | if load_only not in _valid_load_only: | ||
84 | raise ConfigurationError( | ||
85 | "Got invalid value for load_only - should be one of {}".format( | ||
86 | ", ".join(map(repr, _valid_load_only[:-1])) | ||
87 | ) | ||
88 | ) | ||
89 | self.isolated = isolated # type: bool | ||
90 | self.load_only = load_only # type: Optional[Kind] | ||
91 | |||
92 | # The order here determines the override order. | ||
93 | self._override_order = [ | ||
94 | kinds.GLOBAL, kinds.USER, kinds.VENV, kinds.ENV, kinds.ENV_VAR | ||
95 | ] | ||
96 | |||
97 | self._ignore_env_names = ["version", "help"] | ||
98 | |||
99 | # Because we keep track of where we got the data from | ||
100 | self._parsers = { | ||
101 | variant: [] for variant in self._override_order | ||
102 | } # type: Dict[Kind, List[Tuple[str, RawConfigParser]]] | ||
103 | self._config = { | ||
104 | variant: {} for variant in self._override_order | ||
105 | } # type: Dict[Kind, Dict[str, Any]] | ||
106 | self._modified_parsers = [] # type: List[Tuple[str, RawConfigParser]] | ||
107 | |||
108 | def load(self): | ||
109 | # type: () -> None | ||
110 | """Loads configuration from configuration files and environment | ||
111 | """ | ||
112 | self._load_config_files() | ||
113 | if not self.isolated: | ||
114 | self._load_environment_vars() | ||
115 | |||
116 | def get_file_to_edit(self): | ||
117 | # type: () -> Optional[str] | ||
118 | """Returns the file with highest priority in configuration | ||
119 | """ | ||
120 | assert self.load_only is not None, \ | ||
121 | "Need to be specified a file to be editing" | ||
122 | |||
123 | try: | ||
124 | return self._get_parser_to_modify()[0] | ||
125 | except IndexError: | ||
126 | return None | ||
127 | |||
128 | def items(self): | ||
129 | # type: () -> Iterable[Tuple[str, Any]] | ||
130 | """Returns key-value pairs like dict.items() representing the loaded | ||
131 | configuration | ||
132 | """ | ||
133 | return self._dictionary.items() | ||
134 | |||
135 | def get_value(self, key): | ||
136 | # type: (str) -> Any | ||
137 | """Get a value from the configuration. | ||
138 | """ | ||
139 | try: | ||
140 | return self._dictionary[key] | ||
141 | except KeyError: | ||
142 | raise ConfigurationError("No such key - {}".format(key)) | ||
143 | |||
144 | def set_value(self, key, value): | ||
145 | # type: (str, Any) -> None | ||
146 | """Modify a value in the configuration. | ||
147 | """ | ||
148 | self._ensure_have_load_only() | ||
149 | |||
150 | fname, parser = self._get_parser_to_modify() | ||
151 | |||
152 | if parser is not None: | ||
153 | section, name = _disassemble_key(key) | ||
154 | |||
155 | # Modify the parser and the configuration | ||
156 | if not parser.has_section(section): | ||
157 | parser.add_section(section) | ||
158 | parser.set(section, name, value) | ||
159 | |||
160 | self._config[self.load_only][key] = value | ||
161 | self._mark_as_modified(fname, parser) | ||
162 | |||
163 | def unset_value(self, key): | ||
164 | # type: (str) -> None | ||
165 | """Unset a value in the configuration. | ||
166 | """ | ||
167 | self._ensure_have_load_only() | ||
168 | |||
169 | if key not in self._config[self.load_only]: | ||
170 | raise ConfigurationError("No such key - {}".format(key)) | ||
171 | |||
172 | fname, parser = self._get_parser_to_modify() | ||
173 | |||
174 | if parser is not None: | ||
175 | section, name = _disassemble_key(key) | ||
176 | |||
177 | # Remove the key in the parser | ||
178 | modified_something = False | ||
179 | if parser.has_section(section): | ||
180 | # Returns whether the option was removed or not | ||
181 | modified_something = parser.remove_option(section, name) | ||
182 | |||
183 | if modified_something: | ||
184 | # name removed from parser, section may now be empty | ||
185 | section_iter = iter(parser.items(section)) | ||
186 | try: | ||
187 | val = six.next(section_iter) | ||
188 | except StopIteration: | ||
189 | val = None | ||
190 | |||
191 | if val is None: | ||
192 | parser.remove_section(section) | ||
193 | |||
194 | self._mark_as_modified(fname, parser) | ||
195 | else: | ||
196 | raise ConfigurationError( | ||
197 | "Fatal Internal error [id=1]. Please report as a bug." | ||
198 | ) | ||
199 | |||
200 | del self._config[self.load_only][key] | ||
201 | |||
202 | def save(self): | ||
203 | # type: () -> None | ||
204 | """Save the currentin-memory state. | ||
205 | """ | ||
206 | self._ensure_have_load_only() | ||
207 | |||
208 | for fname, parser in self._modified_parsers: | ||
209 | logger.info("Writing to %s", fname) | ||
210 | |||
211 | # Ensure directory exists. | ||
212 | ensure_dir(os.path.dirname(fname)) | ||
213 | |||
214 | with open(fname, "w") as f: | ||
215 | parser.write(f) # type: ignore | ||
216 | |||
217 | # | ||
218 | # Private routines | ||
219 | # | ||
220 | |||
221 | def _ensure_have_load_only(self): | ||
222 | # type: () -> None | ||
223 | if self.load_only is None: | ||
224 | raise ConfigurationError("Needed a specific file to be modifying.") | ||
225 | logger.debug("Will be working with %s variant only", self.load_only) | ||
226 | |||
227 | @property | ||
228 | def _dictionary(self): | ||
229 | # type: () -> Dict[str, Any] | ||
230 | """A dictionary representing the loaded configuration. | ||
231 | """ | ||
232 | # NOTE: Dictionaries are not populated if not loaded. So, conditionals | ||
233 | # are not needed here. | ||
234 | retval = {} | ||
235 | |||
236 | for variant in self._override_order: | ||
237 | retval.update(self._config[variant]) | ||
238 | |||
239 | return retval | ||
240 | |||
241 | def _load_config_files(self): | ||
242 | # type: () -> None | ||
243 | """Loads configuration from configuration files | ||
244 | """ | ||
245 | config_files = dict(self._iter_config_files()) | ||
246 | if config_files[kinds.ENV][0:1] == [os.devnull]: | ||
247 | logger.debug( | ||
248 | "Skipping loading configuration files due to " | ||
249 | "environment's PIP_CONFIG_FILE being os.devnull" | ||
250 | ) | ||
251 | return | ||
252 | |||
253 | for variant, files in config_files.items(): | ||
254 | for fname in files: | ||
255 | # If there's specific variant set in `load_only`, load only | ||
256 | # that variant, not the others. | ||
257 | if self.load_only is not None and variant != self.load_only: | ||
258 | logger.debug( | ||
259 | "Skipping file '%s' (variant: %s)", fname, variant | ||
260 | ) | ||
261 | continue | ||
262 | |||
263 | parser = self._load_file(variant, fname) | ||
264 | |||
265 | # Keeping track of the parsers used | ||
266 | self._parsers[variant].append((fname, parser)) | ||
267 | |||
268 | def _load_file(self, variant, fname): | ||
269 | # type: (Kind, str) -> RawConfigParser | ||
270 | logger.debug("For variant '%s', will try loading '%s'", variant, fname) | ||
271 | parser = self._construct_parser(fname) | ||
272 | |||
273 | for section in parser.sections(): | ||
274 | items = parser.items(section) | ||
275 | self._config[variant].update(self._normalized_keys(section, items)) | ||
276 | |||
277 | return parser | ||
278 | |||
279 | def _construct_parser(self, fname): | ||
280 | # type: (str) -> RawConfigParser | ||
281 | parser = configparser.RawConfigParser() | ||
282 | # If there is no such file, don't bother reading it but create the | ||
283 | # parser anyway, to hold the data. | ||
284 | # Doing this is useful when modifying and saving files, where we don't | ||
285 | # need to construct a parser. | ||
286 | if os.path.exists(fname): | ||
287 | try: | ||
288 | parser.read(fname) | ||
289 | except UnicodeDecodeError: | ||
290 | raise ConfigurationError(( | ||
291 | "ERROR: " | ||
292 | "Configuration file contains invalid %s characters.\n" | ||
293 | "Please fix your configuration, located at %s\n" | ||
294 | ) % (locale.getpreferredencoding(False), fname)) | ||
295 | return parser | ||
296 | |||
297 | def _load_environment_vars(self): | ||
298 | # type: () -> None | ||
299 | """Loads configuration from environment variables | ||
300 | """ | ||
301 | self._config[kinds.ENV_VAR].update( | ||
302 | self._normalized_keys(":env:", self._get_environ_vars()) | ||
303 | ) | ||
304 | |||
305 | def _normalized_keys(self, section, items): | ||
306 | # type: (str, Iterable[Tuple[str, Any]]) -> Dict[str, Any] | ||
307 | """Normalizes items to construct a dictionary with normalized keys. | ||
308 | |||
309 | This routine is where the names become keys and are made the same | ||
310 | regardless of source - configuration files or environment. | ||
311 | """ | ||
312 | normalized = {} | ||
313 | for name, val in items: | ||
314 | key = section + "." + _normalize_name(name) | ||
315 | normalized[key] = val | ||
316 | return normalized | ||
317 | |||
318 | def _get_environ_vars(self): | ||
319 | # type: () -> Iterable[Tuple[str, str]] | ||
320 | """Returns a generator with all environmental vars with prefix PIP_""" | ||
321 | for key, val in os.environ.items(): | ||
322 | should_be_yielded = ( | ||
323 | key.startswith("PIP_") and | ||
324 | key[4:].lower() not in self._ignore_env_names | ||
325 | ) | ||
326 | if should_be_yielded: | ||
327 | yield key[4:].lower(), val | ||
328 | |||
329 | # XXX: This is patched in the tests. | ||
330 | def _iter_config_files(self): | ||
331 | # type: () -> Iterable[Tuple[Kind, List[str]]] | ||
332 | """Yields variant and configuration files associated with it. | ||
333 | |||
334 | This should be treated like items of a dictionary. | ||
335 | """ | ||
336 | # SMELL: Move the conditions out of this function | ||
337 | |||
338 | # environment variables have the lowest priority | ||
339 | config_file = os.environ.get('PIP_CONFIG_FILE', None) | ||
340 | if config_file is not None: | ||
341 | yield kinds.ENV, [config_file] | ||
342 | else: | ||
343 | yield kinds.ENV, [] | ||
344 | |||
345 | # at the base we have any global configuration | ||
346 | yield kinds.GLOBAL, list(site_config_files) | ||
347 | |||
348 | # per-user configuration next | ||
349 | should_load_user_config = not self.isolated and not ( | ||
350 | config_file and os.path.exists(config_file) | ||
351 | ) | ||
352 | if should_load_user_config: | ||
353 | # The legacy config file is overridden by the new config file | ||
354 | yield kinds.USER, [legacy_config_file, new_config_file] | ||
355 | |||
356 | # finally virtualenv configuration first trumping others | ||
357 | if running_under_virtualenv(): | ||
358 | yield kinds.VENV, [venv_config_file] | ||
359 | |||
360 | def _get_parser_to_modify(self): | ||
361 | # type: () -> Tuple[str, RawConfigParser] | ||
362 | # Determine which parser to modify | ||
363 | parsers = self._parsers[self.load_only] | ||
364 | if not parsers: | ||
365 | # This should not happen if everything works correctly. | ||
366 | raise ConfigurationError( | ||
367 | "Fatal Internal error [id=2]. Please report as a bug." | ||
368 | ) | ||
369 | |||
370 | # Use the highest priority parser. | ||
371 | return parsers[-1] | ||
372 | |||
373 | # XXX: This is patched in the tests. | ||
374 | def _mark_as_modified(self, fname, parser): | ||
375 | # type: (str, RawConfigParser) -> None | ||
376 | file_parser_tuple = (fname, parser) | ||
377 | if file_parser_tuple not in self._modified_parsers: | ||
378 | self._modified_parsers.append(file_parser_tuple) | ||