Coverage for python/lsst/daf/butler/cli/opt/options.py: 80%

45 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-15 09:13 +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 

22 

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) 

51 

52from functools import partial 

53from typing import Any 

54 

55import click 

56from lsst.daf.butler.registry import CollectionType 

57 

58from ..cliLog import CliLog 

59from ..utils import MWOptionDecorator, MWPath, split_commas, split_kv, unwrap, yaml_presets 

60 

61 

62class CollectionTypeCallback: 

63 collectionTypes = tuple(collectionType.name for collectionType in CollectionType.all()) 

64 

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()) 

78 

79 return tuple(CollectionType.from_name(item) for item in split_commas(context, param, value)) 

80 

81 

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) 

89 

90 

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) 

101 

102 

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) 

116 

117 

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 

123 

124 

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) 

133 

134 

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) 

143 

144 

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) 

150 

151 

152dataset_type_option = MWOptionDecorator( 

153 "-d", "--dataset-type", callback=split_commas, help="Specific DatasetType(s) to validate.", multiple=True 

154) 

155 

156 

157datasets_option = MWOptionDecorator("--datasets") 

158 

159 

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) 

178 

179 

180long_log_option = MWOptionDecorator( 

181 "--long-log", help="Make log messages appear in long format.", is_flag=True 

182) 

183 

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) 

194 

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) 

203 

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) 

209 

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) 

222 

223 

224processes_option = MWOptionDecorator( 

225 "-j", "--processes", default=1, help="Number of processes to use.", type=click.IntRange(min=1) 

226) 

227 

228 

229regex_option = MWOptionDecorator("--regex") 

230 

231 

232register_dataset_types_option = MWOptionDecorator( 

233 "--register-dataset-types", 

234 help="Register DatasetTypes that do not already exist in the Registry.", 

235 is_flag=True, 

236) 

237 

238run_option = MWOptionDecorator("--output-run", help="The name of the run datasets should be output to.") 

239 

240 

241transfer_option = MWOptionDecorator( 

242 "-t", 

243 "--transfer", 

244 default="auto", # set to `None` if using `required=True` 

245 help="The external data transfer mode.", 

246 type=click.Choice( 

247 choices=["auto", "link", "symlink", "hardlink", "copy", "move", "relsymlink", "direct"], 

248 case_sensitive=False, 

249 ), 

250) 

251 

252 

253transfer_dimensions_option = MWOptionDecorator( 

254 "--transfer-dimensions/--no-transfer-dimensions", 

255 is_flag=True, 

256 default=True, 

257 help=unwrap( 

258 """If true, also copy dimension records along with datasets. 

259 If the dmensions are already present in the destination butler it 

260 can be more efficient to disable this. The default is to transfer 

261 dimensions.""" 

262 ), 

263) 

264 

265 

266verbose_option = MWOptionDecorator("-v", "--verbose", help="Increase verbosity.", is_flag=True) 

267 

268 

269where_option = MWOptionDecorator( 

270 "--where", default="", help="A string expression similar to a SQL WHERE clause." 

271) 

272 

273 

274order_by_option = MWOptionDecorator( 

275 "--order-by", 

276 help=unwrap( 

277 """One or more comma-separated names used to order records. Names can be dimension names, 

278 metadata names optionally prefixed by a dimension name and dot, or 

279 timestamp_begin/timestamp_end (with optional dimension name). To reverse ordering for a name 

280 prefix it with a minus sign.""" 

281 ), 

282 multiple=True, 

283 callback=split_commas, 

284) 

285 

286 

287limit_option = MWOptionDecorator( 

288 "--limit", 

289 help=unwrap("Limit the number of records, by default all records are shown."), 

290 type=int, 

291 default=0, 

292) 

293 

294offset_option = MWOptionDecorator( 

295 "--offset", 

296 help=unwrap("Skip initial number of records, only used when --limit is specified."), 

297 type=int, 

298 default=0, 

299)