MediaStorage.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from typing import List
  2. from pycs.database.Database import Database
  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, db: Database, project_id: int, notifications: NotificationList = None):
  11. self.__db = db
  12. self.__project_id = project_id
  13. self.__notifications = notifications
  14. self.__project = self.__db.project(self.__project_id)
  15. self.__collections = self.__project.collections()
  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()
  22. label_dict = {la.identifier: MediaLabel(la) for la in label_list}
  23. result = []
  24. for label in label_list:
  25. medial_label = label_dict[label.identifier]
  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 list(filter(
  37. lambda ml: ml.parent is None,
  38. self.labels()
  39. ))
  40. def files(self) -> MediaFileList:
  41. """
  42. get a FileList object to filter and receive MediaFile elements
  43. :return: FileList
  44. """
  45. return MediaFileList(self.__project, self.__notifications)