from typing import List from pycs.database.Database import Database from pycs.frontend.notifications.NotificationList import NotificationList from pycs.interfaces.MediaFileList import MediaFileList from pycs.interfaces.MediaLabel import MediaLabel class MediaStorage: """ helper class for pipelines to interact with database entities """ def __init__(self, db: Database, project_id: int, notifications: NotificationList = None): self.__db = db self.__project_id = project_id self.__notifications = notifications self.__project = self.__db.project(self.__project_id) self.__collections = self.__project.collections() def labels(self) -> List[MediaLabel]: """ receive a list of labels :return: list of labels """ label_list = self.__project.labels() label_dict = {la.identifier: MediaLabel(la) for la in label_list} result = [] for label in label_list: medial_label = label_dict[label.identifier] if label.parent_id is not None: medial_label.parent = label_dict[label.parent_id] medial_label.parent.children.append(medial_label) result.append(medial_label) return result def labels_tree(self) -> List[MediaLabel]: """ receive a tree of labels :return: list of root-level labels (parent is None) """ return list(filter( lambda ml: ml.parent is None, self.labels() )) def files(self) -> MediaFileList: """ get a FileList object to filter and receive MediaFile elements :return: FileList """ return MediaFileList(self.__project, self.__notifications)