-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_cards.py
executable file
·156 lines (121 loc) · 4.42 KB
/
generate_cards.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
#!/usr/bin/env python3
# Copyright (C) 2017-2018 Thomas Rebele
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""generate_cards
Usage:
generate_cards.py [options] <file>...
Options:
-h --help Show this screen.
"""
import pathlib
from docopt import docopt
import traceback
try:
genanki = None
import genanki.genanki as genanki
except:
clone_cmd_https = "git clone https://github.com/kerrickstaley/genanki.git"
clone_cmd_ssh = "git clone https://github.com/kerrickstaley/genanki.git"
print("genanki not found, please execute")
print(" " + clone_cmd_ssh)
print("or")
print(" " + clone_cmd_https)
print()
from level_estimate import *
from common import *
# TODO:
no_hierarchy=False
def find_duplicates(path_to_cards):
"""check for duplicate descriptions
returns list of cards"""
description_to_path_to_card = {}
for path, card in path_to_cards:
desc = card["description"]
description_to_path_to_card.setdefault(desc, {})[path] = card
# find duplicates
cards = []
for desc, path_to_card in description_to_path_to_card.items():
if len(path_to_card) == 1:
cards += list(path_to_card.values())
elif len(path_to_card) > 1:
print_err("duplicate description: '" + desc + "', paths ")
print_err("in paths ")
for path in path_to_card:
print_err(" " + path)
return cards
def index_cards(cards):
return dict([(c["example"], i) for i,c in enumerate(cards)])
################################################################################
# genanki
################################################################################
minex_deck = None
if genanki:
def read_model_file(path):
return pathlib.Path(path).read_text()
minex_model = genanki.Model(
1508403096468,
'General::IT',
fields=[
{'name': 'example'},
{'name': 'description'},
{'name': 'pre'},
{'name': 'step'},
{'name': 'post'},
{'name': 'explanation'},
],
templates=[
{
"name": "Card 1",
"qfmt": read_model_file("model/question"),
"afmt": read_model_file("model/answer")
}
],
css=read_model_file("model/style.css"))
class MinExNote(genanki.Note):
@property
def guid(self):
return genanki.guid_for(self.fields[0])
minex_deck = genanki.Deck(
1509187590598,
'General::IT')
################################################################################
# main
################################################################################
if __name__ == '__main__':
arguments = docopt(__doc__, version='read_annotations')
path_to_cards = read_cards(arguments["<file>"]).items()
cards = find_duplicates(path_to_cards)
example_to_index = index_cards(cards)
example_to_level = calculate_levels(cards)
for ex in example_to_level:
if not ex in example_to_index:
print_err("not found " + str(ex))
cards_levels = [(c, example_to_level.get(c["example"], 100)) for c in cards]
sorted_cards_levels = sorted(cards_levels, key = lambda t: t[1])
cards = [c for (c,l) in sorted_cards_levels]
for card in cards:
fields = ["example", "description", "pre", "step", "post", "explanation"]
values=[str(card.get(f, "")) for f in fields]
print("\t".join(values))
if minex_deck:
minex_note = MinExNote(
model=minex_model,
fields=values)
minex_deck.add_note(minex_note)
print(minex_note.guid)
output_file = "/tmp/output.apkg"
if minex_deck:
genanki.Package(minex_deck).write_to_file(output_file)
print("wrote Anki deck to " + str(output_file))