parse_prompt.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. from utils import (
  15. log,
  16. error_msg,
  17. structure_as_list,
  18. )
  19. def parse_prompt_input(slidegen) -> list:
  20. full_structure_list = structure_as_list(slidegen.metadata["structure"])
  21. if len(slidegen.chosen_structure) == 0:
  22. log("chosen structure: {}".format(str(slidegen.chosen_structure)))
  23. return structure_as_list(slidegen.metadata["structure"])
  24. if not "-" in slidegen.chosen_structure:
  25. log("chosen structure: {}".format(str(slidegen.chosen_structure)))
  26. return structure_as_list(str(slidegen.chosen_structure))
  27. dash_index = str(slidegen.chosen_structure).find("-")
  28. start_verse = str(slidegen.chosen_structure[:dash_index]).strip()
  29. end_verse = str(slidegen.chosen_structure[dash_index + 1 :]).strip()
  30. try:
  31. if int(start_verse) >= int(end_verse):
  32. error_msg("{} < {} must be true".format(start_verse, end_verse))
  33. if start_verse not in slidegen.metadata["structure"]:
  34. error_msg("structure {} unknown".format(start_verse))
  35. if end_verse not in slidegen.metadata["structure"]:
  36. error_msg("structure {} unknown".format(end_verse))
  37. except (ValueError, IndexError):
  38. error_msg("please choose a valid integer for the song structure")
  39. start_index = full_structure_list.index(start_verse)
  40. if start_index != 0:
  41. if (
  42. full_structure_list[0] == "R"
  43. and full_structure_list[start_index - 1] == "R"
  44. ):
  45. start_index -= 1
  46. end_index = full_structure_list.index(end_verse)
  47. if end_index != len(full_structure_list) - 1:
  48. if (
  49. full_structure_list[-1] == "R"
  50. and full_structure_list[end_index + 1] == "R"
  51. ):
  52. end_index += 1
  53. log("chosen structure: {}".format(str(slidegen.chosen_structure)))
  54. return full_structure_list[start_index : end_index + 1]