From 263c2b9189fe11ba5b30b984b4d459a2ff7149da Mon Sep 17 00:00:00 2001 From: arch1t3cht Date: Sat, 29 Jul 2023 19:54:09 +0200 Subject: [PATCH] vapoursynth: Make askyesno method configurable In particular, don't always import tkinter.messagebox since not all Python distributions include tkinter. --- automation/vapoursynth/aegisub_vs.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/automation/vapoursynth/aegisub_vs.py b/automation/vapoursynth/aegisub_vs.py index 6ccd7b9b9..412ac1e5d 100644 --- a/automation/vapoursynth/aegisub_vs.py +++ b/automation/vapoursynth/aegisub_vs.py @@ -29,9 +29,8 @@ import os import os.path import re from enum import Enum -from tkinter.messagebox import askyesno from collections import deque -from typing import Any, Dict, List, Tuple +from typing import Any, Dict, List, Tuple, Callable import vapoursynth as vs core = vs.core @@ -224,8 +223,15 @@ class GenKeyframesMode(Enum): ASK = 2 +def ask_gen_keyframes(_: str) -> bool: + from tkinter.messagebox import askyesno + return askyesno("Generate Keyframes", \ + "No keyframes file was found for this video file.\nShould Aegisub detect keyframes from the video?\nThis will take a while.", default="no") + + def get_keyframes(filename: str, clip: vs.VideoNode, fallback: str | List[int], - generate: GenKeyframesMode = GenKeyframesMode.ASK, **kwargs: Any) -> str | List[int]: + generate: GenKeyframesMode = GenKeyframesMode.ASK, + ask_callback: Callable = ask_gen_keyframes, **kwargs: Any) -> str | List[int]: """ Looks for a keyframes file for the given filename. If no file was found, this function can generate a keyframe file for the given clip next @@ -244,8 +250,7 @@ def get_keyframes(filename: str, clip: vs.VideoNode, fallback: str | List[int], if not os.path.exists(kffilename): if generate == GenKeyframesMode.NEVER: return fallback - if generate == GenKeyframesMode.ASK and not askyesno("Generate Keyframes", \ - "No keyframes file was found for this video file.\nShould Aegisub detect keyframes from the video?\nThis will take a while.", default="no"): + if generate == GenKeyframesMode.ASK and not ask_callback(filename): return fallback vs.core.log_message(vs.MESSAGE_TYPE_INFORMATION, "No keyframes file found, detecting keyframes...\n")