MediaFileList.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from typing import List, Iterator
  2. from pycs.database.Project import Project
  3. from pycs.frontend.notifications.NotificationList import NotificationList
  4. from pycs.interfaces.MediaFile import MediaFile
  5. from pycs.interfaces.MediaLabel import MediaLabel
  6. class MediaFileList:
  7. """
  8. helper class to filter and receive MediaFile elements
  9. """
  10. def __init__(self, project: Project, notifications: NotificationList):
  11. self.__project = project
  12. self.__notifications = notifications
  13. self.__collection = None
  14. self.__label = None
  15. def filter_collection(self, collection_reference: str):
  16. """
  17. only include results matching the collection
  18. :param collection_reference: reference string
  19. :return: MediaFileList
  20. """
  21. self.__collection = collection_reference
  22. return self
  23. def filter_label(self, label: MediaLabel):
  24. """
  25. only include results matching the label
  26. :param label: label
  27. :return: MediaFileList
  28. """
  29. self.__label = label
  30. return self
  31. def iter(self) -> Iterator[MediaFile]:
  32. """
  33. receive an iterator of files
  34. :return: iterator of files
  35. """
  36. if self.__collection is not None:
  37. source = self.__project.collection_by_reference(self.__collection)
  38. else:
  39. source = self.__project
  40. if self.__label is None:
  41. for file in source.files.all():
  42. yield MediaFile(file, self.__notifications)
  43. else:
  44. for file in source.files.all():
  45. for result in file.results.all():
  46. if result.label == self.__label:
  47. yield MediaFile(file, self.__notifications)
  48. break
  49. def list(self) -> List[MediaFile]:
  50. """
  51. receive a list of files
  52. :return: list of files
  53. """
  54. return list(self.iter())