6
0

MediaStorage.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. self.__collections = self.__project.collections.all()
  15. def labels(self) -> List[MediaLabel]:
  16. """
  17. receive a list of labels
  18. :return: list of labels
  19. """
  20. label_list = self.__project.labels.all()
  21. label_dict = {la.id: MediaLabel(la) for la in label_list}
  22. result = []
  23. for label in label_list:
  24. medial_label = label_dict[label.id]
  25. if label.parent_id is not None:
  26. medial_label.parent = label_dict[label.parent_id]
  27. medial_label.parent.children.append(medial_label)
  28. result.append(medial_label)
  29. return result
  30. def labels_tree(self) -> List[MediaLabel]:
  31. """
  32. receive a tree of labels
  33. :return: list of root-level labels (parent is None)
  34. """
  35. return list(filter(
  36. lambda ml: ml.parent is None,
  37. self.labels.all()
  38. ))
  39. def files(self) -> MediaFileList:
  40. """
  41. get a FileList object to filter and receive MediaFile elements
  42. :return: FileList
  43. """
  44. return MediaFileList(self.__project, self.__notifications)