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

38 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-23 02:26 -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/>. 

21 

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_option", 

44 "verbose_option", 

45 "where_option", 

46 "order_by_option", 

47 "limit_option", 

48 "offset_option", 

49) 

50 

51 

52from functools import partial 

53 

54import click 

55from lsst.daf.butler.registry import CollectionType 

56 

57from ..cliLog import CliLog 

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

59 

60 

61class CollectionTypeCallback: 

62 

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

64 

65 @staticmethod 

66 def makeCollectionTypes(context, param, value): 

67 if not value: 

68 # Click seems to demand that the default be an empty tuple, rather 

69 # than a sentinal like None. The behavior that we want is that 

70 # not passing this option at all passes all collection types, while 

71 # passing it uses only the passed collection types. That works 

72 # fine for now, since there's no command-line option to subtract 

73 # collection types, and hence the only way to get an empty tuple 

74 # is as the default. 

75 return tuple(CollectionType.all()) 

76 

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

78 

79 

80collection_type_option = MWOptionDecorator( 

81 "--collection-type", 

82 callback=CollectionTypeCallback.makeCollectionTypes, 

83 multiple=True, 

84 help="If provided, only list collections of this type.", 

85 type=click.Choice(choices=CollectionTypeCallback.collectionTypes, case_sensitive=False), 

86) 

87 

88 

89collections_option = MWOptionDecorator( 

90 "--collections", 

91 help=unwrap( 

92 """One or more expressions that fully or partially identify 

93 the collections to search for datasets. If not provided all 

94 datasets are returned.""" 

95 ), 

96 multiple=True, 

97 callback=split_commas, 

98) 

99 

100 

101components_option = MWOptionDecorator( 

102 "--components/--no-components", 

103 default=None, 

104 help=unwrap( 

105 """For --components, apply all expression patterns to 

106 component dataset type names as well. For --no-components, 

107 never apply patterns to components. Default (where neither 

108 is specified) is to apply patterns to components only if 

109 their parent datasets were not matched by the expression. 

110 Fully-specified component datasets (`str` or `DatasetType` 

111 instances) are always included.""" 

112 ), 

113) 

114 

115 

116config_option = MWOptionDecorator( 

117 "-c", 

118 "--config", 

119 callback=split_kv, 

120 help="Config override, as a key-value pair.", 

121 metavar="TEXT=TEXT", 

122 multiple=True, 

123) 

124 

125 

126config_file_option = MWOptionDecorator( 

127 "-C", 

128 "--config-file", 

129 help=unwrap( 

130 """Path to a pex config override to be included after the 

131 Instrument config overrides are applied.""" 

132 ), 

133) 

134 

135 

136confirm_option = MWOptionDecorator( 

137 "--confirm/--no-confirm", 

138 default=True, 

139 help="Print expected action and a confirmation prompt before executing. Default is --confirm.", 

140) 

141 

142 

143dataset_type_option = MWOptionDecorator( 

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

145) 

146 

147 

148datasets_option = MWOptionDecorator("--datasets") 

149 

150 

151logLevelChoices = ["CRITICAL", "ERROR", "WARNING", "INFO", "VERBOSE", "DEBUG", "TRACE"] 

152log_level_option = MWOptionDecorator( 

153 "--log-level", 

154 callback=partial( 

155 split_kv, 

156 choice=click.Choice(choices=logLevelChoices, case_sensitive=False), 

157 normalize=True, 

158 unseparated_okay=True, 

159 add_to_default=True, 

160 default_key=None, # No separator 

161 ), 

162 help=f"The logging level. Without an explicit logger name, will only affect the default root loggers " 

163 f"({', '.join(CliLog.root_loggers())}). To modify the root logger use '.=LEVEL'. " 

164 f"Supported levels are [{'|'.join(logLevelChoices)}]", 

165 is_eager=True, 

166 metavar="LEVEL|COMPONENT=LEVEL", 

167 multiple=True, 

168) 

169 

170 

171long_log_option = MWOptionDecorator( 

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

173) 

174 

175log_file_option = MWOptionDecorator( 

176 "--log-file", 

177 default=None, 

178 multiple=True, 

179 callback=split_commas, 

180 type=MWPath(file_okay=True, dir_okay=False, writable=True), 

181 help="File(s) to write log messages. If the path ends with '.json' then" 

182 " JSON log records will be written, else formatted text log records" 

183 " will be written. This file can exist and records will be appended.", 

184) 

185 

186log_label_option = MWOptionDecorator( 

187 "--log-label", 

188 default=None, 

189 multiple=True, 

190 callback=split_kv, 

191 type=str, 

192 help="Keyword=value pairs to add to MDC of log records.", 

193) 

194 

195log_tty_option = MWOptionDecorator( 

196 "--log-tty/--no-log-tty", 

197 default=True, 

198 help="Log to terminal (default). If false logging to terminal is disabled.", 

199) 

200 

201options_file_option = MWOptionDecorator( 

202 "--options-file", 

203 "-@", 

204 expose_value=False, # This option should not be forwarded 

205 help=unwrap( 

206 """URI to YAML file containing overrides 

207 of command line options. The YAML should be organized 

208 as a hierarchy with subcommand names at the top 

209 level options for that subcommand below.""" 

210 ), 

211 callback=yaml_presets, 

212) 

213 

214 

215processes_option = MWOptionDecorator( 

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

217) 

218 

219 

220regex_option = MWOptionDecorator("--regex") 

221 

222 

223register_dataset_types_option = MWOptionDecorator( 

224 "--register-dataset-types", 

225 help=unwrap( 

226 """Register DatasetTypes that do not already 

227 exist in the Registry.""" 

228 ), 

229 is_flag=True, 

230) 

231 

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

233 

234 

235transfer_option = MWOptionDecorator( 

236 "-t", 

237 "--transfer", 

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

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

240 type=click.Choice( 

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

242 case_sensitive=False, 

243 ), 

244) 

245 

246 

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

248 

249 

250where_option = MWOptionDecorator("--where", help="A string expression similar to a SQL WHERE clause.") 

251 

252 

253order_by_option = MWOptionDecorator( 

254 "--order-by", 

255 help=unwrap( 

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

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

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

259 prefix it with a minus sign.""" 

260 ), 

261 multiple=True, 

262 callback=split_commas, 

263) 

264 

265 

266limit_option = MWOptionDecorator( 

267 "--limit", 

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

269 type=int, 

270 default=0, 

271) 

272 

273offset_option = MWOptionDecorator( 

274 "--offset", 

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

276 type=int, 

277 default=0, 

278)