def make_select_fields(self, product, field_names, custom_offsets): """ Parse and generate the list of select fields to be passed to the database API. """ assert product and field_names dataset_fields = product.metadata_type.dataset_fields dataset_section = product.metadata_type.definition['dataset'] select_fields = [] for field_name in field_names: if dataset_fields.get(field_name): select_fields.append(dataset_fields[field_name]) else: # try to construct the field if field_name in {'transform', 'extent', 'crs', 'bounds'}: grid_spatial = dataset_section.get('grid_spatial') if grid_spatial: select_fields.append(SimpleDocField( 'grid_spatial', 'grid_spatial', DATASET.c.metadata, False, offset=grid_spatial )) elif custom_offsets and field_name in custom_offsets: select_fields.append(SimpleDocField( field_name, field_name, DATASET.c.metadata, False, offset=custom_offsets[field_name] )) elif field_name == 'uris': select_fields.append(Field('uris', 'uris')) return select_fields
def as_expression(field: Field, value) -> Expression: """ Convert a single field/value to expression, following the "simple" conventions. """ if isinstance(value, Range): return field.between(value.begin, value.end) elif isinstance(value, list): return OrExpression(*(as_expression(field, val) for val in value)) # Treat a date (day) as a time range. elif isinstance(value, date) and not isinstance(value, datetime): return as_expression( field, Range(datetime.combine(value, time.min.replace(tzinfo=tz.tzutc())), datetime.combine(value, time.max.replace(tzinfo=tz.tzutc())))) return field == value