parse_file.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 re import match
  15. from utils import (
  16. error_msg,
  17. structure_as_list,
  18. get_unique_structure_elements,
  19. get_songtext_by_structure,
  20. )
  21. import config as const
  22. def parse_metadata(slidegen) -> None:
  23. metadata_dict = dict.fromkeys(const.METADATA_STRINGS)
  24. try:
  25. with open(
  26. slidegen.song_file_path, mode="r", encoding="utf-8-sig"
  27. ) as opener:
  28. content = opener.readlines()
  29. except IOError:
  30. error_msg(
  31. "could not read the the song input file: '{}'".format(
  32. slidegen.song_file_path
  33. )
  34. )
  35. valid_metadata_strings = list(const.METADATA_STRINGS)
  36. for line_nr, line in enumerate(content):
  37. if len(valid_metadata_strings) == 0:
  38. content = content[line_nr:]
  39. break
  40. if not match(
  41. r"^(?!structure)\S+: .+|^structure: ([0-9]+|R)(,([0-9]+|R))*$",
  42. line,
  43. ):
  44. if line[-1] == "\n":
  45. line = line[:-1]
  46. missing_metadata_strs = ""
  47. for metadata_str in valid_metadata_strings:
  48. missing_metadata_strs += ", " + metadata_str
  49. missing_metadata_strs = missing_metadata_strs[2:]
  50. error_msg(
  51. "invalid metadata syntax on line {}:\n{}\nThe ".format(
  52. line_nr + 1, line
  53. )
  54. + "following metadata strings are still missing: {}".format(
  55. missing_metadata_strs
  56. )
  57. )
  58. metadata_str = line[: line.index(":")]
  59. if metadata_str in valid_metadata_strings:
  60. metadata_dict[metadata_str] = line[line.index(": ") + 2 : -1]
  61. valid_metadata_strings.remove(metadata_str)
  62. continue
  63. error_msg("invalid metadata string '{}'".format(metadata_str))
  64. slidegen.metadata = metadata_dict
  65. slidegen.song_file_content = content
  66. def parse_songtext(slidegen) -> None:
  67. unique_structures = get_unique_structure_elements(
  68. structure_as_list(slidegen.metadata["structure"])
  69. )
  70. output_dict = dict.fromkeys(unique_structures)
  71. for structure in unique_structures:
  72. output_dict[structure] = get_songtext_by_structure(
  73. slidegen.song_file_content, structure
  74. )
  75. slidegen.songtext = output_dict
  76. def get_songchooser_cachefile_content() -> list:
  77. try:
  78. with open(
  79. const.NEXTSONG_CACHE_FILE, mode="r", encoding="utf-8-sig"
  80. ) as cachefile_reader:
  81. cachefile_content = cachefile_reader.readlines()
  82. except (FileNotFoundError, PermissionError, IOError) as error:
  83. error_msg(
  84. "Failed to access cachefile in '{}'. Reason: {}".format(
  85. const.NEXTSONG_CACHE_FILE, error
  86. )
  87. )
  88. return cachefile_content