CleanerImage.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from .WorkWithDB import connect, list_propusk
  2. from sqlalchemy import select
  3. from sqlalchemy.exc import OperationalError
  4. from MyMessageBox import show_dialog
  5. from PySide6.QtWidgets import QMessageBox
  6. import os, sys
  7. from logger import logger
  8. class CleanerImage:
  9. def __init__(self, path: str):
  10. self._path = path
  11. if os.path.exists(path):
  12. self._files_in_db = self._get_list_files_from_db()
  13. else:
  14. logger.error(F"Не корректный путь: {path}")
  15. sys.exit(0)
  16. def _get_list_files_from_db(self) -> list:
  17. try:
  18. with connect() as conn:
  19. list_files = select(
  20. list_propusk.c.face,
  21. list_propusk.c.document
  22. ).select_from(list_propusk)
  23. result = conn.execute(list_files).all()
  24. images = []
  25. for face, document in result:
  26. images.append(face)
  27. images.append(document)
  28. return images
  29. except OperationalError as er:
  30. logger.fatal(er)
  31. show_dialog(QMessageBox.Icon.Critical,
  32. 'Не обновлена БД',
  33. 'Не сходятся таблицы, для исправления перенесите БД в другую директорию или удалите')
  34. sys.exit(0)
  35. def clear(self) -> None:
  36. _, _, files = os.walk(self._path).__next__()
  37. for file in files:
  38. if file not in self._files_in_db:
  39. os.remove(os.path.join(self._path, file))
  40. logger.info(F"Удален файл: {os.path.join(self._path, file)}")