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