slide_selection_iterator.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. const.SSYNC_CHOSEN_FILE_NAMING,
  48. )
  49. )
  50. chosen_song_file = read_chosen_song_file()
  51. if len(chosen_song_file) == 0:
  52. log("no slides chosen, skipping...")
  53. else:
  54. src_dir = os.path.join(rclone_local_dir, chosen_song_file)
  55. dest_dir = create_and_get_dest_dir(
  56. expand_dir(const.OBS_SLIDES_DIR), song_counter
  57. )
  58. full_song_structure = get_structure_for_prompt(
  59. slide_style, src_dir, dest_dir
  60. )
  61. log(
  62. "full song structure of '{}':\n{}".format(
  63. chosen_song_file,
  64. full_song_structure,
  65. ),
  66. color="magenta",
  67. )
  68. structure_prompt_answer = input(
  69. input_prompt_prefix + structure_prompt
  70. ).strip()
  71. log(
  72. "generating slides '{}' to '{}{}'...".format(
  73. chosen_song_file, const.OBS_SUBDIR_NAMING, song_counter
  74. )
  75. )
  76. generate_slides_for_selected_song(
  77. slide_style,
  78. src_dir,
  79. dest_dir,
  80. generate_final_prompt(
  81. structure_prompt_answer, full_song_structure
  82. ),
  83. disable_async_enabled,
  84. )
  85. remove_chosenfile()
  86. def generate_slides_for_selected_song(
  87. classic_slide_style: SlideStyle,
  88. src_dir: str,
  89. dest_dir: str,
  90. calculated_prompt: str | list[str],
  91. disable_async_enabled: bool,
  92. ) -> None:
  93. executing_slidegen_instance = slidegen.Slidegen(
  94. classic_slide_style,
  95. src_dir,
  96. dest_dir,
  97. calculated_prompt,
  98. )
  99. executing_slidegen_instance.execute(disable_async_enabled)
  100. def get_structure_for_prompt(classic_slide_style, src_dir, dest_dir):
  101. dummy_slidegen_instance = slidegen.Slidegen(
  102. classic_slide_style,
  103. src_dir,
  104. dest_dir,
  105. "",
  106. )
  107. parse_metadata(dummy_slidegen_instance)
  108. full_song_structure = dummy_slidegen_instance.metadata["structure"]
  109. return full_song_structure
  110. def get_file_list_inside(rclone_local_dir):
  111. file_list_str = ""
  112. try:
  113. for file in os.listdir(rclone_local_dir):
  114. file_list_str += file + "\n"
  115. except (FileNotFoundError, PermissionError, IOError) as error:
  116. error_msg(
  117. "Failed to access items in '{}'. Reason: {}".format(
  118. rclone_local_dir, error
  119. )
  120. )
  121. file_list_str = file_list_str[:-1]
  122. file_list_str = file_list_str.replace("\n", "\\n")
  123. return file_list_str
  124. def remove_chosenfile() -> None:
  125. try:
  126. if os.path.isfile(const.SSYNC_CHOSEN_FILE_NAMING):
  127. os.remove(const.SSYNC_CHOSEN_FILE_NAMING)
  128. except (FileNotFoundError, PermissionError, IOError) as error:
  129. error_msg("Failed to remove chosenfile. Reason: {}".format(error))
  130. def create_and_get_dest_dir(obs_slides_dir, index) -> str:
  131. dest_dir = os.path.join(
  132. obs_slides_dir,
  133. const.OBS_SUBDIR_NAMING + str(index),
  134. )
  135. os.mkdir(dest_dir)
  136. return dest_dir
  137. def read_chosen_song_file() -> str:
  138. with open(
  139. const.SSYNC_CHOSEN_FILE_NAMING, encoding="utf-8", mode="r"
  140. ) as tempfile_file_opener:
  141. chosen_song_file = tempfile_file_opener.read()[:-1].strip()
  142. return chosen_song_file