-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli_common.py
More file actions
executable file
·326 lines (285 loc) · 9.1 KB
/
cli_common.py
File metadata and controls
executable file
·326 lines (285 loc) · 9.1 KB
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
# vim:ts=4:sw=4:tw=0:sts=4:et
"""Shared CLI helpers for exporters and validation tools."""
import argparse
import logging as log
import os
from typing import Iterable, Optional
from dotenv import load_dotenv
load_dotenv()
DEFAULT_DIRECTORY_USERNAME = os.getenv("DIRECTORYUSERNAME")
DEFAULT_DIRECTORY_PASSWORD = os.getenv("DIRECTORYPASSWORD")
DEFAULT_DIRECTORY_TOKEN = os.getenv("DIRECTORYTOKEN")
class ExtendAction(argparse.Action):
"""Compatibility helper for Python versions without argparse extend."""
def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest) or []
items.extend(values)
setattr(namespace, self.dest, items)
def build_parser(*args, **kwargs) -> argparse.ArgumentParser:
"""Create an ArgumentParser with the shared extend action registered."""
parser = argparse.ArgumentParser(*args, **kwargs)
parser.register("action", "extend", ExtendAction)
return parser
def configure_logging(args) -> None:
"""Configure logging based on standard verbose/debug flags."""
if args.debug:
log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG)
elif args.verbose:
log.basicConfig(format="%(levelname)s: %(message)s", level=log.INFO)
else:
log.basicConfig(format="%(levelname)s: %(message)s")
def _add_hidden_aliases(
parser: argparse.ArgumentParser,
option_strings: Iterable[str],
**kwargs,
) -> None:
for option_string in option_strings:
parser.add_argument(option_string, help=argparse.SUPPRESS, **kwargs)
def add_logging_arguments(parser: argparse.ArgumentParser) -> None:
"""Add standard verbose/debug flags."""
parser.add_argument(
"-v",
"--verbose",
dest="verbose",
action="store_true",
help="verbose information about progress",
)
parser.add_argument(
"-d",
"--debug",
dest="debug",
action="store_true",
help="debug information about progress",
)
def add_xlsx_output_argument(
parser: argparse.ArgumentParser,
*,
dest: str = "outputXLSX",
default_filename: Optional[str] = None,
help_text: str = "write results to the provided XLSX file",
) -> None:
"""Add the shared primary XLSX output option."""
default_value = [default_filename] if default_filename is not None else None
parser.add_argument(
"-X",
"--output-xlsx",
dest=dest,
nargs=1,
default=default_value,
help=help_text,
)
_add_hidden_aliases(
parser,
["--output-XLSX"],
dest=dest,
nargs=1,
)
def add_optional_xlsx_output_argument(
parser: argparse.ArgumentParser,
*,
dest: str,
long_option: str,
help_text: str,
short_option: Optional[str] = None,
legacy_long_options: Optional[Iterable[str]] = None,
) -> None:
"""Add an optional XLSX file path argument with optional legacy aliases."""
option_strings = [long_option]
if short_option is not None:
option_strings.insert(0, short_option)
parser.add_argument(*option_strings, dest=dest, nargs=1, help=help_text)
if legacy_long_options:
_add_hidden_aliases(
parser,
legacy_long_options,
dest=dest,
nargs=1,
)
def add_no_stdout_argument(parser: argparse.ArgumentParser) -> None:
"""Add the shared stdout suppression flag."""
parser.add_argument(
"-N",
"--no-stdout",
dest="nostdout",
action="store_true",
help="suppress stdout output",
)
_add_hidden_aliases(
parser,
["--output-no-stdout"],
dest="nostdout",
action="store_true",
)
def add_validation_warning_argument(parser: argparse.ArgumentParser) -> None:
"""Add shared suppression for non-fatal local validation warnings."""
parser.add_argument(
"--suppress-validation-warnings",
dest="suppress_validation_warnings",
action="store_true",
help="suppress non-fatal local validation warnings (config/cache/input normalization)",
)
def add_withdrawn_scope_arguments(
parser: argparse.ArgumentParser,
*,
include_dest: str = "include_withdrawn",
only_dest: str = "only_withdrawn",
include_help_text: str = "include withdrawn entities in the check/export run",
only_help_text: str = "run only on withdrawn entities",
) -> None:
"""Add shared flags for withdrawn-scope selection."""
parser.add_argument(
"-w",
"--include-withdrawn",
dest=include_dest,
action="store_true",
help=include_help_text,
)
parser.add_argument(
"--only-withdrawn",
dest=only_dest,
action="store_true",
help=only_help_text,
)
def add_include_withdrawn_argument(
parser: argparse.ArgumentParser,
*,
dest: str = "include_withdrawn",
help_text: str = "include withdrawn entities in the check/export run",
) -> None:
"""Backward-compatible wrapper for include-withdrawn-only use."""
add_withdrawn_scope_arguments(
parser,
include_dest=dest,
only_dest="__unused_only_withdrawn",
include_help_text=help_text,
only_help_text=argparse.SUPPRESS,
)
def add_purge_cache_arguments(
parser: argparse.ArgumentParser,
cache_choices,
*,
dest: str = "purgeCaches",
include_purge_all: bool = True,
include_purge_cache: bool = True,
) -> None:
"""Add shared cache purging options."""
if include_purge_all:
parser.add_argument(
"--purge-all-caches",
dest=dest,
action="store_const",
const=list(cache_choices),
help="purge all configured caches",
)
if include_purge_cache:
parser.add_argument(
"--purge-cache",
dest=dest,
nargs="+",
action="extend",
choices=cache_choices,
help="purge one or more configured caches",
)
def add_directory_auth_arguments(parser: argparse.ArgumentParser) -> None:
"""Add username/password and token arguments for Directory login."""
parser.add_argument(
"-u",
"--username",
dest="username",
default=DEFAULT_DIRECTORY_USERNAME,
help="username of the account used to log in to the Directory",
)
parser.add_argument(
"-p",
"--password",
dest="password",
default=DEFAULT_DIRECTORY_PASSWORD,
help="password of the account used to log in to the Directory",
)
parser.add_argument(
"-t",
"--token",
dest="token",
default=DEFAULT_DIRECTORY_TOKEN,
help="access token for the Directory (alternative to username/password)",
)
def add_directory_schema_argument(
parser: argparse.ArgumentParser,
*,
default: str = "ERIC",
dest: str = "schema",
) -> None:
"""Add the standard Directory schema/staging-area argument."""
parser.add_argument(
"-P",
"--schema",
dest=dest,
default=default,
help="Directory schema/staging area name",
)
_add_hidden_aliases(
parser,
["--package"],
dest=dest,
)
def build_directory_kwargs(args, *, pp=None) -> dict:
"""Build normalized keyword arguments for Directory(...)."""
include_withdrawn = bool(
getattr(args, "include_withdrawn", False)
or getattr(args, "only_withdrawn", False)
)
kwargs = {
"schema": getattr(args, "schema", "ERIC"),
"purgeCaches": getattr(args, "purgeCaches", []),
"debug": getattr(args, "debug", False),
"pp": pp,
"include_withdrawn_entities": include_withdrawn,
"only_withdrawn_entities": bool(getattr(args, "only_withdrawn", False)),
}
username = getattr(args, "username", None)
password = getattr(args, "password", None)
if username is not None and password is not None:
kwargs["username"] = username
kwargs["password"] = password
token = getattr(args, "token", None)
if token is not None:
kwargs["token"] = token
return kwargs
def add_remote_check_disable_arguments(
parser: argparse.ArgumentParser,
remote_check_choices,
*,
dest: str = "disableChecksRemote",
) -> None:
"""Add QC-only remote check disable arguments."""
parser.add_argument(
"-r",
"--disable-checks-all-remote",
dest=dest,
action="store_const",
const=list(remote_check_choices),
help="disable all remote checks",
)
parser.add_argument(
"--disable-checks-remote",
dest=dest,
nargs="+",
action="extend",
choices=remote_check_choices,
help="disable one or more remote checks",
)
def add_plugin_disable_argument(
parser: argparse.ArgumentParser,
plugin_choices,
*,
dest: str = "disablePlugins",
) -> None:
"""Add QC-only plugin disable argument."""
parser.add_argument(
"--disable-plugins",
dest=dest,
nargs="+",
action="extend",
choices=plugin_choices,
help="disable one or more checks/plugins",
)