strings.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. import config as const
  13. from .log import error_msg
  14. def get_songtext_by_structure(content: list, structure: str) -> str:
  15. found_desired_structure: bool = False
  16. output_str: str = ""
  17. for line in content:
  18. stripped_line: str = line.strip()
  19. line_length: int = len(line)
  20. if line_length > const.STRUCTURE_ELEMENT_PER_LINE_CHAR_LIMIT:
  21. if line[-1] == "\n":
  22. line = line[:-1]
  23. error_msg(
  24. "line is configured to a character limit of "
  25. + str(const.STRUCTURE_ELEMENT_PER_LINE_CHAR_LIMIT)
  26. + " but has {} characters: \n{}".format(line_length, line)
  27. )
  28. if found_desired_structure:
  29. if stripped_line.startswith("[") and stripped_line.endswith("]"):
  30. break
  31. output_str += stripped_line + "\n"
  32. if (
  33. stripped_line.startswith("[")
  34. and stripped_line.endswith("]")
  35. and structure in stripped_line
  36. ):
  37. found_desired_structure: bool = True
  38. if not found_desired_structure:
  39. error_msg("could not find structure '{}'".format(structure))
  40. return output_str[:-1]
  41. def structure_as_list(structure: str) -> list:
  42. return structure.replace(" ", "").split(",")
  43. def get_unique_structure_elements(structure: list) -> list:
  44. return list(dict.fromkeys(structure))