Coverage for python/lsst/daf/butler/cli/opt/options.py: 80%
45 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-01 10:04 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-01 10:04 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21from __future__ import annotations
23__all__ = (
24 "CollectionTypeCallback",
25 "collection_type_option",
26 "collections_option",
27 "components_option",
28 "config_option",
29 "config_file_option",
30 "confirm_option",
31 "dataset_type_option",
32 "datasets_option",
33 "log_level_option",
34 "long_log_option",
35 "log_file_option",
36 "log_label_option",
37 "log_tty_option",
38 "options_file_option",
39 "processes_option",
40 "regex_option",
41 "register_dataset_types_option",
42 "run_option",
43 "transfer_dimensions_option",
44 "transfer_option",
45 "verbose_option",
46 "where_option",
47 "order_by_option",
48 "limit_option",
49 "offset_option",
50)
52from functools import partial
53from typing import Any
55import click
56from lsst.daf.butler.registry import CollectionType
58from ..cliLog import CliLog
59from ..utils import MWOptionDecorator, MWPath, split_commas, split_kv, unwrap, yaml_presets
62class CollectionTypeCallback:
64 collectionTypes = tuple(collectionType.name for collectionType in CollectionType.all())
66 @staticmethod
67 def makeCollectionTypes(
68 context: click.Context, param: click.Option, value: tuple[str, ...] | str
69 ) -> tuple[CollectionType, ...]:
70 if not value:
71 # Click seems to demand that the default be an empty tuple, rather
72 # than a sentinal like None. The behavior that we want is that
73 # not passing this option at all passes all collection types, while
74 # passing it uses only the passed collection types. That works
75 # fine for now, since there's no command-line option to subtract
76 # collection types, and hence the only way to get an empty tuple
77 # is as the default.
78 return tuple(CollectionType.all())
80 return tuple(CollectionType.from_name(item) for item in split_commas(context, param, value))
83collection_type_option = MWOptionDecorator(
84 "--collection-type",
85 callback=CollectionTypeCallback.makeCollectionTypes,
86 multiple=True,
87 help="If provided, only list collections of this type.",
88 type=click.Choice(choices=CollectionTypeCallback.collectionTypes, case_sensitive=False),
89)
92collections_option = MWOptionDecorator(
93 "--collections",
94 help=unwrap(
95 """One or more expressions that fully or partially identify
96 the collections to search for datasets. If not provided all
97 datasets are returned."""
98 ),
99 multiple=True,
100 callback=split_commas,
101)
104components_option = MWOptionDecorator(
105 "--components/--no-components",
106 default=None,
107 help=unwrap(
108 """For --components, apply all expression patterns to
109 component dataset type names as well. For --no-components,
110 never apply patterns to components. Default (where neither
111 is specified) is to apply patterns to components only if
112 their parent datasets were not matched by the expression.
113 Fully-specified component datasets (`str` or `DatasetType`
114 instances) are always included."""
115 ),
116)
119def _config_split(*args: Any) -> dict[str, str]:
120 # Config values might include commas so disable comma-splitting.
121 result = split_kv(*args, multiple=False)
122 assert isinstance(result, dict), "For mypy check that we get the expected result"
123 return result
126config_option = MWOptionDecorator(
127 "-c",
128 "--config",
129 callback=_config_split,
130 help="Config override, as a key-value pair.",
131 metavar="TEXT=TEXT",
132 multiple=True,
133)
136config_file_option = MWOptionDecorator(
137 "-C",
138 "--config-file",
139 help=unwrap(
140 """Path to a pex config override to be included after the
141 Instrument config overrides are applied."""
142 ),
143)
146confirm_option = MWOptionDecorator(
147 "--confirm/--no-confirm",
148 default=True,
149 help="Print expected action and a confirmation prompt before executing. Default is --confirm.",
150)
153dataset_type_option = MWOptionDecorator(
154 "-d", "--dataset-type", callback=split_commas, help="Specific DatasetType(s) to validate.", multiple=True
155)
158datasets_option = MWOptionDecorator("--datasets")
161logLevelChoices = ["CRITICAL", "ERROR", "WARNING", "INFO", "VERBOSE", "DEBUG", "TRACE"]
162log_level_option = MWOptionDecorator(
163 "--log-level",
164 callback=partial(
165 split_kv,
166 choice=click.Choice(choices=logLevelChoices, case_sensitive=False),
167 normalize=True,
168 unseparated_okay=True,
169 add_to_default=True,
170 default_key=None, # No separator
171 ),
172 help=f"The logging level. Without an explicit logger name, will only affect the default root loggers "
173 f"({', '.join(CliLog.root_loggers())}). To modify the root logger use '.=LEVEL'. "
174 f"Supported levels are [{'|'.join(logLevelChoices)}]",
175 is_eager=True,
176 metavar="LEVEL|COMPONENT=LEVEL",
177 multiple=True,
178)
181long_log_option = MWOptionDecorator(
182 "--long-log", help="Make log messages appear in long format.", is_flag=True
183)
185log_file_option = MWOptionDecorator(
186 "--log-file",
187 default=None,
188 multiple=True,
189 callback=split_commas,
190 type=MWPath(file_okay=True, dir_okay=False, writable=True),
191 help="File(s) to write log messages. If the path ends with '.json' then"
192 " JSON log records will be written, else formatted text log records"
193 " will be written. This file can exist and records will be appended.",
194)
196log_label_option = MWOptionDecorator(
197 "--log-label",
198 default=None,
199 multiple=True,
200 callback=split_kv,
201 type=str,
202 help="Keyword=value pairs to add to MDC of log records.",
203)
205log_tty_option = MWOptionDecorator(
206 "--log-tty/--no-log-tty",
207 default=True,
208 help="Log to terminal (default). If false logging to terminal is disabled.",
209)
211options_file_option = MWOptionDecorator(
212 "--options-file",
213 "-@",
214 expose_value=False, # This option should not be forwarded
215 help=unwrap(
216 """URI to YAML file containing overrides
217 of command line options. The YAML should be organized
218 as a hierarchy with subcommand names at the top
219 level options for that subcommand below."""
220 ),
221 callback=yaml_presets,
222)
225processes_option = MWOptionDecorator(
226 "-j", "--processes", default=1, help="Number of processes to use.", type=click.IntRange(min=1)
227)
230regex_option = MWOptionDecorator("--regex")
233register_dataset_types_option = MWOptionDecorator(
234 "--register-dataset-types",
235 help="Register DatasetTypes that do not already exist in the Registry.",
236 is_flag=True,
237)
239run_option = MWOptionDecorator("--output-run", help="The name of the run datasets should be output to.")
242transfer_option = MWOptionDecorator(
243 "-t",
244 "--transfer",
245 default="auto", # set to `None` if using `required=True`
246 help="The external data transfer mode.",
247 type=click.Choice(
248 choices=["auto", "link", "symlink", "hardlink", "copy", "move", "relsymlink", "direct"],
249 case_sensitive=False,
250 ),
251)
254transfer_dimensions_option = MWOptionDecorator(
255 "--transfer-dimensions/--no-transfer-dimensions",
256 is_flag=True,
257 default=True,
258 help=unwrap(
259 """If true, also copy dimension records along with datasets.
260 If the dmensions are already present in the destination butler it
261 can be more efficient to disable this. The default is to transfer
262 dimensions."""
263 ),
264)
267verbose_option = MWOptionDecorator("-v", "--verbose", help="Increase verbosity.", is_flag=True)
270where_option = MWOptionDecorator(
271 "--where", default="", help="A string expression similar to a SQL WHERE clause."
272)
275order_by_option = MWOptionDecorator(
276 "--order-by",
277 help=unwrap(
278 """One or more comma-separated names used to order records. Names can be dimension names,
279 metadata names optionally prefixed by a dimension name and dot, or
280 timestamp_begin/timestamp_end (with optional dimension name). To reverse ordering for a name
281 prefix it with a minus sign."""
282 ),
283 multiple=True,
284 callback=split_commas,
285)
288limit_option = MWOptionDecorator(
289 "--limit",
290 help=unwrap("Limit the number of records, by default all records are shown."),
291 type=int,
292 default=0,
293)
295offset_option = MWOptionDecorator(
296 "--offset",
297 help=unwrap("Skip initial number of records, only used when --limit is specified."),
298 type=int,
299 default=0,
300)