verify.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 os import path
  14. from re import match
  15. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  16. QMessageBox,
  17. )
  18. import config as const
  19. from utils import get_yyyy_mm_dd_date
  20. from input import InfoMsgBox, get_cachefile_content
  21. def is_valid_cd_record_checkfile(
  22. cachefile_content: list, yyyy_mm_dd: str
  23. ) -> bool:
  24. return (
  25. len(cachefile_content) == 6
  26. # YYYY-MM-DD
  27. and bool(match(r"[0-9]{4}-[0-9]{2}-[0-9]{2}$", cachefile_content[0]))
  28. # last set marker
  29. and bool(match(r"^[0-9][0-9]?$", cachefile_content[1]))
  30. # pid of ffmpeg recording instance
  31. and bool(match(r"^[0-9]+$", cachefile_content[2]))
  32. # unix milis @ recording start
  33. and bool(match(r"^[0-9]+$", cachefile_content[3]))
  34. # unix milis @ last track
  35. and bool(match(r"^[0-9]+$", cachefile_content[4]))
  36. # cd number
  37. and bool(match(r"^[0-9]+$", cachefile_content[5]))
  38. # date matches today
  39. and cachefile_content[0].strip() == yyyy_mm_dd
  40. )
  41. def make_sure_there_is_no_ongoing_cd_recording() -> None:
  42. if path.isfile(const.CD_RECORD_CACHEFILE):
  43. cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
  44. if is_valid_cd_record_checkfile(
  45. cachefile_content, get_yyyy_mm_dd_date()
  46. ):
  47. if cachefile_content[1].strip() != "9001":
  48. InfoMsgBox(
  49. QMessageBox.Critical,
  50. "Error",
  51. "Error: Ongoing CD Recording detected",
  52. )
  53. sys.exit(1)
  54. def is_legal_sheet_filename(filename: str) -> bool:
  55. return bool(match(r"^sheet-[0-9]+\.cue", filename)) and len(filename) == 17
  56. def get_padded_cd_num_from_sheet_filename(filename: str) -> str:
  57. if not is_legal_sheet_filename(filename):
  58. InfoMsgBox(
  59. QMessageBox.Critical,
  60. "Error",
  61. f"Error: filename '{filename}' in illegal format",
  62. )
  63. sys.exit(1)
  64. return filename[6:13]