Coverage for python/lsst/daf/butler/cli/opt/options.py: 80%
44 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-04 02:04 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-04 02:04 -0800
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_option",
44 "verbose_option",
45 "where_option",
46 "order_by_option",
47 "limit_option",
48 "offset_option",
49)
51from functools import partial
52from typing import Any
54import click
55from lsst.daf.butler.registry import CollectionType
57from ..cliLog import CliLog
58from ..utils import MWOptionDecorator, MWPath, split_commas, split_kv, unwrap, yaml_presets
61class CollectionTypeCallback:
63 collectionTypes = tuple(collectionType.name for collectionType in CollectionType.all())
65 @staticmethod
66 def makeCollectionTypes(
67 context: click.Context, param: click.Option, value: tuple[str, ...] | str
68 ) -> tuple[CollectionType, ...]:
69 if not value:
70 # Click seems to demand that the default be an empty tuple, rather
71 # than a sentinal like None. The behavior that we want is that
72 # not passing this option at all passes all collection types, while
73 # passing it uses only the passed collection types. That works
74 # fine for now, since there's no command-line option to subtract
75 # collection types, and hence the only way to get an empty tuple
76 # is as the default.
77 return tuple(CollectionType.all())
79 return tuple(CollectionType.from_name(item) for item in split_commas(context, param, value))
82collection_type_option = MWOptionDecorator(
83 "--collection-type",
84 callback=CollectionTypeCallback.makeCollectionTypes,
85 multiple=True,
86 help="If provided, only list collections of this type.",
87 type=click.Choice(choices=CollectionTypeCallback.collectionTypes, case_sensitive=False),
88)
91collections_option = MWOptionDecorator(
92 "--collections",
93 help=unwrap(
94 """One or more expressions that fully or partially identify
95 the collections to search for datasets. If not provided all
96 datasets are returned."""
97 ),
98 multiple=True,
99 callback=split_commas,
100)
103components_option = MWOptionDecorator(
104 "--components/--no-components",
105 default=None,
106 help=unwrap(
107 """For --components, apply all expression patterns to
108 component dataset type names as well. For --no-components,
109 never apply patterns to components. Default (where neither
110 is specified) is to apply patterns to components only if
111 their parent datasets were not matched by the expression.
112 Fully-specified component datasets (`str` or `DatasetType`
113 instances) are always included."""
114 ),
115)
118def _config_split(*args: Any) -> dict[str, str]:
119 # Config values might include commas so disable comma-splitting.
120 result = split_kv(*args, multiple=False)
121 assert isinstance(result, dict), "For mypy check that we get the expected result"
122 return result
125config_option = MWOptionDecorator(
126 "-c",
127 "--config",
128 callback=_config_split,
129 help="Config override, as a key-value pair.",
130 metavar="TEXT=TEXT",
131 multiple=True,
132)
135config_file_option = MWOptionDecorator(
136 "-C",
137 "--config-file",
138 help=unwrap(
139 """Path to a pex config override to be included after the
140 Instrument config overrides are applied."""
141 ),
142)
145confirm_option = MWOptionDecorator(
146 "--confirm/--no-confirm",
147 default=True,
148 help="Print expected action and a confirmation prompt before executing. Default is --confirm.",
149)
152dataset_type_option = MWOptionDecorator(
153 "-d", "--dataset-type", callback=split_commas, help="Specific DatasetType(s) to validate.", multiple=True
154)
157datasets_option = MWOptionDecorator("--datasets")
160logLevelChoices = ["CRITICAL", "ERROR", "WARNING", "INFO", "VERBOSE", "DEBUG", "TRACE"]
161log_level_option = MWOptionDecorator(
162 "--log-level",
163 callback=partial(
164 split_kv,
165 choice=click.Choice(choices=logLevelChoices, case_sensitive=False),
166 normalize=True,
167 unseparated_okay=True,
168 add_to_default=True,
169 default_key=None, # No separator
170 ),
171 help=f"The logging level. Without an explicit logger name, will only affect the default root loggers "
172 f"({', '.join(CliLog.root_loggers())}). To modify the root logger use '.=LEVEL'. "
173 f"Supported levels are [{'|'.join(logLevelChoices)}]",
174 is_eager=True,
175 metavar="LEVEL|COMPONENT=LEVEL",
176 multiple=True,
177)
180long_log_option = MWOptionDecorator(
181 "--long-log", help="Make log messages appear in long format.", is_flag=True
182)
184log_file_option = MWOptionDecorator(
185 "--log-file",
186 default=None,
187 multiple=True,
188 callback=split_commas,
189 type=MWPath(file_okay=True, dir_okay=False, writable=True),
190 help="File(s) to write log messages. If the path ends with '.json' then"
191 " JSON log records will be written, else formatted text log records"
192 " will be written. This file can exist and records will be appended.",
193)
195log_label_option = MWOptionDecorator(
196 "--log-label",
197 default=None,
198 multiple=True,
199 callback=split_kv,
200 type=str,
201 help="Keyword=value pairs to add to MDC of log records.",
202)
204log_tty_option = MWOptionDecorator(
205 "--log-tty/--no-log-tty",
206 default=True,
207 help="Log to terminal (default). If false logging to terminal is disabled.",
208)
210options_file_option = MWOptionDecorator(
211 "--options-file",
212 "-@",
213 expose_value=False, # This option should not be forwarded
214 help=unwrap(
215 """URI to YAML file containing overrides
216 of command line options. The YAML should be organized
217 as a hierarchy with subcommand names at the top
218 level options for that subcommand below."""
219 ),
220 callback=yaml_presets,
221)
224processes_option = MWOptionDecorator(
225 "-j", "--processes", default=1, help="Number of processes to use.", type=click.IntRange(min=1)
226)
229regex_option = MWOptionDecorator("--regex")
232register_dataset_types_option = MWOptionDecorator(
233 "--register-dataset-types",
234 help=unwrap(
235 """Register DatasetTypes that do not already
236 exist in the Registry."""
237 ),
238 is_flag=True,
239)
241run_option = MWOptionDecorator("--output-run", help="The name of the run datasets should be output to.")
244transfer_option = MWOptionDecorator(
245 "-t",
246 "--transfer",
247 default="auto", # set to `None` if using `required=True`
248 help="The external data transfer mode.",
249 type=click.Choice(
250 choices=["auto", "link", "symlink", "hardlink", "copy", "move", "relsymlink", "direct"],
251 case_sensitive=False,
252 ),
253)
256verbose_option = MWOptionDecorator("-v", "--verbose", help="Increase verbosity.", is_flag=True)
259where_option = MWOptionDecorator("--where", help="A string expression similar to a SQL WHERE clause.")
262order_by_option = MWOptionDecorator(
263 "--order-by",
264 help=unwrap(
265 """One or more comma-separated names used to order records. Names can be dimension names,
266 metadata names optionally prefixed by a dimension name and dot, or
267 timestamp_begin/timestamp_end (with optional dimension name). To reverse ordering for a name
268 prefix it with a minus sign."""
269 ),
270 multiple=True,
271 callback=split_commas,
272)
275limit_option = MWOptionDecorator(
276 "--limit",
277 help=unwrap("Limit the number of records, by default all records are shown."),
278 type=int,
279 default=0,
280)
282offset_option = MWOptionDecorator(
283 "--offset",
284 help=unwrap("Skip initial number of records, only used when --limit is specified."),
285 type=int,
286 default=0,
287)