create_min_obs_subdirs.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. Copyright © 2022 Noah Vogt <noah@noahvogt.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. """
  14. import os
  15. import config as const
  16. from .log import error_msg, log
  17. from .path import expand_dir
  18. def create_min_obs_subdirs() -> None:
  19. obs_slides_dir = expand_dir(const.OBS_SLIDES_DIR)
  20. subdirs_to_create = []
  21. for num in range(1, const.OBS_MIN_SUBDIRS + 1):
  22. subdirs_to_create.append(num)
  23. for file in os.listdir(obs_slides_dir):
  24. if file.startswith(str(const.OBS_SUBDIR_NAMING)):
  25. try:
  26. index = int(file[len(str(const.OBS_SUBDIR_NAMING)) :])
  27. except ValueError:
  28. error_msg(
  29. "could not parse file '{}' in '{}'".format(
  30. file, obs_slides_dir
  31. )
  32. )
  33. if index in subdirs_to_create:
  34. subdirs_to_create.remove(index)
  35. dirname = ""
  36. try:
  37. for number in subdirs_to_create:
  38. dirname = os.path.join(
  39. obs_slides_dir,
  40. const.OBS_SUBDIR_NAMING + str(number),
  41. )
  42. os.mkdir(dirname)
  43. log("creating empty slide directory '{}'...".format(dirname))
  44. except (FileNotFoundError, PermissionError, IOError) as error:
  45. error_msg(
  46. "Failed to create directory '{}'. Reason: {}".format(dirname, error)
  47. )