Просмотр исходного кода

seperate gui and cmdline error messages

Noah Vogt 1 год назад
Родитель
Сommit
903eddac5f
9 измененных файлов с 160 добавлено и 48 удалено
  1. 24 8
      force_song.py
  2. 14 3
      input/parse_file.py
  3. 23 7
      input/validate_config.py
  4. 1 1
      next_song.py
  5. 1 1
      previous_song.py
  6. 1 1
      recording/cd.py
  7. 50 14
      set_cd_marker.py
  8. 26 6
      stop_cd_recording.py
  9. 20 7
      utils/path.py

+ 24 - 8
force_song.py

@@ -15,38 +15,54 @@
 # You should have received a copy of the GNU General Public License
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-from sys import argv
+import sys
 
 
 import colorama
 import colorama
+from PyQt5.QtWidgets import (  # pylint: disable=no-name-in-module
+    QApplication,
+    QMessageBox,
+)
 
 
 from utils import (
 from utils import (
-    error_msg,
     make_sure_file_exists,
     make_sure_file_exists,
 )
 )
 from song_switcher import switch_to_song
 from song_switcher import switch_to_song
-from input import validate_obs_song_scene_switcher_config
+from input import validate_obs_song_scene_switcher_config, InfoMsgBox
 import config as const
 import config as const
 
 
 
 
 # pylint: disable=inconsistent-return-statements
 # pylint: disable=inconsistent-return-statements
 def get_force_int() -> int:
 def get_force_int() -> int:
     try:
     try:
-        return int(argv[1])
+        return int(sys.argv[1])
     except IndexError:
     except IndexError:
-        error_msg("couldn't parse force song integer")
+        app = QApplication
+        InfoMsgBox(
+            QMessageBox.Critical,
+            "Error",
+            "couldn't parse force song integer",
+        )
+        del app
+        sys.exit(1)
 
 
 
 
 def exit_if_force_int_is_illegal():
 def exit_if_force_int_is_illegal():
     force_int = get_force_int()
     force_int = get_force_int()
+    msg = ""
     if force_int > const.OBS_MIN_SUBDIRS:
     if force_int > const.OBS_MIN_SUBDIRS:
-        error_msg("force integer too big")
+        msg = f"force integer {force_int} too big"
     if force_int < 1:
     if force_int < 1:
-        error_msg("force integer cannot be smaller than 1")
+        msg = f"force integer {force_int} cannot be smaller than 1"
+    if msg != "":
+        app = QApplication
+        InfoMsgBox(QMessageBox.Critical, "Error", msg)
+        del app
+        sys.exit(1)
 
 
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
     colorama.init()
     colorama.init()
     validate_obs_song_scene_switcher_config()
     validate_obs_song_scene_switcher_config()
-    make_sure_file_exists(const.NEXTSONG_CACHE_FILE)
+    make_sure_file_exists(const.NEXTSONG_CACHE_FILE, gui_error_out=True)
     exit_if_force_int_is_illegal()
     exit_if_force_int_is_illegal()
     switch_to_song(get_force_int())
     switch_to_song(get_force_int())

+ 14 - 3
input/parse_file.py

@@ -13,8 +13,14 @@
 # You should have received a copy of the GNU General Public License
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import sys
 from re import match
 from re import match
 
 
+from PyQt5.QtWidgets import (  # pylint: disable=no-name-in-module
+    QApplication,
+    QMessageBox,
+)
+
 from utils import (
 from utils import (
     error_msg,
     error_msg,
     structure_as_list,
     structure_as_list,
@@ -22,7 +28,7 @@ from utils import (
     get_songtext_by_structure,
     get_songtext_by_structure,
     expand_dir,
     expand_dir,
 )
 )
-
+from input import InfoMsgBox
 import config as const
 import config as const
 
 
 
 
@@ -97,9 +103,14 @@ def get_cachefile_content(cachefile: str) -> list:
         ) as cachefile_reader:
         ) as cachefile_reader:
             cachefile_content = cachefile_reader.readlines()
             cachefile_content = cachefile_reader.readlines()
     except (FileNotFoundError, PermissionError, IOError) as error:
     except (FileNotFoundError, PermissionError, IOError) as error:
-        error_msg(
+        app = QApplication
+        InfoMsgBox(
+            QMessageBox.Critical,
+            "Error",
             "Failed to access cachefile in '{}'. Reason: {}".format(
             "Failed to access cachefile in '{}'. Reason: {}".format(
                 expanded_path, error
                 expanded_path, error
-            )
+            ),
         )
         )
