slide_selection_iterator.py 5.3 KB

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