parse_file.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(slidegen.song_file_path, mode="r", encoding="utf8") as opener:
  26. content = opener.readlines()
  27. except IOError:
  28. error_msg(
  29. "could not read the the song input file: '{}'".format(
  30. slidegen.song_file_path
  31. )
  32. )
  33. valid_metadata_strings = list(const.METADATA_STRINGS)
  34. for line_nr, line in enumerate(content):
  35. if len(valid_metadata_strings) == 0:
  36. content = content[line_nr:]
  37. break
  38. if not match(
  39. r"^(?!structure)\S+: .+|^structure: ([0-9]+|R)(,([0-9]+|R))*$",
  40. line,
  41. ):
  42. if line[-1] == "\n":
  43. line = line[:-1]
  44. missing_metadata_strs = ""
  45. for metadata_str in valid_metadata_strings:
  46. missing_metadata_strs += ", " + metadata_str
  47. missing_metadata_strs = missing_metadata_strs[2:]
  48. error_msg(
  49. "invalid metadata syntax on line {}:\n{}\nThe ".format(
  50. line_nr + 1, line
  51. )
  52. + "following metadata strings are still missing: {}".format(
  53. missing_metadata_strs
  54. )
  55. )
  56. metadata_str = line[: line.index(":")]
  57. if metadata_str in valid_metadata_strings:
  58. metadata_dict[metadata_str] = line[line.index(": ") + 2 : -1]
  59. valid_metadata_strings.remove(metadata_str)
  60. continue
  61. error_msg("invalid metadata string '{}'".format(metadata_str))
  62. slidegen.metadata = metadata_dict
  63. slidegen.song_file_content = content
  64. def parse_songtext(slidegen) -> None:
  65. unique_structures = get_unique_structure_elements(
  66. structure_as_list(slidegen.metadata["structure"])
  67. )
  68. output_dict = dict.fromkeys(unique_structures)
  69. for structure in unique_structures:
  70. output_dict[structure] = get_songtext_by_structure(
  71. slidegen.song_file_content, structure
  72. )
  73. slidegen.songtext = output_dict