parse_file.py 3.5 KB

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