1
1

MediaFileList.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. # TODO pydoc
  16. def filter_collection(self, collection_reference: str):
  17. self.__collection = collection_reference
  18. return self
  19. # TODO pydoc
  20. def filter_label(self, label: MediaLabel):
  21. self.__label = label
  22. return self
  23. def iter(self) -> Iterator[MediaFile]:
  24. """
  25. receive an iterator of files
  26. :return: iterator of files
  27. """
  28. if self.__collection is not None:
  29. source = self.__project.collection_by_reference(self.__collection)
  30. else:
  31. source = self.__project
  32. if self.__label is None:
  33. for file in source.files_iter():
  34. yield MediaFile(file, self.__notifications)
  35. else:
  36. for file in source.files_iter():
  37. for result in file.results():
  38. if result.label == self.__label:
  39. yield MediaFile(file, self.__notifications)
  40. break
  41. def list(self) -> List[MediaFile]:
  42. """
  43. receive a list of files
  44. :return: list of files
  45. """
  46. return list(self.iter())