From c2901e4859159d5dfc196fe74b293d1a2c6a1a70 Mon Sep 17 00:00:00 2001 From: ianki Date: Mon, 9 Nov 2020 01:45:14 -0800 Subject: [PATCH] Add hooks for filtering media. --- CONTRIBUTORS | 1 + pylib/anki/exporting.py | 1 + pylib/tools/genhooks.py | 6 ++++++ qt/aqt/main.py | 1 + qt/aqt/mediasrv.py | 3 +++ qt/aqt/sound.py | 10 ++++++++-- qt/tools/genhooks_gui.py | 1 + 7 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index af1e46b4d..a581234e5 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -68,6 +68,7 @@ Piotr Kubowicz RumovZ Cecini Krish Shah +ianki ******************** diff --git a/pylib/anki/exporting.py b/pylib/anki/exporting.py index cc7b6294a..fd0826d3e 100644 --- a/pylib/anki/exporting.py +++ b/pylib/anki/exporting.py @@ -355,6 +355,7 @@ class AnkiPackageExporter(AnkiExporter): media = {} for c, file in enumerate(files): cStr = str(c) + file = hooks.media_file_filter(file) mpath = os.path.join(fdir, file) if os.path.isdir(mpath): continue diff --git a/pylib/tools/genhooks.py b/pylib/tools/genhooks.py index 11d8acc49..575c41e25 100644 --- a/pylib/tools/genhooks.py +++ b/pylib/tools/genhooks.py @@ -32,6 +32,12 @@ hooks = [ args=["exporters: List[Tuple[str, Any]]"], legacy_hook="exportersList", ), + Hook( + name="media_file_filter", + args=["txt: str"], + return_type="str", + doc="""Allows manipulating the file path that media will be read from""", + ), Hook( name="field_filter", args=[ diff --git a/qt/aqt/main.py b/qt/aqt/main.py index 8cea38bcc..2d5f48f58 100644 --- a/qt/aqt/main.py +++ b/qt/aqt/main.py @@ -1374,6 +1374,7 @@ will be lost. Continue?""" check_db(self) def on_check_media_db(self) -> None: + gui_hooks.media_check_will_start() check_media_db(self) def onStudyDeck(self): diff --git a/qt/aqt/mediasrv.py b/qt/aqt/mediasrv.py index 30a30d799..1a0a63437 100644 --- a/qt/aqt/mediasrv.py +++ b/qt/aqt/mediasrv.py @@ -19,6 +19,7 @@ from flask import Response, request from waitress.server import create_server import aqt +from anki import hooks from anki.rsbackend import from_json_bytes from anki.utils import devMode from aqt.qt import * @@ -224,6 +225,8 @@ def _redirectWebExports(path): print(f"collection not open, ignore request for {path}") return None + path = hooks.media_file_filter(path) + return aqt.mw.col.media.dir(), path diff --git a/qt/aqt/sound.py b/qt/aqt/sound.py index d65cb4622..1c28b5e43 100644 --- a/qt/aqt/sound.py +++ b/qt/aqt/sound.py @@ -14,6 +14,7 @@ from operator import itemgetter from typing import Any, Callable, Dict, List, Optional, Tuple import aqt +from anki import hooks from anki.cards import Card from anki.lang import _ from anki.sound import AV_REF_RE, AVTag, SoundOrVideoTag @@ -390,7 +391,9 @@ class MpvManager(MPV, SoundOrVideoPlayer): def play(self, tag: AVTag, on_done: OnDoneCallback) -> None: assert isinstance(tag, SoundOrVideoTag) self._on_done = on_done - path = os.path.join(os.getcwd(), tag.filename) + filename = hooks.media_file_filter(tag.filename) + path = os.path.join(os.getcwd(), filename) + self.command("loadfile", path, "append-play") gui_hooks.av_player_did_begin_playing(self, tag) @@ -434,8 +437,11 @@ class SimpleMplayerSlaveModePlayer(SimpleMplayerPlayer): def _play(self, tag: AVTag) -> None: assert isinstance(tag, SoundOrVideoTag) + + filename = hooks.media_file_filter(tag.filename) + self._process = subprocess.Popen( - self.args + [tag.filename], + self.args + [filename], env=self.env, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 34eebd135..a42b654e6 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -584,6 +584,7 @@ gui_hooks.webview_did_inject_style_into_page.append(mytest) Note that the media sync did not necessarily finish at this point.""", ), + Hook(name="media_check_will_start", args=[]), # Adding cards ################### Hook(