Merge pull request #811 from ianki/media_filters_head

Add hooks for filtering media.
This commit is contained in:
Damien Elmes 2020-11-09 20:41:56 +10:00 committed by GitHub
commit 92e516c6b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 2 deletions

View file

@ -68,6 +68,7 @@ Piotr Kubowicz <piotr.kubowicz@gmail.com>
RumovZ <gp5glkw78@relay.firefox.com>
Cecini <github.com/cecini>
Krish Shah <github.com/k12ish>
ianki <iankigit@gmail.com>
********************

View file

@ -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

View file

@ -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=[

View file

@ -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):

View file

@ -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

View file

@ -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,

View file

@ -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(