clear_obs_slides_dir.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. import os
  13. import shutil
  14. import config as const
  15. from .log import log, error_msg
  16. from .path import expand_dir
  17. def clear_obs_slides_dir() -> None:
  18. log("clearing obs slides directory...")
  19. expanded_dir = expand_dir(const.OBS_SLIDES_DIR)
  20. try:
  21. for filename in os.listdir(expanded_dir):
  22. file_path = os.path.join(expanded_dir, filename)
  23. try:
  24. if os.path.isfile(file_path) or os.path.islink(file_path):
  25. os.unlink(file_path)
  26. elif os.path.isdir(file_path):
  27. shutil.rmtree(file_path)
  28. except (FileNotFoundError, PermissionError, IOError) as error:
  29. error_msg(
  30. "Failed to delete %s. Reason: %s" % (file_path, error)
  31. )
  32. except (FileNotFoundError, PermissionError, IOError) as error:
  33. error_msg(
  34. "could not list directory '{}'. Reason: {}".format(
  35. expanded_dir, error
  36. )
  37. )