comparison env/lib/python3.9/site-packages/virtualenv/config/ini.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
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, env=None):
23 env = os.environ if env is None else env
24 config_file = env.get(self.VIRTUALENV_CONFIG_FILE_ENV_VAR, None)
25 self.is_env_var = config_file is not None
26 config_file = (
27 Path(config_file)
28 if config_file is not None
29 else Path(user_config_dir(appname="virtualenv", appauthor="pypa")) / "virtualenv.ini"
30 )
31 self.config_file = config_file
32 self._cache = {}
33
34 exception = None
35 self.has_config_file = None
36 try:
37 self.has_config_file = self.config_file.exists()
38 except OSError as exc:
39 exception = exc
40 else:
41 if self.has_config_file:
42 self.config_file = self.config_file.resolve()
43 self.config_parser = ConfigParser.ConfigParser()
44 try:
45 self._load()
46 self.has_virtualenv_section = self.config_parser.has_section(self.section)
47 except Exception as exc:
48 exception = exc
49 if exception is not None:
50 logging.error("failed to read config file %s because %r", config_file, exception)
51
52 def _load(self):
53 with self.config_file.open("rt") as file_handler:
54 reader = getattr(self.config_parser, "read_file" if PY3 else "readfp")
55 reader(file_handler)
56
57 def get(self, key, as_type):
58 cache_key = key, as_type
59 if cache_key in self._cache:
60 return self._cache[cache_key]
61 # noinspection PyBroadException
62 try:
63 source = "file"
64 raw_value = self.config_parser.get(self.section, key.lower())
65 value = convert(raw_value, as_type, source)
66 result = value, source
67 except Exception:
68 result = None
69 self._cache[cache_key] = result
70 return result
71
72 def __bool__(self):
73 return bool(self.has_config_file) and bool(self.has_virtualenv_section)
74
75 @property
76 def epilog(self):
77 msg = "{}config file {} {} (change{} via env var {})"
78 return msg.format(
79 "\n",
80 self.config_file,
81 self.STATE[self.has_config_file],
82 "d" if self.is_env_var else "",
83 self.VIRTUALENV_CONFIG_FILE_ENV_VAR,
84 )