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

40 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-08-28 07:52 +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/>. 

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 

116def _config_split(*args): 

117 # Config values might include commas so disable comma-splitting. 

118 return split_kv(*args, multiple=False) 

119 

120 

121config_option = MWOptionDecorator( 

122 "-c", 

123 "--config", 

124 callback=_config_split, 

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

126 metavar="TEXT=TEXT", 

127 multiple=True, 

128) 

129 

130 

131config_file_option = MWOptionDecorator( 

132 "-C", 

133 "--config-file", 

134 help=unwrap( 

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

136 Instrument config overrides are applied.""" 

137 ), 

138) 

139 

140 

141confirm_option = MWOptionDecorator( 

142 "--confirm/--no-confirm", 

143 default=True, 

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

145) 

146 

147 

148dataset_type_option = MWOptionDecorator( 

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

150) 

151 

152 

153datasets_option = MWOptionDecorator("--datasets") 

154 

155 

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

157log_level_option = MWOptionDecorator( 

158 "--log-level", 

159 callback=partial( 

160 split_kv, 

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

162 normalize=True, 

163 unseparated_okay=True, 

164 add_to_default=True, 

165 default_key=None, # No separator 

166 ), 

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

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

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

170 is_eager=True, 

171 metavar="LEVEL|COMPONENT=LEVEL", 

172 multiple=True, 

173) 

174 

175 

176long_log_option = MWOptionDecorator( 

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

178) 

179 

180log_file_option = MWOptionDecorator( 

181 "--log-file", 

182 default=None, 

183 multiple=True, 

184 callback=split_commas, 

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

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

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

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

189) 

190 

191log_label_option = MWOptionDecorator( 

192 "--log-label", 

193 default=None, 

194 multiple=True, 

195 callback=split_kv, 

196 type=str, 

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

198) 

199 

200log_tty_option = MWOptionDecorator( 

201 "--log-tty/--no-log-tty", 

202 default=True, 

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

204) 

205 

206options_file_option = MWOptionDecorator( 

207 "--options-file", 

208 "-@", 

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

210 help=unwrap( 

211 """URI to YAML file containing overrides 

212 of command line options. The YAML should be organized 

213 as a hierarchy with subcommand names at the top 

214 level options for that subcommand below.""" 

215 ), 

216 callback=yaml_presets, 

217) 

218 

219 

220processes_option = MWOptionDecorator( 

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

222) 

223 

224 

225regex_option = MWOptionDecorator("--regex") 

226 

227 

228register_dataset_types_option = MWOptionDecorator( 

229 "--register-dataset-types", 

230 help=unwrap( 

231 """Register DatasetTypes that do not already 

232 exist in the Registry.""" 

233 ), 

234 is_flag=True, 

235) 

236 

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

238 

239 

240transfer_option = MWOptionDecorator( 

241 "-t", 

242 "--transfer", 

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

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

245 type=click.Choice( 

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

247 case_sensitive=False, 

248 ), 

249) 

250 

251 

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

253 

254 

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

256 

257 

258order_by_option = MWOptionDecorator( 

259 "--order-by", 

260 help=unwrap( 

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

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

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

264 prefix it with a minus sign.""" 

265 ), 

266 multiple=True, 

267 callback=split_commas, 

268) 

269 

270 

271limit_option = MWOptionDecorator( 

272 "--limit", 

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

274 type=int, 

275 default=0, 

276) 

277 

278offset_option = MWOptionDecorator( 

279 "--offset", 

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

281 type=int, 

282 default=0, 

283)