FileUtils.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. return [name for name in glob(os.path.join(path, "**/*.jpg"), recursive=True) if os.path.isfile(os.path.join(path, name))]
  21. def verify_expected_subfolders(session_path: str):
  22. """Assert that the given session folder contains exactly the three subfolders Motion, Lapse, Full.
  23. Args:
  24. session_path (str): session folder path
  25. """
  26. subfolders = list_folders(session_path)
  27. if sorted(subfolders) != sorted(expected_subfolders):
  28. raise AssertionError(f"{session_path}: Expected subfolders {expected_subfolders} but found {subfolders}")
  29. # Pickle helpers
  30. def dump(filename: str, data):
  31. with open(filename, "wb") as f:
  32. pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL)
  33. def load(filename: str):
  34. with open(filename, "rb") as f:
  35. return pickle.load(f)