def wrapper(properties="", exclude="", **kwargs): include = list(filter(lambda p: p != "", properties.split(","))) exclude = list(filter(lambda p: p != "", exclude.split(","))) fields = {name: True for name in include} fields.update(**{name: False for name in exclude}) kwargs["fields"] = FieldsRequest.create(fields) return func(**kwargs)
def __init__( self, conservator, wrapping_type=None, query=None, base_operation=None, fields=None, page_size=25, unpack_field=None, reverse=False, total_unpack_field=None, **kwargs, ): # Unfortunately, query is a required arg, but for backwards-compatibility reasons can't be made required. assert query is not None self._conservator = conservator self._query = query self.fields = FieldsRequest.create(fields) self._page = 0 self._limit = page_size self.unpack_field = unpack_field self.results = [] self.reverse = reverse self._total_items = 0 if reverse: if not total_unpack_field: raise KeyError( f"total_unpack_field must be supplied if reverse is True") self.fields.include_field(total_unpack_field) # Perform a single-entry query to collect the total count of items. try: results = self._conservator.query(query=self._query, fields=self.fields, page=1, limit=1, **kwargs) except AttributeError as exc: if str(exc).endswith(total_unpack_field): raise KeyError(total_unpack_field) raise self._total_items = getattr(results, total_unpack_field) if self._limit > self._total_items: self._limit = self._total_items # Don't confuse the API. if self._limit == 0: self._limit = 1 # Set the page number to the last page of results. if self._total_items > self._limit: self._page = self._total_items // self._limit if self._total_items % self._limit: # Count any partial page. self._page += 1 # Page numbers are 0-based. self._page -= 1 self.kwargs = kwargs self.started = False self.done = False self.filters = []
def including_all_fields(self): """ Include all non-excluded fields in the results. """ if self.started: raise ConcurrentQueryModificationException() self.fields = FieldsRequest.create(None) return self
def wrapper(self, *args, **kwargs): fr = FieldsRequest.create(fields) if hasattr(self, "populate"): self.populate(fr) for field in fields: if not self.has_field(field): raise MissingFieldException( f"Missing required field '{field}'") return f(self, *args, **kwargs)
def list_media(identifier, recursive): manager = get_instance() collection_fields = FieldsRequest.create(("id", "name", "path")) top_collection = manager.from_string(identifier, collection_fields) media_fields = FieldsRequest.create(("id", "name")) if recursive: collection_paths = top_collection.recursively_get_children( include_self=True, fields=collection_fields) else: collection_paths = [top_collection] no_results = True for coll in collection_paths: for media_file in coll.get_media(media_fields): click.echo(f"{coll.path}/{media_file.name}") no_results = False if no_results: click.echo(f"No media found in collection {identifier}") return True
def download_metadata(self, path): """Downloads image and video metadata to ``media_metadata/``.""" path = os.path.join(path, "media_metadata") os.makedirs(path, exist_ok=True) fields = FieldsRequest.create(["metadata", "filename"]) videos = self.get_videos(fields=fields) for video in videos: video.download_metadata(path) images = self.get_images(fields=fields) for image in images: image.download_metadata(path)
def _query(self, query, fields, **kwargs): type_ = query.type op = Operation(query.container) query_name = query.name query = getattr(op, query_name) query(**kwargs) fr = FieldsRequest.create(fields) fr.prepare_query(query) result = self.run(op) value = getattr(result, query_name) return TypeProxy.wrap(self, type_, value)
def populate(self, fields=None): """ Query conservator for the specified fields, even if they already exist on the object. To filter existing fields, use :func:`~FLIR.conservator.wrappers.type_proxy.requires_fields` """ fields = FieldsRequest.create(fields) result = self._populate( fields) # returns a TypeProxy with the new fields if result is None: raise InvalidIdException( f"Query with id='{self.id}' returned None") # copy over fields from other _instance (to get unproxied) for field in result._instance: v = getattr(result._instance, field) setattr(self._instance, field, v) self._initialized_fields.append(field)
def recursively_get_children(self, include_self=False, fields=None): """ Yields all child collections recursively. :param include_self: If `True`, yield this collection too. :param fields: The `fields` to populate on children. """ fields = FieldsRequest.create(fields) fields.include("children.id") self.populate(fields) if include_self: yield self collections = [*self.children] while len(collections) > 0: collection = collections.pop() collection.populate(fields) yield collection collections.extend(collection.children)
def _query_frames(self, start_frame_index=None, frame_index=None, fields=None): fields = FieldsRequest.create(fields) video_fields = { "frames": { "start_frame_index": start_frame_index, "frame_index": frame_index, } } for path, value in fields.paths.items(): new_path = "frames." + path video_fields[new_path] = value video = self._conservator.query( query=self.by_id_query, fields=video_fields, id=self.id, ) return video.frames
def from_path(self, string, fields="id"): if "/" not in string: return None # start by path lookup parent_path = "/".join(string.split("/")[:-1]) name = string.split("/")[-1] parent = self._conservator.collections.from_remote_path( path=parent_path, make_if_no_exist=False, fields="id" ) # look inside parent for media with exact name match fields = FieldsRequest.create(fields) fields.include_field("name") media = list(parent.get_media(fields=fields, search_text="name:" + name)) media = [m for m in media if m.name == name] if len(media) == 1: return media[0] if len(media) > 1: raise AmbiguousIdentifierException(string) return None
def with_fields(self, fields): """Sets the query's :class:`~FLIR.conservator.fields_request.FieldsRequest` to `fields`.""" if self.started: raise ConcurrentQueryModificationException() self.fields = FieldsRequest.create(fields) return self