MediaStorage.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from typing import List
  2. from pycs.database.Project import Project
  3. from pycs.frontend.notifications.NotificationList import NotificationList
  4. from pycs.interfaces.MediaFileList import MediaFileList
  5. from pycs.interfaces.MediaLabel import MediaLabel
  6. class MediaStorage:
  7. """
  8. helper class for pipelines to interact with database entities
  9. """
  10. def __init__(self, project_id: int, notifications: NotificationList = None):
  11. self.__project_id = project_id
  12. self.__notifications = notifications
  13. self.__project = Project.query.get(self.__project_id)
  14. # this one is not used anywhere
  15. # self.__collections = self.__project.collections.all()
  16. def labels(self) -> List[MediaLabel]:
  17. """
  18. receive a list of labels
  19. :return: list of labels
  20. """
  21. label_list = self.__project.labels.all()
  22. label_dict = {la.id: MediaLabel(la) for la in label_list}
  23. result = []
  24. for label in label_list:
  25. medial_label = label_dict[label.id]
  26. if label.parent_id is not None:
  27. medial_label.parent = label_dict[label.parent_id]
  28. medial_label.parent.children.append(medial_label)
  29. result.append(medial_label)
  30. return result
  31. def labels_tree(self) -> List[MediaLabel]:
  32. """
  33. receive a tree of labels
  34. :return: list of root-level labels (parent is None)
  35. """
  36. return [label for label in self.labels() if label.parent is None]
  37. def files(self) -> MediaFileList:
  38. """
  39. get a FileList object to filter and receive MediaFile elements
  40. :return: FileList
  41. """
  42. return MediaFileList(self.__project, self.__notifications)