strings.py 2.0 KB

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