stop_cd_recording.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. # Copyright © 2024 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. import sys
  14. from time import sleep
  15. import colorama
  16. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  17. QApplication,
  18. QMessageBox,
  19. )
  20. from utils import (
  21. get_yyyy_mm_dd_date,
  22. make_sure_file_exists,
  23. get_unix_milis,
  24. warn,
  25. expand_dir,
  26. InfoMsgBox,
  27. )
  28. from input import get_cachefile_content, validate_cd_record_config
  29. import config as const
  30. from recording import is_valid_cd_record_checkfile, mark_end_of_recording
  31. from os_agnostic import kill_process
  32. def stop_cd_recording() -> None:
  33. filename = expand_dir(const.CD_RECORD_CACHEFILE)
  34. cachefile_content = get_cachefile_content(filename)
  35. yyyy_mm_dd = get_yyyy_mm_dd_date()
  36. if is_valid_cd_record_checkfile(cachefile_content, yyyy_mm_dd):
  37. unix_milis = get_unix_milis()
  38. last_track_milis = int(cachefile_content[4])
  39. milis_diff = unix_milis - last_track_milis
  40. if milis_diff < const.CD_RECORD_MIN_TRACK_MILIS:
  41. warn(
  42. f"Minimum track length of {const.CD_RECORD_MIN_TRACK_MILIS}"
  43. + "ms not satisfied, sleeping until reached..."
  44. )
  45. sleep(milis_diff / 1000 + 1)
  46. try:
  47. kill_process(int(cachefile_content[2]))
  48. except ProcessLookupError:
  49. app = QApplication
  50. InfoMsgBox(
  51. QMessageBox.Critical,
  52. "Error",
  53. "Recording not running, cannot be stopped.",
  54. )
  55. del app
  56. sys.exit(1)
  57. mark_end_of_recording(cachefile_content)
  58. else:
  59. if cachefile_content[1].strip() == "9001":
  60. app = QApplication
  61. InfoMsgBox(
  62. QMessageBox.Warning,
  63. "Warning",
  64. "CD Recording stopped already.",
  65. )
  66. del app
  67. sys.exit(0)
  68. app = QApplication
  69. InfoMsgBox(
  70. QMessageBox.Critical,
  71. "Error",
  72. f"CD Record Checkfile {filename} is invalid.",
  73. )
  74. del app
  75. sys.exit(1)
  76. if __name__ == "__main__":
  77. colorama.init()
  78. validate_cd_record_config()
  79. make_sure_file_exists(const.CD_RECORD_CACHEFILE, gui_error_out=True)
  80. stop_cd_recording()