CleanerImage.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from .WorkWithDB import connect, list_propusk
  2. from sqlalchemy import select
  3. from appdirs import user_data_dir
  4. import os
  5. class CleanerImage:
  6. def __init__(self, path: str):
  7. self._path = path
  8. if os.path.exists(path):
  9. self._files_in_db = self._get_list_files_from_db()
  10. else:
  11. raise ValueError(F"Не корректный путь: {path}")
  12. def _get_list_files_from_db(self) -> list:
  13. with connect() as conn:
  14. list_files = select(
  15. list_propusk.c.face_photo,
  16. list_propusk.c.pasport_photo
  17. ).select_from(list_propusk)
  18. return sum([[self._only_file(x[0]), self._only_file(x[0])]
  19. for x in conn.execute(list_files).all()], [])
  20. def _only_file(self, path: str) -> str:
  21. if '/' in path:
  22. return path.split('/')[-1]
  23. else:
  24. return path.split('\\')[-1]
  25. def clear(self) -> None:
  26. _, _, files = os.walk(self._path).__next__()
  27. for file in files:
  28. if file not in self._files_in_db:
  29. os.remove(os.path.join(self._path, file))
  30. print(F"Удален файл: {os.path.join(self._path, file)}")