CleanerImage.py 1.4 KB

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