slide_selection_iterator.py 4.8 KB

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