# Copyright © 2024 Noah Vogt # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . import sys from os import path, listdir from time import sleep from re import match from enum import Enum from dataclasses import dataclass from pyautogui import keyDown, keyUp from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module QApplication, QMessageBox, ) from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module QDialog, ) from utils import ( log, error_msg, get_yyyy_mm_dd_date, expand_dir, ) from input import RadioButtonDialog, get_cachefile_content, InfoMsgBox import config as const def make_sure_file_exists(cachefile: str) -> None: if not path.isfile(cachefile): try: with open( cachefile, mode="w+", encoding="utf-8-sig" ) as file_creator: file_creator.write("") except (FileNotFoundError, PermissionError, IOError) as error: error_msg( "Failed to create file in '{}'. Reason: {}".format( cachefile, error ) ) class SongDirection(Enum): PREVIOUS = "previous" NEXT = "next" def cycle_to_song_direction(song_direction: SongDirection): cachefile_content = get_cachefile_content(const.NEXTSONG_CACHE_FILE) if song_direction == SongDirection.PREVIOUS: step = -1 else: step = 1 if ( not ( len(cachefile_content) == 2 and match(r"[0-9]{4}-[0-9]{2}-[0-9]{2}$", cachefile_content[0]) and match(r"^[0-9]+$", cachefile_content[1]) ) or cachefile_content[0].strip() != get_yyyy_mm_dd_date() ): switch_to_song(1) else: switch_to_song(int(cachefile_content[1]) + step) def switch_to_song(song_number: int) -> None: if song_number > const.OBS_MIN_SUBDIRS: song_number = 1 if song_number < 1: song_number = const.OBS_MIN_SUBDIRS log("sending hotkey to switch to scene {}".format(song_number), "cyan") scene_switch_hotkey = list(const.OBS_SWITCH_TO_SCENE_HOTKEY_PREFIX) scene_switch_hotkey.append("f{}".format(song_number)) safe_send_hotkey(scene_switch_hotkey) log("sending hotkey to transition to scene {}".format(song_number), "cyan") safe_send_hotkey(const.OBS_TRANSITION_HOTKEY) create_cachfile_for_song(song_number) def create_cachfile_for_song(song) -> None: log("writing song {} to cachefile...".format(song)) cachefile = expand_dir(const.NEXTSONG_CACHE_FILE) try: with open(cachefile, mode="w", encoding="utf-8-sig") as file_writer: file_writer.write(get_yyyy_mm_dd_date() + "\n") file_writer.write(str(song) + "\n") except (FileNotFoundError, PermissionError, IOError) as error: app = QApplication InfoMsgBox( QMessageBox.Critical, "Error", "Failed to write to cachefile '{}'. Reason: {}".format( cachefile, error ), ) del app sys.exit(1) def safe_send_hotkey(hotkey: list, sleep_time=0.1) -> None: for key in hotkey: keyDown(key) sleep(sleep_time) for key in hotkey: keyUp(key)