11 FBASENAME, _ = os.path.splitext(os.path.basename(FONAME)) |
14 FBASENAME, _ = os.path.splitext(os.path.basename(FONAME)) |
12 |
15 |
13 CWD = os.getcwd() |
16 CWD = os.getcwd() |
14 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME)) |
17 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME)) |
15 |
18 |
16 def cleanup(type_, ex, traceback): |
19 def cleanup(): |
17 os.chdir(CWD) |
20 os.chdir(CWD) |
18 shutil.rmtree(TMPDIR) |
21 shutil.rmtree(TMPDIR) |
19 |
22 |
20 sys.excepthook = cleanup |
23 def exception_handler(etype, ex, tb): |
|
24 cleanup() |
|
25 traceback.print_exception(etype, ex, tb) |
|
26 |
|
27 sys.excepthook = exception_handler |
21 |
28 |
22 sys.path.append('/usr/share/anki') |
29 sys.path.append('/usr/share/anki') |
23 import anki |
30 import anki |
24 from anki.exporting import AnkiPackageExporter |
31 from anki.exporting import AnkiPackageExporter |
25 |
32 |
48 font-style: italic; |
55 font-style: italic; |
49 } |
56 } |
50 """ |
57 """ |
51 |
58 |
52 collection.models.addField(model, collection.models.newField('en')) |
59 collection.models.addField(model, collection.models.newField('en')) |
53 collection.models.addField(model, collection.models.newField('tr')) |
60 collection.models.addField(model, collection.models.newField('ru')) |
54 |
61 |
55 tmpl = collection.models.newTemplate('en -> tr') |
62 tmpl = collection.models.newTemplate('en -> ru') |
56 tmpl['qfmt'] = '<div class="from">{{en}}</div>' |
63 tmpl['qfmt'] = '<div class="from">{{en}}</div>' |
57 tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n{{tr}}' |
64 tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n{{ru}}' |
58 collection.models.addTemplate(model, tmpl) |
65 collection.models.addTemplate(model, tmpl) |
59 tmpl = collection.models.newTemplate('tr -> en') |
66 tmpl = collection.models.newTemplate('ru -> en') |
60 tmpl['qfmt'] = '{{tr}}' |
67 tmpl['qfmt'] = '{{ru}}' |
61 tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n<div class="from">{{en}}</div>' |
68 tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n<div class="from">{{en}}</div>' |
62 collection.models.addTemplate(model, tmpl) |
69 collection.models.addTemplate(model, tmpl) |
63 |
70 |
64 |
71 # print(dir(model)) |
65 print(dir(model)) |
72 # print(type(model)) |
66 print(type(model)) |
73 # print(model) |
67 print(model) |
|
68 # Equivalent of: |
74 # Equivalent of: |
69 # collection.models.add(model) |
75 # collection.models.add(model) |
70 # without setting auto-generated ID: |
76 # without setting auto-generated ID: |
71 model['id'] = 12345678 # essential for upgrade detection |
77 model['id'] = 12345678 # essential for upgrade detection |
72 collection.models.update(model) |
78 collection.models.update(model) |
74 collection.models.save(model) |
80 collection.models.save(model) |
75 |
81 |
76 # collection.decks.select(deck_id) |
82 # collection.decks.select(deck_id) |
77 |
83 |
78 note = anki.notes.Note(collection, model) |
84 note = anki.notes.Note(collection, model) |
79 print(dir(note)) |
85 # print(dir(note)) |
80 print(type(note)) |
86 # print(type(note)) |
81 print(note._fmap) |
87 # print(note._fmap) |
82 print(note) |
88 # print(note) |
83 |
89 |
84 |
90 |
85 note['en'] = "hello" |
91 note['en'] = "hello" |
86 note['tr'] = u"[heləʊ]\nint. привет" |
92 note['ru'] = u"[heləʊ]\nint. привет" |
87 note.guid = "xxx1" |
93 note.guid = "enru_1" |
88 collection.addNote(note) |
94 collection.addNote(note) |
89 print(dir(note)) |
95 # print(dir(note)) |
90 |
96 |
91 note = collection.newNote() |
97 note = collection.newNote() |
92 note['en'] = "bye" |
98 note['en'] = "bye" |
93 note['tr'] = u"[baɪ]\nint. пока" |
99 note['ru'] = u"[baɪ]\nint. пока" |
94 note.guid = "xxx2" |
100 note.guid = "enru_2" |
95 collection.addNote(note) |
101 collection.addNote(note) |
96 |
102 |
97 # model.add(deck) |
103 # model.add(deck) |
98 # model.save() |
104 # model.save() |
|
105 |
|
106 ################################################################ |
|
107 # Add another model and new types of notes into same storage! |
|
108 model_irrverb = collection.models.new(FBASENAME + "_model_irrverb") |
|
109 model_irrverb['tags'].append(FBASENAME + "_tag_irrverb") |
|
110 model_irrverb['did'] = deck_id |
|
111 model_irrverb['css'] = """ |
|
112 .card { |
|
113 font-family: arial; |
|
114 font-size: 20px; |
|
115 text-align: center; |
|
116 color: black; |
|
117 background-color: white; |
|
118 } |
|
119 .v1 { color: red; } |
|
120 .v2 { color: green; } |
|
121 .v3 { color: blue; } |
|
122 """ |
|
123 |
|
124 collection.models.addField(model_irrverb, collection.models.newField('v1')) |
|
125 collection.models.addField(model_irrverb, collection.models.newField('v2')) |
|
126 collection.models.addField(model_irrverb, collection.models.newField('v3')) |
|
127 |
|
128 ANSWER_MSG = '{{FrontSide}}<hr id=answer>{{v1}}\n{{v2}}\n{{v3}}' |
|
129 tmpl = collection.models.newTemplate('v1') |
|
130 tmpl['qfmt'] = '<div class="v1">{{v1}}</div>' |
|
131 tmpl['afmt'] = ANSWER_MSG |
|
132 collection.models.addTemplate(model_irrverb, tmpl) |
|
133 |
|
134 tmpl = collection.models.newTemplate('v2') |
|
135 tmpl['qfmt'] = '<div class="v2">{{v2}}</div>' |
|
136 tmpl['afmt'] = ANSWER_MSG |
|
137 collection.models.addTemplate(model_irrverb, tmpl) |
|
138 tmpl = collection.models.newTemplate('v3') |
|
139 tmpl['qfmt'] = '<div class="v3">{{v3}}</div>' |
|
140 tmpl['afmt'] = ANSWER_MSG |
|
141 collection.models.addTemplate(model_irrverb, tmpl) |
|
142 |
|
143 # We mustn't define 'id' before adding tmpl because addTemplate() fail because |
|
144 # it tries to update DB but we have no DB entry yet... |
|
145 model_irrverb['id'] = 12345678+1 # essential for upgrade detection |
|
146 collection.models.update(model_irrverb) |
|
147 collection.models.setCurrent(model_irrverb) |
|
148 collection.models.save(model_irrverb) |
|
149 |
|
150 note = anki.notes.Note(collection, model_irrverb) |
|
151 note['v1'] = "go" |
|
152 note['v2'] = "went" |
|
153 note['v3'] = "gone" |
|
154 note.guid = "irrverb_1" |
|
155 collection.addNote(note) |
|
156 |
|
157 note = anki.notes.Note(collection, model_irrverb) |
|
158 note['v1'] = "drive" |
|
159 note['v2'] = "drove" |
|
160 note['v3'] = "driven" |
|
161 note.guid = "irrverb_2" |
|
162 collection.addNote(note) |
99 |
163 |
100 export = AnkiPackageExporter(collection) |
164 export = AnkiPackageExporter(collection) |
101 try: |
165 try: |
102 os.remove(FONAME) |
166 os.remove(FONAME) |
103 except OSError as ex: |
167 except OSError as ex: |