parse_file.py 3.7 KB

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