+        del app
+        sys.exit(1)
     return cachefile_content
     return cachefile_content

+ 23 - 7
input/validate_config.py

@@ -13,8 +13,15 @@
 # You should have received a copy of the GNU General Public License
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-from utils import log, error_msg
+import sys
+
+from PyQt5.QtWidgets import (  # pylint: disable=no-name-in-module
+    QApplication,
+    QMessageBox,
+)
 
 
+from utils import log, error_msg
+from input import InfoMsgBox
 import config as const
 import config as const
 
 
 
 
@@ -40,7 +47,7 @@ def validate_obs_song_scene_switcher_config() -> None:
         "OBS_TRANSITION_HOTKEY": const.OBS_TRANSITION_HOTKEY,
         "OBS_TRANSITION_HOTKEY": const.OBS_TRANSITION_HOTKEY,
         "OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX": const.OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX,
         "OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX": const.OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX,
     }
     }
-    general_config_validator(needed_constants)
+    general_config_validator(needed_constants, gui_error_out=True)
 
 
 
 
 def validate_cd_burn_config() -> None:
 def validate_cd_burn_config() -> None:
@@ -48,7 +55,7 @@ def validate_cd_burn_config() -> None:
         "CD_RECORD_CACHEFILE": const.CD_RECORD_CACHEFILE,
         "CD_RECORD_CACHEFILE": const.CD_RECORD_CACHEFILE,
         "CD_RECORD_OUTPUT_BASEDIR": const.CD_RECORD_OUTPUT_BASEDIR,
         "CD_RECORD_OUTPUT_BASEDIR": const.CD_RECORD_OUTPUT_BASEDIR,
     }
     }
-    general_config_validator(needed_constants)
+    general_config_validator(needed_constants, gui_error_out=True)
 
 
 
 
 def validate_cd_record_config() -> None:
 def validate_cd_record_config() -> None:
@@ -59,7 +66,7 @@ def validate_cd_record_config() -> None:
         "CD_RECORD_MAX_SECONDS": const.CD_RECORD_MAX_SECONDS,
         "CD_RECORD_MAX_SECONDS": const.CD_RECORD_MAX_SECONDS,
         "CD_RECORD_MIN_TRACK_MILIS": const.CD_RECORD_MIN_TRACK_MILIS,
         "CD_RECORD_MIN_TRACK_MILIS": const.CD_RECORD_MIN_TRACK_MILIS,
     }
     }
-    general_config_validator(needed_constants)
+    general_config_validator(needed_constants, gui_error_out=True)
 
 
 
 
 def validate_sermon_upload_config() -> None:
 def validate_sermon_upload_config() -> None:
@@ -72,11 +79,20 @@ def validate_sermon_upload_config() -> None:
         "SERMON_UPLOAD_FTP_UPLOAD_DIR": const.SERMON_UPLOAD_FTP_UPLOAD_DIR,
         "SERMON_UPLOAD_FTP_UPLOAD_DIR": const.SERMON_UPLOAD_FTP_UPLOAD_DIR,
         "SERMON_UPLOAD_SUITABLE_SEGMENT_FRAMES": const.SERMON_UPLOAD_SUITABLE_SEGMENT_FRAMES,
         "SERMON_UPLOAD_SUITABLE_SEGMENT_FRAMES": const.SERMON_UPLOAD_SUITABLE_SEGMENT_FRAMES,
     }
     }
-    general_config_validator(needed_constants)
+    general_config_validator(needed_constants, gui_error_out=True)
 
 
 
 
-def general_config_validator(needed_constants: dict) -> None:
+def general_config_validator(
+    needed_constants: dict, gui_error_out=False
+) -> None:
     for key in needed_constants:
     for key in needed_constants:
         if needed_constants.get(key) == "":
         if needed_constants.get(key) == "":
-            error_msg("needed config entry '{}' is empty".format(key))
+            msg = "needed config entry '{}' is empty".format(key)
+            if not gui_error_out:
+                error_msg(msg)
+            app = QApplication
+            InfoMsgBox(QMessageBox.Critical, "Error", msg)
+            del app
+            sys.exit(1)
+
     log("configuration initialised")
     log("configuration initialised")

+ 1 - 1
next_song.py

