comparison env/lib/python3.7/site-packages/virtualenv/config/ini.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 from __future__ import absolute_import, unicode_literals
2
3 import logging
4 import os
5
6 from appdirs import user_config_dir
7
8 from virtualenv.info import PY3
9 from virtualenv.util import ConfigParser
10 from virtualenv.util.path import Path
11 from virtualenv.util.six import ensure_str
12
13 from .convert import convert
14
15
16 class IniConfig(object):
17 VIRTUALENV_CONFIG_FILE_ENV_VAR = ensure_str("VIRTUALENV_CONFIG_FILE")
18 STATE = {None: "failed to parse", True: "active", False: "missing"}
19
20 section = "virtualenv"
21
22 def __init__(self):
23 config_file = os.environ.get(self.VIRTUALENV_CONFIG_FILE_ENV_VAR, None)
24 self.is_env_var = config_file is not None
25 config_file = (
26 Path(config_file)
27 if config_file is not None
28 else Path(user_config_dir(appname="virtualenv", appauthor="pypa")) / "virtualenv.ini"
29 )
30 self.config_file = config_file
31 self._cache = {}
32
33 exception = None
34 self.has_config_file = None
35 try:
36 self.has_config_file = self.config_file.exists()
37 except OSError as exc:
38 exception = exc
39 else:
40 if self.has_config_file:
41 self.config_file = self.config_file.resolve()
42 self.config_parser = ConfigParser.ConfigParser()
43 try:
44 self._load()
45 self.has_virtualenv_section = self.config_parser.has_section(self.section)
46 except Exception as exc:
47 exception = exc
48 if exception is not None:
49 logging.error("failed to read config file %s because %r", config_file, exception)
50
51 def _load(self):
52 with self.config_file.open("rt") as file_handler:
53 reader = getattr(self.config_parser, "read_file" if PY3 else "readfp")
54 reader(file_handler)
55
56 def get(self, key, as_type):
57 cache_key = key, as_type
58 if cache_key in self._cache:
59 return self._cache[cache_key]
60 # noinspection PyBroadException
61 try:
62 source = "file"
63 raw_value = self.config_parser.get(self.section, key.lower())
64 value = convert(raw_value, as_type, source)
65 result = value, source
66 except Exception:
67 result = None
68 self._cache[cache_key] = result
69 return result
70
71 def __bool__(self):
72 return bool(self.has_config_file) and bool(self.has_virtualenv_section)
73
74 @property
75 def epilog(self):
76 msg = "{}config file {} {} (change{} via env var {})"
77 return msg.format(
78 os.linesep,
79 self.config_file,
80 self.STATE[self.has_config_file],
81 "d" if self.is_env_var else "",
82 self.VIRTUALENV_CONFIG_FILE_ENV_VAR,
83 )