parse_file.py 3.4 KB

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