/
_option_set.py
322 lines (261 loc) · 10.9 KB
/
_option_set.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright 2019 Alethea Katherine Flowers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""High-level options interface. This allows defining options just once that
can be specified from the command line and the noxfile, easily used in tests,
and surfaced in documentation."""
import argparse
import collections
import functools
from argparse import ArgumentError as ArgumentError
from argparse import ArgumentParser, Namespace
from typing import Any, Callable, List, Optional, Tuple, Union
import argcomplete
class OptionGroup:
"""A single group for command-line options.
Args:
name (str): The name used to refer to the group.
args: Passed through to``ArgumentParser.add_argument_group``.
kwargs: Passed through to``ArgumentParser.add_argument_group``.
"""
def __init__(self, name: str, *args: Any, **kwargs: Any) -> None:
self.name = name
self.args = args
self.kwargs = kwargs
class Option:
"""A single option that can be specified via command-line or configuration
file.
Args:
name (str): The name used to refer to the option in the final namespace
object.
flags (Sequence[str]): The list of flags used by argparse. Effectively
the ``*args`` for ``ArgumentParser.add_argument``.
group (OptionGroup): The argument group this option belongs to.
help (str): The help string pass to argparse.
noxfile (bool): Whether or not this option can be set in the
configuration file.
merge_func (Callable[[Namespace, Namespace], Any]): A function that
can define custom behavior when merging the command-line options
with the configuration file options. The first argument is the
command-line options, the second is the configuration file options.
It should return the new value for the option.
finalizer_func (Callable[Any, Namespace], Any): A function that can
define custom finalization behavior. This is called after all
arguments are parsed. It's called with the options parsed value
and the set of command-line options and should return the new
value.
default (Union[Any, Callable[[], Any]]): The default value. It may
also be a function in which case it will be invoked after argument
parsing if nothing was specified.
hidden (bool): Means this option will be present in the namespace, but
will not show up on the argument list.
kwargs: Passed through to``ArgumentParser.add_argument``.
"""
def __init__(
self,
name: str,
*flags: str,
group: Optional[OptionGroup],
help: Optional[str] = None,
noxfile: bool = False,
merge_func: Optional[Callable[[Namespace, Namespace], Any]] = None,
finalizer_func: Optional[Callable[[Any, Namespace], Any]] = None,
default: Union[Any, Callable[[], Any]] = None,
hidden: bool = False,
completer: Optional[Callable[..., List[str]]] = None,
**kwargs: Any,
) -> None:
self.name = name
self.flags = flags
self.group = group
self.help = help
self.noxfile = noxfile
self.merge_func = merge_func
self.finalizer_func = finalizer_func
self.hidden = hidden
self.completer = completer
self.kwargs = kwargs
self._default = default
@property
def default(self) -> Optional[Union[bool, str]]:
if callable(self._default):
return self._default()
return self._default
def flag_pair_merge_func(
enable_name: str,
disable_name: str,
command_args: Namespace,
noxfile_args: Namespace,
) -> bool:
"""Merge function for flag pairs. If the flag is set in the noxfile or
the command line params, return ``True`` *unless* the disable flag has been
specified on the command-line.
For example, assuming you have a flag pair created using::
make_flag_pair(
"thing_a",
"--thing-a",
"--no-thing-a"
)
Then if the Noxfile says::
nox.options.thing_a = True
But the command line says::
nox --no-thing-a
Then the result will be ``False``.
However, without the "--no-thing-a" flag set then this returns ``True`` if
*either*::
nox.options.thing_a = True
or::
nox --thing-a
are specified.
"""
noxfile_value = getattr(noxfile_args, enable_name)
command_value = getattr(command_args, enable_name)
disable_value = getattr(command_args, disable_name)
return (command_value or noxfile_value) and not disable_value
def make_flag_pair(
name: str,
enable_flags: Union[Tuple[str, str], Tuple[str]],
disable_flags: Tuple[str],
**kwargs: Any,
) -> Tuple[Option, Option]:
"""Returns two options - one to enable a behavior and another to disable it.
The positive option is considered to be available to the noxfile, as
there isn't much point in doing flag pairs without it.
"""
disable_name = f"no_{name}"
kwargs["action"] = "store_true"
enable_option = Option(
name,
*enable_flags,
noxfile=True,
merge_func=functools.partial(flag_pair_merge_func, name, disable_name),
**kwargs,
)
kwargs["help"] = f"Disables {enable_flags[-1]} if it is enabled in the Noxfile."
disable_option = Option(disable_name, *disable_flags, **kwargs)
return enable_option, disable_option
class OptionSet:
"""A set of options.
A high-level wrapper over ``argparse.ArgumentParser``. It allows for
introspection of options as well as quality-of-life features such as
finalization, callable defaults, and strongly typed namespaces for tests.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.parser_args = args
self.parser_kwargs = kwargs
self.options: "collections.OrderedDict[str, Option]" = collections.OrderedDict()
self.groups: "collections.OrderedDict[str, OptionGroup]" = (
collections.OrderedDict()
)
def add_options(self, *args: Option) -> None:
"""Adds a sequence of Options to the OptionSet.
Args:
args (Sequence[Options])
"""
for option in args:
self.options[option.name] = option
def add_groups(self, *args: OptionGroup) -> None:
"""Adds a sequence of OptionGroups to the OptionSet.
Args:
args (Sequence[OptionGroup])
"""
for option_group in args:
self.groups[option_group.name] = option_group
def parser(self) -> ArgumentParser:
"""Returns an ``ArgumentParser`` for this option set.
Generally, you won't use this directly. Instead, use
:func:`parse_args`.
"""
parser = argparse.ArgumentParser(*self.parser_args, **self.parser_kwargs)
groups = {
name: parser.add_argument_group(*option_group.args, **option_group.kwargs)
for name, option_group in self.groups.items()
}
for option in self.options.values():
if option.hidden is True:
continue
# Every option must have a group (except for hidden options)
if option.group is None:
raise ValueError(
f"Option {option.name} must either have a group or be hidden."
)
argument = groups[option.group.name].add_argument(
*option.flags, help=option.help, default=option.default, **option.kwargs
)
if option.completer:
argument.completer = option.completer # type: ignore[attr-defined]
return parser
def print_help(self) -> None:
return self.parser().print_help()
def _finalize_args(self, args: Namespace) -> None:
"""Does any necessary post-processing on arguments."""
for option in self.options.values():
# Handle hidden items.
if option.hidden and not hasattr(args, option.name):
setattr(args, option.name, option.default)
value = getattr(args, option.name)
# Handle options that have finalizer functions.
if option.finalizer_func:
setattr(args, option.name, option.finalizer_func(value, args))
def parse_args(self) -> Namespace:
parser = self.parser()
argcomplete.autocomplete(parser)
args = parser.parse_args()
try:
self._finalize_args(args)
except ArgumentError as err:
parser.error(str(err))
return args
def namespace(self, **kwargs: Any) -> argparse.Namespace:
"""Return a namespace that contains all of the options in this set.
kwargs can be used to set values and does so in a checked way - you
can not set an option that does not exist in the set. This is useful
for testing.
"""
args = {option.name: option.default for option in self.options.values()}
# Don't use update - validate that the keys actually exist so that
# we don't accidentally set non-existent options.
# don't bother with coverage here, this is effectively only ever
# used in tests.
for key, value in kwargs.items():
if key not in args:
raise KeyError(f"{key} is not an option.")
args[key] = value
return argparse.Namespace(**args)
def noxfile_namespace(self) -> Namespace:
"""Returns a namespace of options that can be set in the configuration
file."""
return argparse.Namespace(
**{
option.name: option.default
for option in self.options.values()
if option.noxfile
}
)
def merge_namespaces(
self, command_args: Namespace, noxfile_args: Namespace
) -> None:
"""Merges the command-line options with the noxfile options."""
command_args_copy = Namespace(**vars(command_args))
for name, option in self.options.items():
if option.merge_func:
setattr(
command_args,
name,
option.merge_func(command_args_copy, noxfile_args),
)
elif option.noxfile:
value = getattr(command_args_copy, name, None) or getattr(
noxfile_args, name, None
)
setattr(command_args, name, value)