stop_cd_recording.py 2.9 KB

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