comparison env/lib/python3.7/site-packages/virtualenv/config/cli/parser.py @ 2:6af9afd405e9 draft

"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author shellac
date Thu, 14 May 2020 14:56:58 -0400
parents 26e78fe6e8c4
children
comparison
equal deleted inserted replaced
1:75ca89e9b81c 2:6af9afd405e9
1 from __future__ import absolute_import, unicode_literals
2
3 from argparse import SUPPRESS, ArgumentDefaultsHelpFormatter, ArgumentParser
4 from collections import OrderedDict
5
6 from virtualenv.config.convert import get_type
7
8 from ..env_var import get_env_var
9 from ..ini import IniConfig
10
11
12 class VirtualEnvConfigParser(ArgumentParser):
13 """
14 Custom option parser which updates its defaults by checking the configuration files and environmental variables
15 """
16
17 def __init__(self, options=None, *args, **kwargs):
18 self.file_config = IniConfig()
19 self.epilog_list = []
20 kwargs["epilog"] = self.file_config.epilog
21 kwargs["add_help"] = False
22 kwargs["formatter_class"] = HelpFormatter
23 kwargs["prog"] = "virtualenv"
24 super(VirtualEnvConfigParser, self).__init__(*args, **kwargs)
25 self._fixed = set()
26 self._elements = None
27 self._verbosity = None
28 self._options = options
29 self._interpreter = None
30 self._app_data = None
31
32 def _fix_defaults(self):
33 for action in self._actions:
34 action_id = id(action)
35 if action_id not in self._fixed:
36 self._fix_default(action)
37 self._fixed.add(action_id)
38
39 def _fix_default(self, action):
40 if hasattr(action, "default") and hasattr(action, "dest") and action.default != SUPPRESS:
41 as_type = get_type(action)
42 names = OrderedDict((i.lstrip("-").replace("-", "_"), None) for i in action.option_strings)
43 outcome = None
44 for name in names:
45 outcome = get_env_var(name, as_type)
46 if outcome is not None:
47 break
48 if outcome is None and self.file_config:
49 for name in names:
50 outcome = self.file_config.get(name, as_type)
51 if outcome is not None:
52 break
53 if outcome is not None:
54 action.default, action.default_source = outcome
55
56 def enable_help(self):
57 self._fix_defaults()
58 self.add_argument("-h", "--help", action="help", default=SUPPRESS, help="show this help message and exit")
59
60 def parse_known_args(self, args=None, namespace=None):
61 self._fix_defaults()
62 return super(VirtualEnvConfigParser, self).parse_known_args(args, namespace=namespace)
63
64 def parse_args(self, args=None, namespace=None):
65 self._fix_defaults()
66 return super(VirtualEnvConfigParser, self).parse_args(args, namespace=namespace)
67
68
69 class HelpFormatter(ArgumentDefaultsHelpFormatter):
70 def __init__(self, prog):
71 super(HelpFormatter, self).__init__(prog, max_help_position=32, width=240)
72
73 def _get_help_string(self, action):
74 # noinspection PyProtectedMember
75 text = super(HelpFormatter, self)._get_help_string(action)
76 if hasattr(action, "default_source"):
77 default = " (default: %(default)s)"
78 if text.endswith(default):
79 text = "{} (default: %(default)s -> from %(default_source)s)".format(text[: -len(default)])
80 return text