strings.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 config as const
  15. from .log import error_msg
  16. def get_songtext_by_structure(content: list, structure: str) -> str:
  17. found_desired_structure: bool = False
  18. output_str: str = ""
  19. for line in content:
  20. stripped_line: str = line.strip()
  21. line_length: int = len(line)
  22. if line_length > const.STRUCTURE_ELEMENT_PER_LINE_CHAR_LIMIT:
  23. if line[-1] == "\n":
  24. line = line[:-1]
  25. error_msg(
  26. "line is configured to a character limit of "
  27. + str(const.STRUCTURE_ELEMENT_PER_LINE_CHAR_LIMIT)
  28. + " but has {} characters: \n{}".format(line_length, line)
  29. )
  30. if found_desired_structure:
  31. if stripped_line.startswith("[") and stripped_line.endswith("]"):
  32. break
  33. output_str += stripped_line + "\n"
  34. if (
  35. stripped_line.startswith("[")
  36. and stripped_line.endswith("]")
  37. and structure in stripped_line
  38. ):
  39. found_desired_structure: bool = True
  40. if not found_desired_structure:
  41. error_msg("could not find structure '{}'".format(structure))
  42. return output_str[:-1]
  43. def structure_as_list(structure: str) -> list:
  44. return structure.replace(" ", "").split(",")
  45. def get_unique_structure_elements(structure: list) -> list:
  46. return list(dict.fromkeys(structure))