slide_selection_iterator.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. from utils import (
  16. log,
  17. create_min_obs_subdirs,
  18. error_msg,
  19. expand_dir,
  20. )
  21. from slides import ClassicSongTemplate, ClassicStartSlide, ClassicSongSlide
  22. from input import parse_metadata, generate_final_prompt
  23. import config as const
  24. import slidegen
  25. def slide_selection_iterator(ssync):
  26. iterator_prompt = "Exit now? [y/N]: "
  27. structure_prompt = (
  28. "Choose song structure (leave blank for full song)"
  29. + " eg. [1,R,2,R] / [1-4]: "
  30. )
  31. file_list_str = ""
  32. rclone_local_dir = expand_dir(const.RCLONE_LOCAL_DIR)
  33. obs_slides_dir = expand_dir(const.OBS_SLIDES_DIR)
  34. try:
  35. for file in os.listdir(rclone_local_dir):
  36. file_list_str += file + "\n"
  37. except (FileNotFoundError, PermissionError, IOError) as error:
  38. error_msg(
  39. "Failed to access items in '{}'. Reason: {}".format(
  40. rclone_local_dir, error
  41. )
  42. )
  43. file_list_str = file_list_str[:-1]
  44. const.SSYNC_CHOSEN_FILE_NAMING = ".chosen-tempfile"
  45. index = 0
  46. while True:
  47. index += 1
  48. input_song_prompt = "[{}{}] ".format(const.OBS_SUBDIR_NAMING, index)
  49. prompt_answer = str(input(input_song_prompt + iterator_prompt))
  50. if prompt_answer.lower() == "y":
  51. create_min_obs_subdirs()
  52. break
  53. file_list_str = file_list_str.replace("\n", "\\n")
  54. os.system(
  55. 'printf "{}" | fzf > {}'.format(
  56. file_list_str, const.SSYNC_CHOSEN_FILE_NAMING
  57. )
  58. )
  59. chosen_song_file = read_chosen_song_file()
  60. if len(chosen_song_file) == 0:
  61. log("no slides chosen, skipping...")
  62. else:
  63. src_dir = os.path.join(rclone_local_dir, chosen_song_file)
  64. dest_dir = create_and_get_dest_dir(obs_slides_dir, index)
  65. dummy_slidegen_instance = slidegen.Slidegen(
  66. ClassicSongTemplate,
  67. ClassicStartSlide,
  68. ClassicSongSlide,
  69. src_dir,
  70. dest_dir,
  71. "",
  72. )
  73. parse_metadata(dummy_slidegen_instance)
  74. full_song_structure = dummy_slidegen_instance.metadata["structure"]
  75. log(
  76. "full song structure of '{}':\n{}".format(
  77. chosen_song_file,
  78. full_song_structure,
  79. ),
  80. color="magenta",
  81. )
  82. structure_prompt_answer = input(
  83. input_song_prompt + structure_prompt
  84. ).strip()
  85. calculated_prompt = generate_final_prompt(
  86. structure_prompt_answer, full_song_structure
  87. )
  88. log(
  89. "generating slides '{}' to '{}{}'...".format(
  90. chosen_song_file, const.OBS_SUBDIR_NAMING, index
  91. )
  92. )
  93. executing_slidegen_instance = slidegen.Slidegen(
  94. ClassicSongTemplate,
  95. ClassicStartSlide,
  96. ClassicSongSlide,
  97. src_dir,
  98. dest_dir,
  99. calculated_prompt,
  100. )
  101. executing_slidegen_instance.execute(ssync.disable_async)
  102. remove_chosenfile()
  103. def remove_chosenfile() -> None:
  104. try:
  105. if os.path.isfile(const.SSYNC_CHOSEN_FILE_NAMING):
  106. os.remove(const.SSYNC_CHOSEN_FILE_NAMING)
  107. except (FileNotFoundError, PermissionError, IOError) as error:
  108. error_msg("Failed to remove chosenfile. Reason: {}".format(error))
  109. def create_and_get_dest_dir(obs_slides_dir, index) -> str:
  110. dest_dir = os.path.join(
  111. obs_slides_dir,
  112. const.OBS_SUBDIR_NAMING + str(index),
  113. )
  114. os.mkdir(dest_dir)
  115. return dest_dir
  116. def read_chosen_song_file() -> str:
  117. with open(
  118. const.SSYNC_CHOSEN_FILE_NAMING, encoding="utf-8", mode="r"
  119. ) as tempfile_file_opener:
  120. chosen_song_file = tempfile_file_opener.read()[:-1].strip()
  121. return chosen_song_file