/
genhooks.py
175 lines (158 loc) · 5.38 KB
/
genhooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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.
To add a new hook, update the hooks list below, then send a pull request
that includes the changes to this file.
In most cases, hooks are better placed in genhooks_gui.py.
"""
import sys
from hookslib import Hook, write_file
# Hook/filter list
######################################################################
hooks = [
Hook(
name="card_did_leech",
args=["card: Card"],
legacy_hook="leech",
doc="Called by v1/v2 scheduler when a card is marked as a leech.",
),
Hook(name="card_odue_was_invalid"),
Hook(name="schema_will_change", args=["proceed: bool"], return_type="bool"),
Hook(
name="notes_will_be_deleted",
args=["col: anki.collection.Collection", "ids: Sequence[anki.notes.NoteId]"],
legacy_hook="remNotes",
),
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.""",
),
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.",
),
Hook(
name="exporters_list_created",
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=[
"field_text: str",
"field_name: str",
"filter_name: str",
"ctx: anki.template.TemplateRenderContext",
],
return_type="str",
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.""",
),
Hook(
name="note_will_flush",
args=["note: Note"],
doc="Allow to change a note before it is added/updated in the database.",
),
Hook(
name="card_will_flush",
args=["card: Card"],
doc="Allow to change a card before it is added/updated in the database.",
),
Hook(
name="card_did_render",
args=[
"output: anki.template.TemplateRenderOutput",
"ctx: anki.template.TemplateRenderContext",
],
doc="Can modify the resulting text after rendering completes.",
),
Hook(
name="schedv2_did_answer_review_card",
args=["card: anki.cards.Card", "ease: int", "early: bool"],
),
Hook(
name="scheduler_new_limit_for_single_deck",
args=["count: int", "deck: anki.decks.DeckDict"],
return_type="int",
doc="""Allows changing the number of new card for this deck (without
considering descendants).""",
),
Hook(
name="scheduler_review_limit_for_single_deck",
args=["count: int", "deck: anki.decks.DeckDict"],
return_type="int",
doc="""Allows changing the number of rev card for this deck (without
considering descendants).""",
),
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""",
),
# obsolete
Hook(
name="deck_added",
args=["deck: anki.decks.DeckDict"],
doc="Obsolete, do not use.",
),
Hook(
name="note_type_added",
args=["notetype: anki.models.NotetypeDict"],
doc="Obsolete, do not use.",
),
Hook(
name="sync_stage_did_change",
args=["stage: str"],
doc="Obsolete, do not use.",
),
Hook(
name="sync_progress_did_change",
args=["msg: str"],
doc="Obsolete, do not use.",
),
]
prefix = """\
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
# This file is automatically generated; edit tools/genhooks.py instead.
# Please import from anki.hooks instead of this file.
# pylint: disable=unused-import
from __future__ import annotations
from typing import Any, Callable, Sequence
import anki
import anki.hooks
from anki.cards import Card
from anki.notes import Note
"""
suffix = ""
if __name__ == "__main__":
path = sys.argv[1]
write_file(path, hooks, prefix, suffix)