2020-01-13 04:57:51 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
"""
|
|
|
|
Generate code for hook handling, and insert it into anki/hooks.py.
|
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
To add a new hook, update the hooks list below, then send a pull request
|
|
|
|
that includes the changes to this file.
|
2020-05-22 02:47:14 +02:00
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
In most cases, hooks are better placed in genhooks_gui.py.
|
2020-01-13 04:57:51 +01:00
|
|
|
"""
|
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from hookslib import Hook, write_file
|
2020-01-13 04:57:51 +01:00
|
|
|
|
2020-01-13 08:59:52 +01:00
|
|
|
# Hook/filter list
|
2020-01-13 04:57:51 +01:00
|
|
|
######################################################################
|
|
|
|
|
|
|
|
hooks = [
|
2020-01-15 07:43:22 +01:00
|
|
|
Hook(name="card_odue_was_invalid"),
|
|
|
|
Hook(name="schema_will_change", args=["proceed: bool"], return_type="bool"),
|
2020-01-13 08:59:52 +01:00
|
|
|
Hook(
|
2020-01-15 08:45:35 +01:00
|
|
|
name="notes_will_be_deleted",
|
2021-03-27 12:38:20 +01:00
|
|
|
args=["col: anki.collection.Collection", "ids: Sequence[anki.notes.NoteId]"],
|
2020-01-13 08:59:52 +01:00
|
|
|
legacy_hook="remNotes",
|
|
|
|
),
|
2023-04-27 07:34:33 +02:00
|
|
|
Hook(
|
|
|
|
name="note_will_be_added",
|
|
|
|
args=[
|
|
|
|
"col: anki.collection.Collection",
|
|
|
|
"note: anki.notes.Note",
|
|
|
|
"deck_id: anki.decks.DeckId",
|
|
|
|
],
|
|
|
|
doc="""Allows modifying a note before it's added to the collection.
|
|
|
|
|
|
|
|
This hook may be called both when users use the Add screen, and when
|
|
|
|
add-ons like AnkiConnect add notes. It is not called when importing. If
|
|
|
|
you wish to alter the Add screen, use gui_hooks.add_cards_will_add_note
|
|
|
|
instead.""",
|
|
|
|
),
|
2022-05-06 06:47:00 +02:00
|
|
|
Hook(
|
|
|
|
name="media_files_did_export",
|
|
|
|
args=["count: int"],
|
|
|
|
doc="Only used by legacy .apkg exporter. Will be deprecated in the future.",
|
|
|
|
),
|
|
|
|
Hook(
|
|
|
|
name="legacy_export_progress",
|
|
|
|
args=["progress: str"],
|
|
|
|
doc="Temporary hook used in transition to new import/export code.",
|
|
|
|
),
|
2020-01-13 08:59:52 +01:00
|
|
|
Hook(
|
2020-01-15 08:45:35 +01:00
|
|
|
name="exporters_list_created",
|
2021-12-09 00:11:22 +01:00
|
|
|
args=["exporters: list[tuple[str, Any]]"],
|
2020-01-13 08:59:52 +01:00
|
|
|
legacy_hook="exportersList",
|
|
|
|
),
|
2020-11-09 10:45:14 +01:00
|
|
|
Hook(
|
|
|
|
name="media_file_filter",
|
|
|
|
args=["txt: str"],
|
|
|
|
return_type="str",
|
|
|
|
doc="""Allows manipulating the file path that media will be read from""",
|
|
|
|
),
|
2020-02-04 00:07:15 +01:00
|
|
|
Hook(
|
2020-01-15 08:45:35 +01:00
|
|
|
name="field_filter",
|
2020-01-13 08:59:52 +01:00
|
|
|
args=[
|
|
|
|
"field_text: str",
|
|
|
|
"field_name: str",
|
|
|
|
"filter_name: str",
|
2020-01-24 04:44:13 +01:00
|
|
|
"ctx: anki.template.TemplateRenderContext",
|
2020-01-13 08:59:52 +01:00
|
|
|
],
|
|
|
|
return_type="str",
|
2020-01-17 00:30:42 +01:00
|
|
|
doc="""Allows you to define custom {{filters:..}}
|
|
|
|
|
|
|
|
Your add-on can check filter_name to decide whether it should modify
|
|
|
|
field_text or not before returning it.""",
|
|
|
|
),
|
2020-02-12 01:16:38 +01:00
|
|
|
Hook(
|
|
|
|
name="note_will_flush",
|
|
|
|
args=["note: Note"],
|
|
|
|
doc="Allow to change a note before it is added/updated in the database.",
|
|
|
|
),
|
2020-02-12 01:23:20 +01:00
|
|
|
Hook(
|
|
|
|
name="card_will_flush",
|
|
|
|
args=["card: Card"],
|
|
|
|
doc="Allow to change a card before it is added/updated in the database.",
|
|
|
|
),
|
2020-01-17 00:30:42 +01:00
|
|
|
Hook(
|
|
|
|
name="card_did_render",
|
2020-01-24 04:44:13 +01:00
|
|
|
args=[
|
|
|
|
"output: anki.template.TemplateRenderOutput",
|
|
|
|
"ctx: anki.template.TemplateRenderContext",
|
|
|
|
],
|
2020-01-17 00:30:42 +01:00
|
|
|
doc="Can modify the resulting text after rendering completes.",
|
2020-01-13 08:59:52 +01:00
|
|
|
),
|
2021-12-09 02:20:29 +01:00
|
|
|
Hook(
|
|
|
|
name="importing_importers",
|
|
|
|
args=["importers: list[tuple[str, Any]]"],
|
|
|
|
doc="""Allows updating the list of importers.
|
|
|
|
The resulting list is not saved and should be changed each time the
|
|
|
|
filter is called.
|
|
|
|
|
|
|
|
NOTE: Updates to the import/export code are expected in the coming
|
|
|
|
months, and this hook may be replaced with another solution at that
|
|
|
|
time. Tracked on https://github.com/ankitects/anki/issues/1018""",
|
|
|
|
),
|
2020-05-22 02:47:14 +02:00
|
|
|
# obsolete
|
2020-08-11 22:56:58 +02:00
|
|
|
Hook(
|
2021-03-24 07:28:38 +01:00
|
|
|
name="deck_added",
|
|
|
|
args=["deck: anki.decks.DeckDict"],
|
|
|
|
doc="Obsolete, do not use.",
|
2020-08-11 22:56:58 +02:00
|
|
|
),
|
2020-05-22 02:47:14 +02:00
|
|
|
Hook(
|
|
|
|
name="note_type_added",
|
2021-03-27 12:46:49 +01:00
|
|
|
args=["notetype: anki.models.NotetypeDict"],
|
2020-05-22 02:47:14 +02:00
|
|
|
doc="Obsolete, do not use.",
|
|
|
|
),
|
2020-05-30 04:28:22 +02:00
|
|
|
Hook(
|
2020-08-31 05:29:28 +02:00
|
|
|
name="sync_stage_did_change",
|
|
|
|
args=["stage: str"],
|
|
|
|
doc="Obsolete, do not use.",
|
2020-05-30 04:28:22 +02:00
|
|
|
),
|
|
|
|
Hook(
|
2020-08-31 05:29:28 +02:00
|
|
|
name="sync_progress_did_change",
|
|
|
|
args=["msg: str"],
|
|
|
|
doc="Obsolete, do not use.",
|
2020-05-30 04:28:22 +02:00
|
|
|
),
|
2020-01-13 04:57:51 +01:00
|
|
|
]
|
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
prefix = """\
|
2021-04-13 10:45:05 +02:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
# This file is automatically generated; edit tools/genhooks.py instead.
|
|
|
|
# Please import from anki.hooks instead of this file.
|
|
|
|
|
2021-01-07 07:20:02 +01:00
|
|
|
# pylint: disable=unused-import
|
|
|
|
|
2020-11-01 05:26:58 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-12-09 00:11:22 +01:00
|
|
|
from typing import Any, Callable, Sequence
|
2020-11-01 05:26:58 +01:00
|
|
|
import anki
|
|
|
|
import anki.hooks
|
|
|
|
from anki.cards import Card
|
|
|
|
from anki.notes import Note
|
|
|
|
"""
|
|
|
|
|
|
|
|
suffix = ""
|
|
|
|
|
2020-01-13 05:38:05 +01:00
|
|
|
if __name__ == "__main__":
|
2020-11-01 05:26:58 +01:00
|
|
|
path = sys.argv[1]
|
|
|
|
write_file(path, hooks, prefix, suffix)
|