Coverage for python/lsst/daf/butler/cli/opt/options.py: 84%
38 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-24 23:50 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-24 23:50 -0700
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/>.
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)
52from functools import partial
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(context, param, value):
66 if not value:
67 # Click seems to demand that the default be an empty tuple, rather
68 # than a sentinal like None. The behavior that we want is that
69 # not passing this option at all passes all collection types, while
70 # passing it uses only the passed collection types. That works
71 # fine for now, since there's no command-line option to subtract
72 # collection types, and hence the only way to get an empty tuple
73 # is as the default.
74 return tuple(CollectionType.all())
76 return tuple(CollectionType.from_name(item) for item in split_commas(context, param, value))
79collection_type_option = MWOptionDecorator(
80 "--collection-type",
81 callback=CollectionTypeCallback.makeCollectionTypes,
82 multiple=True,
83 help="If provided, only list collections of this type.",
84 type=click.Choice(choices=CollectionTypeCallback.collectionTypes, case_sensitive=False),
85)
88collections_option = MWOptionDecorator(
89 "--collections",
90 help=unwrap(
91 """One or more expressions that fully or partially identify
92 the collections to search for datasets. If not provided all
93 datasets are returned."""
94 ),
95 multiple=True,
96 callback=split_commas,
97)
100components_option = MWOptionDecorator(
101 "--components/--no-components",
102 default=None,
103 help=unwrap(
104 """For --components, apply all expression patterns to
105 component dataset type names as well. For --no-components,
106 never apply patterns to components. Default (where neither
107 is specified) is to apply patterns to components only if
108 their parent datasets were not matched by the expression.
109 Fully-specified component datasets (`str` or `DatasetType`
110 instances) are always included."""
111 ),
112)
115config_option = MWOptionDecorator(
116 "-c",
117 "--config",
118 callback=split_kv,
119 help="Config override, as a key-value pair.",
120 metavar="TEXT=TEXT",
121 multiple=True,
122)
125config_file_option = MWOptionDecorator(
126 "-C",
127 "--config-file",
128 help=unwrap(
129 """Path to a pex config override to be included after the
130 Instrument config overrides are applied."""
131 ),
132)
135confirm_option = MWOptionDecorator(
136 "--confirm/--no-confirm",
137 default=True,
138 help="Print expected action and a confirmation prompt before executing. Default is --confirm.",
139)
142dataset_type_option = MWOptionDecorator(
143 "-d", "--dataset-type", callback=split_commas, help="Specific DatasetType(s) to validate.", multiple=True
144)
147datasets_option = MWOptionDecorator("--datasets")
150logLevelChoices = ["CRITICAL", "ERROR", "WARNING", "INFO", "VERBOSE", "DEBUG", "TRACE"]
151log_level_option = MWOptionDecorator(
152 "--log-level",
153 callback=partial(
154 split_kv,
155 choice=click.Choice(choices=logLevelChoices, case_sensitive=False),
156 normalize=True,
157 unseparated_okay=True,
158 add_to_default=True,
159 default_key=None, # No separator
160 ),
161 help=f"The logging level. Without an explicit logger name, will only affect the default root loggers "
162 f"({', '.join(CliLog.root_loggers())}). To modify the root logger use '.=LEVEL'. "
163 f"Supported levels are [{'|'.join(logLevelChoices)}]",
164 is_eager=True,
165 metavar="LEVEL|COMPONENT=LEVEL",
166 multiple=True,
167)
170long_log_option = MWOptionDecorator(
171 "--long-log", help="Make log messages appear in long format.", is_flag=True
172)
174log_file_option = MWOptionDecorator(
175 "--log-file",
176 default=None,
177 multiple=True,
178 callback=split_commas,
179 type=MWPath(file_okay=True, dir_okay=False, writable=True),
180 help="File(s) to write log messages. If the path ends with '.json' then"
181 " JSON log records will be written, else formatted text log records"
182 " will be written. This file can exist and records will be appended.",
183)
185log_label_option = MWOptionDecorator(
186 "--log-label",
187 default=None,
188 multiple=True,
189 callback=split_kv,
190 type=str,
191 help="Keyword=value pairs to add to MDC of log records.",
192)
194log_tty_option = MWOptionDecorator(
195 "--log-tty/--no-log-tty",
196 default=True,
197 help="Log to terminal (default). If false logging to terminal is disabled.",
198)
200options_file_option = MWOptionDecorator(
201 "--options-file",
202 "-@",
203 expose_value=False, # This option should not be forwarded
204 help=unwrap(
205 """URI to YAML file containing overrides
206 of command line options. The YAML should be organized
207 as a hierarchy with subcommand names at the top
208 level options for that subcommand below."""
209 ),
210 callback=yaml_presets,
211)
214processes_option = MWOptionDecorator(
215 "-j", "--processes", default=1, help="Number of processes to use.", type=click.IntRange(min=1)
216)
219regex_option = MWOptionDecorator("--regex")
222register_dataset_types_option = MWOptionDecorator(
223 "--register-dataset-types",
224 help=unwrap(
225 """Register DatasetTypes that do not already
226 exist in the Registry."""
227 ),
228 is_flag=True,
229)
231run_option = MWOptionDecorator("--output-run", help="The name of the run datasets should be output to.")
234transfer_option = MWOptionDecorator(
235 "-t",
236 "--transfer",
237 default="auto", # set to `None` if using `required=True`
238 help="The external data transfer mode.",
239 type=click.Choice(
240 choices=["auto", "link", "symlink", "hardlink", "copy", "move", "relsymlink", "direct"],
241 case_sensitive=False,
242 ),
243)
246verbose_option = MWOptionDecorator("-v", "--verbose", help="Increase verbosity.", is_flag=True)
249where_option = MWOptionDecorator("--where", help="A string expression similar to a SQL WHERE clause.")
252order_by_option = MWOptionDecorator(
253 "--order-by",
254 help=unwrap(
255 """One or more comma-separated names used to order records. Names can be dimension names,
256 metadata names optionally prefixed by a dimension name and dot, or
257 timestamp_begin/timestamp_end (with optional dimension name). To reverse ordering for a name
258 prefix it with a minus sign."""
259 ),
260 multiple=True,
261 callback=split_commas,
262)
265limit_option = MWOptionDecorator(
266 "--limit",
267 help=unwrap("Limit the number of records, by default all records are shown."),
268 type=int,
269 default=0,
270)
272offset_option = MWOptionDecorator(
273 "--offset",
274 help=unwrap("Skip initial number of records, only used when --limit is specified."),
275 type=int,
276 default=0,
277)