comparison planemo/lib/python3.7/site-packages/virtualenv/config/cli/parser.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 from __future__ import absolute_import, unicode_literals
2
3 from argparse import SUPPRESS, ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace
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 VirtualEnvOptions(Namespace):
13 def __init__(self, **kwargs):
14 super(VirtualEnvOptions, self).__init__(**kwargs)
15 self._src = None
16 self._sources = {}
17
18 def set_src(self, key, value, src):
19 setattr(self, key, value)
20 if src.startswith("env var"):
21 src = "env var"
22 self._sources[key] = src
23
24 def __setattr__(self, key, value):
25 if getattr(self, "_src", None) is not None:
26 self._sources[key] = self._src
27 super(VirtualEnvOptions, self).__setattr__(key, value)
28
29 def get_source(self, key):
30 return self._sources.get(key)
31
32 @property
33 def verbosity(self):
34 if not hasattr(self, "verbose") and not hasattr(self, "quiet"):
35 return None
36 return max(self.verbose - self.quiet, 0)
37
38 def __repr__(self):
39 return "{}({})".format(
40 type(self).__name__,
41 ", ".join("{}={}".format(k, v) for k, v in vars(self).items() if not k.startswith("_")),
42 )
43
44
45 class VirtualEnvConfigParser(ArgumentParser):
46 """
47 Custom option parser which updates its defaults by checking the configuration files and environmental variables
48 """
49
50 def __init__(self, options=None, *args, **kwargs):
51 self.file_config = IniConfig()
52 self.epilog_list = []
53 kwargs["epilog"] = self.file_config.epilog
54 kwargs["add_help"] = False
55 kwargs["formatter_class"] = HelpFormatter
56 kwargs["prog"] = "virtualenv"
57 super(VirtualEnvConfigParser, self).__init__(*args, **kwargs)
58 self._fixed = set()
59 if options is not None and not isinstance(options, VirtualEnvOptions):
60 raise TypeError("options must be of type VirtualEnvOptions")
61 self.options = VirtualEnvOptions() if options is None else options
62 self._interpreter = None
63 self._app_data = None
64
65 def _fix_defaults(self):
66 for action in self._actions:
67 action_id = id(action)
68 if action_id not in self._fixed:
69 self._fix_default(action)
70 self._fixed.add(action_id)
71
72 def _fix_default(self, action):
73 if hasattr(action, "default") and hasattr(action, "dest") and action.default != SUPPRESS:
74 as_type = get_type(action)
75 names = OrderedDict((i.lstrip("-").replace("-", "_"), None) for i in action.option_strings)
76 outcome = None
77 for name in names:
78 outcome = get_env_var(name, as_type)
79 if outcome is not None:
80 break
81 if outcome is None and self.file_config:
82 for name in names:
83 outcome = self.file_config.get(name, as_type)
84 if outcome is not None:
85 break
86 if outcome is not None:
87 action.default, action.default_source = outcome
88 else:
89 outcome = action.default, "default"
90 self.options.set_src(action.dest, *outcome)
91
92 def enable_help(self):
93 self._fix_defaults()
94 self.add_argument("-h", "--help", action="help", default=SUPPRESS, help="show this help message and exit")
95
96 def parse_known_args(self, args=None, namespace=None):
97 if namespace is None:
98 namespace = self.options
99 elif namespace is not self.options:
100 raise ValueError("can only pass in parser.options")
101 self._fix_defaults()
102 self.options._src = "cli"
103 try:
104 return super(VirtualEnvConfigParser, self).parse_known_args(args, namespace=namespace)
105 finally:
106 self.options._src = None
107
108
109 class HelpFormatter(ArgumentDefaultsHelpFormatter):
110 def __init__(self, prog):
111 super(HelpFormatter, self).__init__(prog, max_help_position=32, width=240)
112
113 def _get_help_string(self, action):
114 # noinspection PyProtectedMember
115 text = super(HelpFormatter, self)._get_help_string(action)
116 if hasattr(action, "default_source"):
117 default = " (default: %(default)s)"
118 if text.endswith(default):
119 text = "{} (default: %(default)s -> from %(default_source)s)".format(text[: -len(default)])
120 return text