@@ -28,5 +28,5 @@ import config as const
 if __name__ == "__main__":
 if __name__ == "__main__":
     colorama.init()
     colorama.init()
     validate_obs_song_scene_switcher_config()
     validate_obs_song_scene_switcher_config()
-    make_sure_file_exists(const.NEXTSONG_CACHE_FILE)
+    make_sure_file_exists(const.NEXTSONG_CACHE_FILE, gui_error_out=True)
     cycle_to_song_direction(SongDirection.NEXT)
     cycle_to_song_direction(SongDirection.NEXT)

+ 1 - 1
previous_song.py

@@ -28,5 +28,5 @@ import config as const
 if __name__ == "__main__":
 if __name__ == "__main__":
     colorama.init()
     colorama.init()
     validate_obs_song_scene_switcher_config()
     validate_obs_song_scene_switcher_config()
-    make_sure_file_exists(const.NEXTSONG_CACHE_FILE)
+    make_sure_file_exists(const.NEXTSONG_CACHE_FILE, gui_error_out=True)
     cycle_to_song_direction(SongDirection.PREVIOUS)
     cycle_to_song_direction(SongDirection.PREVIOUS)

+ 1 - 1
recording/cd.py

@@ -149,7 +149,7 @@ def burn_and_eject_cd(
 
 
 def burn_cds_of_day(yyyy_mm_dd: str) -> None:
 def burn_cds_of_day(yyyy_mm_dd: str) -> None:
     validate_cd_burn_config()
     validate_cd_burn_config()
-    make_sure_file_exists(const.CD_RECORD_CACHEFILE)
+    make_sure_file_exists(const.CD_RECORD_CACHEFILE, gui_error_out=True)
 
 
     try:
     try:
         target_dir = path.join(
         target_dir = path.join(

+ 50 - 14
set_cd_marker.py

@@ -15,12 +15,17 @@
 # You should have received a copy of the GNU General Public License
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import sys
 from os import path, mkdir, listdir
 from os import path, mkdir, listdir
 from shlex import split
 from shlex import split
 from subprocess import Popen
 from subprocess import Popen
 from re import match
 from re import match
 
 
 import colorama
 import colorama
+from PyQt5.QtWidgets import (  # pylint: disable=no-name-in-module
+    QApplication,
+    QMessageBox,
+)
 
 
 from utils import (
 from utils import (
     get_yyyy_mm_dd_date,
     get_yyyy_mm_dd_date,
@@ -28,10 +33,9 @@ from utils import (
     get_unix_milis,
     get_unix_milis,
     log,
     log,
     warn,
     warn,
-    error_msg,
     expand_dir,
     expand_dir,
 )
 )
-from input import get_cachefile_content, validate_cd_record_config
+from input import get_cachefile_content, validate_cd_record_config, InfoMsgBox
 import config as const
 import config as const
 from recording import is_valid_cd_record_checkfile, mark_end_of_recording
 from recording import is_valid_cd_record_checkfile, mark_end_of_recording
 
 
@@ -86,11 +90,16 @@ def start_cd_recording() -> None:
                 file_writer.write(f"{unix_milis}\n")
                 file_writer.write(f"{unix_milis}\n")
                 file_writer.write(f"{cd_num}\n")
                 file_writer.write(f"{cd_num}\n")
         except (FileNotFoundError, PermissionError, IOError) as error:
         except (FileNotFoundError, PermissionError, IOError) as error:
-            error_msg(
+            app = QApplication
+            InfoMsgBox(
+                QMessageBox.Critical,
+                "Error",
                 "Failed to write to cachefile '{}'. Reason: {}".format(
                 "Failed to write to cachefile '{}'. Reason: {}".format(
                     cachefile, error
                     cachefile, error
-                )
+                ),
             )
             )
+            del app
+            sys.exit(1)
         fresh_cachefile_content = get_cachefile_content(
         fresh_cachefile_content = get_cachefile_content(
             const.CD_RECORD_CACHEFILE
             const.CD_RECORD_CACHEFILE
         )
         )
@@ -103,7 +112,14 @@ def start_cd_recording() -> None:
         cd_num += 1
         cd_num += 1
         if process.returncode not in [255, 0]:
         if process.returncode not in [255, 0]:
             mark_end_of_recording(cachefile_content)
             mark_end_of_recording(cachefile_content)
-            error_msg(f"ffmpeg terminated with exit code {process.returncode}")
+            app = QApplication
+            InfoMsgBox(
+                QMessageBox.Critical,
+                "Error",
+                f"ffmpeg terminated with exit code {process.returncode}",
+            )
+            del app
+            sys.exit(1)
 
 
 
 
 def ensure_output_dir_exists(date):
 def ensure_output_dir_exists(date):
@@ -112,11 +128,16 @@ def ensure_output_dir_exists(date):
         if not path.exists(cue_sheet_dir):
         if not path.exists(cue_sheet_dir):
             mkdir(cue_sheet_dir)
             mkdir(cue_sheet_dir)
     except (FileNotFoundError, PermissionError, IOError) as error:
     except (FileNotFoundError, PermissionError, IOError) as error:
-        error_msg(
+        app = QApplication
+        InfoMsgBox(
+            QMessageBox.Critical,
+            "Error",
             "Failed to create to cue sheet directory '{}'. Reason: {}".format(
             "Failed to create to cue sheet directory '{}'. Reason: {}".format(
                 cue_sheet_dir, error
                 cue_sheet_dir, error
-            )
+            ),
         )
         )
+        del app
+        sys.exit(1)
 
 
 
 
 def create_cachefile_for_marker(
 def create_cachefile_for_marker(
@@ -157,11 +178,16 @@ def create_cachefile_for_marker(
             else:
             else:
                 file_writer.write(f"{cachefile_content[5].strip()}\n")
                 file_writer.write(f"{cachefile_content[5].strip()}\n")
     except (FileNotFoundError, PermissionError, IOError) as error:
     except (FileNotFoundError, PermissionError, IOError) as error:
-        error_msg(
+        app = QApplication
+        InfoMsgBox(
+            QMessageBox.Critical,
+            "Error",
             "Failed to write to cachefile '{}'. Reason: {}".format(
             "Failed to write to cachefile '{}'. Reason: {}".format(
                 cachefile, error
                 cachefile, error
-            )
+            ),
         )
         )
+        del app
+        sys.exit(1)
 
 
 
 
 def update_cue_sheet(
 def update_cue_sheet(
@@ -189,11 +215,16 @@ def update_cue_sheet(
                 file_writer.write("  TRACK 01 AUDIO\n")
                 file_writer.write("  TRACK 01 AUDIO\n")
                 file_writer.write("    INDEX 01 00:00:00\n")
                 file_writer.write("    INDEX 01 00:00:00\n")
         except (FileNotFoundError, PermissionError, IOError) as error:
         except (FileNotFoundError, PermissionError, IOError) as error:
-            error_msg(
+            app = QApplication
+            InfoMsgBox(
+                QMessageBox.Critical,
+                "Error",
                 "Failed to write to cue sheet file '{}'. Reason: {}".format(
                 "Failed to write to cue sheet file '{}'. Reason: {}".format(
                     cue_sheet_path, error
                     cue_sheet_path, error
-                )
+                ),
             )
             )
+            del app
+            sys.exit(1)
     else:
     else:
         marker = int(cachefile_content[1]) + 1
         marker = int(cachefile_content[1]) + 1
         if marker > 99:
         if marker > 99:
@@ -244,11 +275,16 @@ def update_cue_sheet(
                     )
                     )
                 )
                 )
         except (FileNotFoundError, PermissionError, IOError) as error:
         except (FileNotFoundError, PermissionError, IOError) as error:
-            error_msg(
+            app = QApplication
+            InfoMsgBox(
+                QMessageBox.Critical,
+                "Error",
                 "Failed to write to cue sheet file '{}'. Reason: {}".format(
                 "Failed to write to cue sheet file '{}'. Reason: {}".format(
                     cue_sheet_path, error
                     cue_sheet_path, error
-                )
+                ),
             )
             )
+            del app
+            sys.exit(1)
 
 
 
 
 def set_cd_marker() -> None:
 def set_cd_marker() -> None:
@@ -269,5 +305,5 @@ def set_cd_marker() -> None:
 if __name__ == "__main__":
 if __name__ == "__main__":
     colorama.init()
     colorama.init()
     validate_cd_record_config()
     validate_cd_record_config()
-    make_sure_file_exists(const.CD_RECORD_CACHEFILE)
+    make_sure_file_exists(const.CD_RECORD_CACHEFILE, gui_error_out=True)
     set_cd_marker()
     set_cd_marker()

+ 26 - 6
stop_cd_recording.py

@@ -15,26 +15,32 @@
 # You should have received a copy of the GNU General Public License
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import sys
 from os import kill
 from os import kill
 from signal import SIGTERM
 from signal import SIGTERM
 from time import sleep
 from time import sleep
 
 
 import colorama
 import colorama
+from PyQt5.QtWidgets import (  # pylint: disable=no-name-in-module
+    QApplication,
+    QMessageBox,
+)
 
 
 from utils import (
 from utils import (
     get_yyyy_mm_dd_date,
     get_yyyy_mm_dd_date,
     make_sure_file_exists,
     make_sure_file_exists,
-    error_msg,
     get_unix_milis,
     get_unix_milis,
     warn,
     warn,
+    expand_dir,
 )
 )
-from input import get_cachefile_content, validate_cd_record_config
+from input import get_cachefile_content, validate_cd_record_config, InfoMsgBox
 import config as const
 import config as const
 from recording import is_valid_cd_record_checkfile, mark_end_of_recording
 from recording import is_valid_cd_record_checkfile, mark_end_of_recording
 
 
 
 
 def stop_cd_recording() -> None:
 def stop_cd_recording() -> None:
-    cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
+    filename = expand_dir(const.CD_RECORD_CACHEFILE)
+    cachefile_content = get_cachefile_content(filename)
     yyyy_mm_dd = get_yyyy_mm_dd_date()
     yyyy_mm_dd = get_yyyy_mm_dd_date()
 
 
     if is_valid_cd_record_checkfile(cachefile_content, yyyy_mm_dd):
     if is_valid_cd_record_checkfile(cachefile_content, yyyy_mm_dd):
@@ -51,14 +57,28 @@ def stop_cd_recording() -> None:
         try:
         try:
             kill(int(cachefile_content[2]), SIGTERM)
             kill(int(cachefile_content[2]), SIGTERM)
         except ProcessLookupError:
         except ProcessLookupError:
-            error_msg("Recording not running, cannot be stopped.")
+            app = QApplication
+            InfoMsgBox(
+                QMessageBox.Critical,
+                "Error",
+                "Recording not running, cannot be stopped.",
+            )
+            del app
+            sys.exit(1)
         mark_end_of_recording(cachefile_content)
         mark_end_of_recording(cachefile_content)
     else:
     else:
-        error_msg("CD Record Checkfile is invalid.")
+        app = QApplication
+        InfoMsgBox(
+            QMessageBox.Critical,
+            "Error",
+            f"CD Record Checkfile {filename} is invalid.",
+        )
+        del app
+        sys.exit(1)
 
 
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
     colorama.init()
     colorama.init()
     validate_cd_record_config()
     validate_cd_record_config()
-    make_sure_file_exists(const.CD_RECORD_CACHEFILE)
+    make_sure_file_exists(const.CD_RECORD_CACHEFILE, gui_error_out=True)
     stop_cd_recording()
     stop_cd_recording()

+ 20 - 7
utils/path.py

@@ -13,7 +13,15 @@
 # You should have received a copy of the GNU General Public License
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import sys
 from os import path
 from os import path
+
+from PyQt5.QtWidgets import (  # pylint: disable=no-name-in-module
+    QApplication,
+    QMessageBox,
+)
+
+from input import InfoMsgBox
 from .log import error_msg
 from .log import error_msg
 
 
 
 
@@ -24,14 +32,19 @@ def expand_dir(directory: str) -> str:
     return abs_path
     return abs_path
 
 
 
 
-def make_sure_file_exists(filename: str) -> None:
-    if not path.isfile(filename):
+def make_sure_file_exists(filename: str, gui_error_out=False) -> None:
+    expanded = expand_dir(filename)
+    if not path.isfile(expanded):
         try:
         try:
-            with open(filename, mode="w+", encoding="utf-8") as file_creator:
+            with open(expanded, mode="w+", encoding="utf-8") as file_creator:
                 file_creator.write("")
                 file_creator.write("")
         except (FileNotFoundError, PermissionError, IOError) as error:
         except (FileNotFoundError, PermissionError, IOError) as error:
-            error_msg(
-                "Failed to create file in '{}'. Reason: {}".format(
-                    filename, error
-                )
+            msg = "Failed to create file in '{}'. Reason: {}".format(
+                expanded, error
             )
             )
+            if not gui_error_out:
+                error_msg(msg)
+            app = QApplication
+            InfoMsgBox(QMessageBox.Critical, "Error", msg)
+            del app
+            sys.exit(1)