CleanerImage.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. else:
  12. logger.error(F"Не корректный путь: {path}")
  13. raise ValueError(F"Не корректный путь: {path}")
  14. def _get_list_files_from_db(self) -> list:
  15. with connect() as conn:
  16. list_files = select(
  17. list_propusk.c.face,
  18. list_propusk.c.document
  19. ).select_from(list_propusk)
  20. result = conn.execute(list_files).all()
  21. images = []
  22. for face, document in result:
  23. images.append(face)
  24. images.append(document)
  25. return images
  26. def clear(self) -> None:
  27. _, _, files = os.walk(self._path).__next__()
  28. for file in files:
  29. if file not in self._files_in_db:
  30. os.remove(os.path.join(self._path, file))
  31. logger.info(F"Удален файл: {os.path.join(self._path, file)}")