|
14 | 14 | """The Planet Data CLI.""" |
15 | 15 | from datetime import datetime |
16 | 16 | import json |
17 | | -from typing import List, Union |
| 17 | +from typing import List, Optional |
18 | 18 | from contextlib import asynccontextmanager |
19 | 19 |
|
20 | 20 | import click |
@@ -138,97 +138,93 @@ class DateTimeType(click.ParamType): |
138 | 138 | name = 'datetime' |
139 | 139 |
|
140 | 140 | def convert(self, value, param, ctx) -> datetime: |
141 | | - if isinstance(value, datetime): |
142 | | - return value |
143 | | - else: |
| 141 | + if not isinstance(value, datetime): |
144 | 142 | try: |
145 | | - return io.str_to_datetime(value) |
| 143 | + value = io.str_to_datetime(value) |
146 | 144 | except exceptions.PlanetError as e: |
147 | 145 | self.fail(str(e)) |
148 | 146 |
|
| 147 | + return value |
| 148 | + |
149 | 149 |
|
150 | 150 | class CommaSeparatedString(click.types.StringParamType): |
151 | 151 | """A list of strings that is extracted from a comma-separated string.""" |
152 | 152 |
|
153 | 153 | def convert(self, value, param, ctx) -> List[str]: |
154 | 154 | value = super().convert(value, param, ctx) |
155 | 155 |
|
156 | | - if isinstance(value, list): |
157 | | - return value |
158 | | - else: |
159 | | - return [part.strip() for part in value.split(",")] |
| 156 | + if not isinstance(value, list): |
| 157 | + value = [part.strip() for part in value.split(",")] |
| 158 | + |
| 159 | + return value |
160 | 160 |
|
161 | 161 |
|
162 | | -class CommaSeparatedFloat(CommaSeparatedString): |
| 162 | +class CommaSeparatedFloat(click.types.StringParamType): |
163 | 163 | """A list of floats that is extracted from a comma-separated string.""" |
164 | 164 | name = 'VALUE' |
165 | 165 |
|
166 | | - def convert(self, value, param, ctx): |
167 | | - values = super().convert(value, param, ctx) |
| 166 | + def convert(self, value, param, ctx) -> List[float]: |
| 167 | + values = CommaSeparatedString().convert(value, param, ctx) |
168 | 168 |
|
169 | 169 | try: |
170 | | - return [float(v) for v in values] |
| 170 | + ret = [float(v) for v in values] |
171 | 171 | except ValueError: |
172 | 172 | self.fail(f'Cound not convert all entries in {value} to float.') |
173 | 173 |
|
| 174 | + return ret |
| 175 | + |
174 | 176 |
|
175 | | -def assets_to_filter(ctx, param, assets: str) -> dict: |
176 | | - if assets: |
177 | | - # TODO: validate and normalize |
178 | | - return data_filter.asset_filter(assets) |
| 177 | +def assets_to_filter(ctx, param, assets: List[str]) -> Optional[dict]: |
| 178 | + # TODO: validate and normalize |
| 179 | + return data_filter.asset_filter(assets) if assets else None |
179 | 180 |
|
180 | 181 |
|
181 | | -def date_range_to_filter(ctx, param, values) -> Union[List[dict], None]: |
| 182 | +def date_range_to_filter(ctx, param, values) -> Optional[List[dict]]: |
182 | 183 |
|
183 | 184 | def _func(obj): |
184 | 185 | field, comp, value = obj |
185 | 186 | kwargs = {'field_name': field, comp: value} |
186 | 187 | return data_filter.date_range_filter(**kwargs) |
187 | 188 |
|
188 | | - if values: |
189 | | - return [_func(v) for v in values] |
| 189 | + return [_func(v) for v in values] if values else None |
190 | 190 |
|
191 | 191 |
|
192 | | -def range_to_filter(ctx, param, values) -> Union[List[dict], None]: |
| 192 | +def range_to_filter(ctx, param, values) -> Optional[List[dict]]: |
193 | 193 |
|
194 | 194 | def _func(obj): |
195 | 195 | field, comp, value = obj |
196 | 196 | kwargs = {'field_name': field, comp: value} |
197 | 197 | return data_filter.range_filter(**kwargs) |
198 | 198 |
|
199 | | - if values: |
200 | | - return [_func(v) for v in values] |
| 199 | + return [_func(v) for v in values] if values else None |
201 | 200 |
|
202 | 201 |
|
203 | | -def update_to_filter(ctx, param, values) -> Union[List[dict], None]: |
| 202 | +def update_to_filter(ctx, param, values) -> Optional[List[dict]]: |
204 | 203 |
|
205 | 204 | def _func(obj): |
206 | 205 | field, comp, value = obj |
207 | 206 | kwargs = {'field_name': field, comp: value} |
208 | 207 | return data_filter.update_filter(**kwargs) |
209 | 208 |
|
210 | | - if values: |
211 | | - return [_func(v) for v in values] |
| 209 | + return [_func(v) for v in values] if values else None |
212 | 210 |
|
213 | 211 |
|
214 | | -def number_in_to_filter(ctx, param, values) -> Union[dict, None]: |
| 212 | +def number_in_to_filter(ctx, param, values) -> Optional[List[dict]]: |
215 | 213 |
|
216 | 214 | def _func(obj): |
217 | 215 | field, values = obj |
218 | 216 | return data_filter.number_in_filter(field_name=field, values=values) |
219 | 217 |
|
220 | | - if values: |
221 | | - return [_func(v) for v in values] |
| 218 | + return [_func(v) for v in values] if values else None |
222 | 219 |
|
223 | 220 |
|
224 | | -def string_in_to_filter(ctx, param, values) -> Union[dict, None]: |
| 221 | +def string_in_to_filter(ctx, param, values) -> Optional[List[dict]]: |
225 | 222 |
|
226 | 223 | def _func(obj): |
227 | 224 | field, values = obj |
228 | 225 | return data_filter.string_in_filter(field_name=field, values=values) |
229 | 226 |
|
230 | | - if values: |
231 | | - return [_func(v) for v in values] |
| 227 | + return [_func(v) for v in values] if values else None |
232 | 228 |
|
233 | 229 |
|
234 | 230 | @data.command() |
|
0 commit comments