create_min_obs_subdirs.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 config as const
  14. from .log import error_msg, log
  15. from .path import expand_dir
  16. def create_min_obs_subdirs() -> None:
  17. obs_slides_dir = expand_dir(const.OBS_SLIDES_DIR)
  18. subdirs_to_create = []
  19. for num in range(1, const.OBS_MIN_SUBDIRS + 1):
  20. subdirs_to_create.append(num)
  21. for file in os.listdir(obs_slides_dir):
  22. if file.startswith(str(const.OBS_SUBDIR_NAMING)):
  23. try:
  24. index = int(file[len(str(const.OBS_SUBDIR_NAMING)) :])
  25. except ValueError:
  26. error_msg(
  27. "could not parse file '{}' in '{}'".format(
  28. file, obs_slides_dir
  29. )
  30. )
  31. if index in subdirs_to_create:
  32. subdirs_to_create.remove(index)
  33. dirname = ""
  34. try:
  35. for number in subdirs_to_create:
  36. dirname = os.path.join(
  37. obs_slides_dir,
  38. const.OBS_SUBDIR_NAMING + str(number),
  39. )
  40. os.mkdir(dirname)
  41. log("creating empty slide directory '{}'...".format(dirname))
  42. except (FileNotFoundError, PermissionError, IOError) as error:
  43. error_msg(
  44. "Failed to create directory '{}'. Reason: {}".format(dirname, error)
  45. )