FileUtils.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from glob import glob
  2. import os
  3. import pickle
  4. expected_subfolders = sorted(["Motion", "Lapse", "Full"])
  5. def list_folders(path: str) -> list:
  6. """Returns the names of all immediate child folders of path.
  7. Args:
  8. path (str): path to search
  9. Returns:
  10. list: list of all child folder names
  11. """
  12. return [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]
  13. def list_jpegs_recursive(path: str) -> list:
  14. """Recursively lists all jpeg files in path.
  15. Args:
  16. path (str): path to search
  17. Returns:
  18. list: list of all jpeg files
  19. """
  20. print(os.path.join(path, "**/*.jpg"))
  21. return [name for name in glob(os.path.join(path, "**/*.jpg"), recursive=True) if os.path.isfile(os.path.join(path, name))]
  22. def verify_expected_subfolders(session_path: str):
  23. """Assert that the given session folder contains exactly the three subfolders Motion, Lapse, Full.
  24. Args:
  25. session_path (str): session folder path
  26. """
  27. subfolders = list_folders(session_path)
  28. if sorted(subfolders) != sorted(expected_subfolders):
  29. raise AssertionError(f"{session_path}: Expected subfolders {expected_subfolders} but found {subfolders}")
  30. # Pickle helpers
  31. def dump(filename: str, data):
  32. with open(filename, "wb") as f:
  33. pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
  34. def load(filename: str):
  35. with open(filename, "rb") as f:
  36. return pickle.load(f)