-
Notifications
You must be signed in to change notification settings - Fork 29
/
argx.py
404 lines (324 loc) · 13.9 KB
/
argx.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# Copyright 2015, Aiven, https://aiven.io/
#
# This file is under the Apache License, Version 2.0.
# See the file `LICENSE` for details.
from __future__ import annotations
from .pretty import TableLayout
from aiven.client import envdefault, pretty
from argparse import Action, ArgumentParser, Namespace
from os import PathLike
from typing import Any, Callable, cast, Collection, Mapping, NoReturn, Sequence, TextIO, TYPE_CHECKING, TypeVar
import aiven.client.client
import argparse
import csv as csvlib
import errno
import functools
import json as jsonlib
import logging
import os
import requests.exceptions
import sys
# Optional shell completions
try:
import argcomplete # type: ignore
ARGCOMPLETE_INSTALLED = True
except ImportError:
ARGCOMPLETE_INSTALLED = False
try:
from .version import __version__
except ImportError:
__version__ = "UNKNOWN"
SKIP_EVALUATION_TYPES = (property, functools.cached_property)
ARG_LIST_PROP = "_arg_list"
LOG_FORMAT = "%(levelname)s\t%(message)s"
class ArgumentDeprecationNotice(argparse.Action):
"""Base class for creating deprecation notice for the arguments of avn CLI."""
log = logging.getLogger("deprecation-notice")
help_prefix = "(DEPRECATED)"
message = "Argument is deprecated: '%s'."
message_hint: str | None = None
help_hint: str | None = None
ignore_argument: bool = False
log_level = logging.WARNING
def __init__(self, *args: Any, **kwargs: Any) -> None:
for k, v in kwargs.copy().items():
if k.startswith("deprecation_"):
setattr(self, "_".join(k.split("_")[1:]), v)
kwargs.pop(k)
super().__init__(*args, **kwargs)
def __call__(
self,
parser: ArgumentParser,
namespace: Namespace,
values: str | Sequence[Any] | None,
option_string: str | None = None,
) -> None:
# default action is store
setattr(namespace, self.dest, values)
if values is not None:
msg = f"{self.message if self.message else ''}{' ' + self.message_hint if self.message_hint else ''}"
self.log.log(self.log_level, msg, ",".join(self.option_strings))
if self.ignore_argument:
delattr(namespace, self.dest)
class NextReleaseDeprecationNotice(ArgumentDeprecationNotice):
"""Action class for CLI parameters that will be deprecated in the next major release.
Parameters:
deprecation_message_hint: (Optional) Shows the message when you use the parameter.
deprecation_help_hint: (Optional) Shows the message on help text.
Example Usage:
@arg(
"--account-id",
help="Account ID of the project",
action=argx.NextReleaseDeprecationNotice,
deprecation_message_hint="Please use `--parent-id` instead, which will be mandatory in the next release.",
deprecation_help_hint="Will be replaced by `--parent-id` in the next release.",
)
"""
message = "Argument `%s` is deprecated and will be removed in the next release."
class CustomFormatter(argparse.RawDescriptionHelpFormatter):
"""Help formatter to display the default value only for integers and non-empty strings"""
def _get_help_string(self, action: Action) -> str:
help_text = action.help or ""
if "%(default)" not in help_text and action.default is not argparse.SUPPRESS:
if action.option_strings or action.nargs in [
argparse.OPTIONAL,
argparse.ZERO_OR_MORE,
]:
if (not isinstance(action.default, bool) and isinstance(action.default, int)) or (
isinstance(action.default, str) and action.default
):
help_text += " (default: %(default)s)"
if isinstance(action, ArgumentDeprecationNotice):
help_text = (
f"{action.help_prefix + ' ' if action.help_prefix else ''}"
f"{help_text}"
f"{'. ' + action.help_hint if action.help_hint else ''}"
)
return help_text
class UserError(Exception):
"""User error"""
F = TypeVar("F", bound=Callable)
class Arg:
"""Declares an argument of an CLI command.
This decorator accepts the same arguments as `argparse.Parser::add_argument`.
Methods marked with this decorator will be exposed as a CLI command, the
argument is made available through the instance attribute `self.args`.
`args` is an `argparse.Namespace` instance.
Example usage::
class CLI(CommandLineTool):
@arg("n", type=int)
def command(self):
print(self.args.n)
"""
def __call__(self, *args: Any, **kwargs: Any) -> Callable[[F], F]:
def wrap(func: F) -> F:
arg_list = getattr(func, ARG_LIST_PROP, None)
if arg_list is None:
arg_list = []
setattr(func, ARG_LIST_PROP, arg_list)
if args or kwargs:
arg_list.insert(0, (args, kwargs))
return func
return wrap
if TYPE_CHECKING:
def __getattr__(self, name: str) -> Callable:
...
def __setattr__(self, name: str, value: Callable) -> None:
...
arg = Arg()
def name_to_cmd_parts(name: str) -> list[str]:
if "__" in name:
# allow multi-level commands, separating each level with double underscores
cmd_parts = name.split("__")
else:
# previously we only allowed two levels, separated by a single underscore
cmd_parts = name.split("_", 1)
return [part.replace("_", "-") for part in cmd_parts]
class Config(dict):
def __init__(self, file_path: PathLike):
dict.__init__(self)
self.file_path = file_path
self.load()
def load(self) -> None:
self.clear()
try:
with open(self.file_path, encoding="utf-8") as fp:
self.update(jsonlib.load(fp))
except OSError as ex:
if ex.errno == errno.ENOENT:
return
raise UserError(
"Failed to load configuration file {!r}: {}: {}".format(self.file_path, ex.__class__.__name__, ex)
) from ex
except ValueError as ex:
raise UserError("Invalid JSON in configuration file {!r}".format(self.file_path)) from ex
def save(self) -> None:
config_dir = os.path.dirname(self.file_path)
if not os.path.isdir(config_dir):
os.makedirs(config_dir)
os.chmod(config_dir, 0o700)
with open(self.file_path, "w", encoding="utf-8") as fp:
os.chmod(fp.name, 0o600)
jsonlib.dump(self, fp, sort_keys=True, indent=4)
class CommandLineTool:
config: Config
def __init__(self, name: str, parser: argparse.ArgumentParser | None = None):
self.log = logging.getLogger(name)
self._cats: dict[tuple[str, ...], argparse._SubParsersAction] = {}
self._extensions: list[CommandLineTool] = []
self.parser = parser or argparse.ArgumentParser(prog=name, formatter_class=CustomFormatter)
self.parser.add_argument(
"--config",
help="config file location %(default)r",
default=envdefault.AIVEN_CLIENT_CONFIG,
)
self.parser.add_argument("--version", action="version", version="aiven-client {}".format(__version__))
self.subparsers = self.parser.add_subparsers(title="command categories", dest="command", help="", metavar="")
self.args: Namespace = Namespace()
def add_cmd(self, func: Callable) -> None:
"""Add a parser for a single command method call"""
assert func.__doc__, f"Missing docstring for {func.__qualname__}"
cmd_parts = name_to_cmd_parts(func.__name__)
cats, cmd = cmd_parts, cmd_parts.pop()
subparsers = self.subparsers
for level in range(len(cats)):
cat = tuple(cats[: level + 1])
if cat not in self._cats:
parser = subparsers.add_parser(
cat[-1],
help=" ".join(cat).title() + " commands",
formatter_class=CustomFormatter,
)
self._cats[cat] = parser.add_subparsers()
subparsers = self._cats[cat]
parser = subparsers.add_parser(cmd, help=func.__doc__, description=func.__doc__, formatter_class=CustomFormatter)
parser.set_defaults(func=func)
for arg_prop in getattr(func, ARG_LIST_PROP, []):
parser.add_argument(*arg_prop[0], **arg_prop[1])
# Ensure the list of actions remains sorted as we append to to it.
self.subparsers._choices_actions.sort(key=lambda item: item.dest)
def add_args(self, parser: argparse.ArgumentParser) -> None:
pass # override in sub-class
def extend_commands(self, sub_client: CommandLineTool) -> None:
"""Add top-level args and all commands from a CommandLineTool instance"""
sub_client.add_args(self.parser) # top-level args
sub_client.add_cmds(self.add_cmd) # sub-commands
self._extensions.append(sub_client)
def add_cmds(self, add_func: Callable[[Callable], None]) -> None:
"""Add every method tagged with @arg as a command"""
for prop in dir(self):
# Skip @property and @cached_property attributes to delay coercing their evaluation.
classprop = getattr(self.__class__, prop, None)
if isinstance(classprop, SKIP_EVALUATION_TYPES):
continue
func = getattr(self, prop, None)
if getattr(func, ARG_LIST_PROP, None) is not None:
assert callable(func)
add_func(func)
def parse_args(self, args: Sequence[str] | None = None) -> None:
self.extend_commands(self)
if ARGCOMPLETE_INSTALLED:
argcomplete.autocomplete(self.parser)
ext_args = self.parser.parse_args(args=args)
for ext in self._extensions:
ext.args = ext_args
def pre_run(self, func: Callable) -> None:
"""Override in sub-class"""
def expected_errors(self) -> Sequence[type[BaseException]]:
return []
def _to_mapping_collection(
self, obj: Mapping[str, Any] | Collection[Mapping[str, Any]], single_item: bool = False
) -> Collection[Mapping[str, Any]]:
if single_item:
assert isinstance(obj, Mapping)
return [obj]
else:
assert isinstance(obj, Collection)
return cast(Collection[Mapping[str, Any]], obj)
def print_response(
self,
result: Mapping[str, Any] | Collection[Mapping[str, Any]],
json: bool = True,
format: str | None = None,
drop_fields: Collection[str] | None = None,
table_layout: TableLayout | None = None,
single_item: bool = False,
header: bool = True,
csv: bool = False,
file: TextIO | None = None,
) -> None:
"""print request response in chosen format"""
if file is None:
file = sys.stdout
if format is not None:
for item in self._to_mapping_collection(result, single_item=single_item):
print(format.format(**item), file=file)
elif json:
assert isinstance(result, (Collection, Mapping))
print(
jsonlib.dumps(result, indent=4, sort_keys=True, cls=pretty.CustomJsonEncoder),
file=file,
)
elif csv:
fields = []
assert table_layout is not None
for field in table_layout:
if isinstance(field, str):
fields.append(field)
else:
fields.extend(field)
writer = csvlib.DictWriter(file, extrasaction="ignore", fieldnames=fields)
if header:
writer.writeheader()
for item in self._to_mapping_collection(result, single_item=single_item):
writer.writerow(item)
else:
pretty.print_table(
self._to_mapping_collection(result, single_item=single_item),
drop_fields=drop_fields,
table_layout=table_layout,
header=header,
file=file,
)
def run(self, args: Sequence[str] | None = None) -> int | None:
args = args or sys.argv[1:]
if not args:
args = ["--help"]
self.parse_args(args=args)
assert self.args is not None and hasattr(self.args, "config")
self.config = Config(self.args.config)
expected_errors: list[type[BaseException]] = [
requests.exceptions.ConnectionError,
UserError,
aiven.client.client.Error,
]
for ext in self._extensions: # note: _extensions includes self
expected_errors.extend(ext.expected_errors())
ext.config = self.config
try:
return self.run_actual(args)
except tuple(expected_errors) as ex:
# nicer output on "expected" errors
err = "command failed: {0.__class__.__name__}: {0}".format(ex)
self.log.error(err)
return 1
except OSError as ex:
if ex.errno != errno.EPIPE:
raise
self.log.error("*** output truncated ***")
return 13 # SIGPIPE value in case anyone cares
except KeyboardInterrupt:
self.log.error("*** terminated by keyboard ***")
return 2 # SIGINT
def run_actual(self, args_for_help: Sequence[str]) -> int | None:
func = getattr(self.args, "func", None)
if not func:
self.parser.parse_args(list(args_for_help) + ["--help"])
return 1
self.pre_run(func)
return func()
def main(self, args: Sequence[str] | None = None) -> NoReturn:
# TODO: configurable log level
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
logging.getLogger("requests").setLevel(logging.WARNING)
sys.exit(self.run(args))