forked from FooSoft/anki-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnkiConnect.py
1163 lines (867 loc) · 30.3 KB
/
AnkiConnect.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2016 Alex Yatskov <[email protected]>
# Author: Alex Yatskov <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import anki
import aqt
import base64
import hashlib
import inspect
import json
import os
import os.path
import re
import select
import socket
import sys
from operator import itemgetter
from time import time
from unicodedata import normalize
#
# Constants
#
API_VERSION = 6
API_LOG_PATH = None
NET_ADDRESS = os.getenv('ANKICONNECT_BIND_ADDRESS', '127.0.0.1')
NET_BACKLOG = 5
NET_PORT = 8765
TICK_INTERVAL = 25
URL_TIMEOUT = 10
URL_UPGRADE = 'https://raw.githubusercontent.com/FooSoft/anki-connect/master/AnkiConnect.py'
#
# Helpers
#
if sys.version_info[0] < 3:
import urllib2
def download(url):
contents = None
resp = urllib2.urlopen(url, timeout=URL_TIMEOUT)
if resp.code == 200:
contents = resp.read()
return (resp.code, contents)
from PyQt4.QtCore import QTimer
from PyQt4.QtGui import QMessageBox
else:
unicode = str
from anki.sync import AnkiRequestsClient
def download(url):
contents = None
client = AnkiRequestsClient()
client.timeout = URL_TIMEOUT
resp = client.get(url)
if resp.status_code == 200:
contents = client.streamContent(resp)
return (resp.status_code, contents)
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox
def makeBytes(data):
return data.encode('utf-8')
def makeStr(data):
return data.decode('utf-8')
def api(*versions):
def decorator(func):
method = lambda *args, **kwargs: func(*args, **kwargs)
setattr(method, 'versions', versions)
setattr(method, 'api', True)
return method
return decorator
#
# WebRequest
#
class WebRequest:
def __init__(self, headers, body):
self.headers = headers
self.body = body
#
# WebClient
#
class WebClient:
def __init__(self, sock, handler):
self.sock = sock
self.handler = handler
self.readBuff = bytes()
self.writeBuff = bytes()
def advance(self, recvSize=1024):
if self.sock is None:
return False
rlist, wlist = select.select([self.sock], [self.sock], [], 0)[:2]
if rlist:
msg = self.sock.recv(recvSize)
if not msg:
self.close()
return False
self.readBuff += msg
req, length = self.parseRequest(self.readBuff)
if req is not None:
self.readBuff = self.readBuff[length:]
self.writeBuff += self.handler(req)
if wlist and self.writeBuff:
length = self.sock.send(self.writeBuff)
self.writeBuff = self.writeBuff[length:]
if not self.writeBuff:
self.close()
return False
return True
def close(self):
if self.sock is not None:
self.sock.close()
self.sock = None
self.readBuff = bytes()
self.writeBuff = bytes()
def parseRequest(self, data):
parts = data.split(makeBytes('\r\n\r\n'), 1)
if len(parts) == 1:
return None, 0
headers = {}
for line in parts[0].split(makeBytes('\r\n')):
pair = line.split(makeBytes(': '))
headers[pair[0].lower()] = pair[1] if len(pair) > 1 else None
headerLength = len(parts[0]) + 4
bodyLength = int(headers.get(makeBytes('content-length'), 0))
totalLength = headerLength + bodyLength
if totalLength > len(data):
return None, 0
body = data[headerLength : totalLength]
return WebRequest(headers, body), totalLength
#
# WebServer
#
class WebServer:
def __init__(self, handler):
self.handler = handler
self.clients = []
self.sock = None
self.resetHeaders()
def setHeader(self, name, value):
self.headersOpt[name] = value
def resetHeaders(self):
self.headers = [
['HTTP/1.1 200 OK', None],
['Content-Type', 'text/json'],
['Access-Control-Allow-Origin', '*']
]
self.headersOpt = {}
def getHeaders(self):
headers = self.headers[:]
for name in self.headersOpt:
headers.append([name, self.headersOpt[name]])
return headers
def advance(self):
if self.sock is not None:
self.acceptClients()
self.advanceClients()
def acceptClients(self):
rlist = select.select([self.sock], [], [], 0)[0]
if not rlist:
return
clientSock = self.sock.accept()[0]
if clientSock is not None:
clientSock.setblocking(False)
self.clients.append(WebClient(clientSock, self.handlerWrapper))
def advanceClients(self):
self.clients = list(filter(lambda c: c.advance(), self.clients))
def listen(self):
self.close()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.setblocking(False)
self.sock.bind((NET_ADDRESS, NET_PORT))
self.sock.listen(NET_BACKLOG)
def handlerWrapper(self, req):
if len(req.body) == 0:
body = makeBytes('AnkiConnect v.{}'.format(API_VERSION))
else:
try:
params = json.loads(makeStr(req.body))
body = makeBytes(json.dumps(self.handler(params)))
except ValueError:
body = makeBytes(json.dumps(None))
resp = bytes()
self.setHeader('Content-Length', str(len(body)))
headers = self.getHeaders()
for key, value in headers:
if value is None:
resp += makeBytes('{}\r\n'.format(key))
else:
resp += makeBytes('{}: {}\r\n'.format(key, value))
resp += makeBytes('\r\n')
resp += body
return resp
def close(self):
if self.sock is not None:
self.sock.close()
self.sock = None
for client in self.clients:
client.close()
self.clients = []
#
# AnkiConnect
#
class AnkiConnect:
def __init__(self):
self.server = WebServer(self.handler)
self.log = None
if API_LOG_PATH is not None:
self.log = open(API_LOG_PATH, 'w')
try:
self.server.listen()
self.timer = QTimer()
self.timer.timeout.connect(self.advance)
self.timer.start(TICK_INTERVAL)
except:
QMessageBox.critical(
self.window(),
'AnkiConnect',
'Failed to listen on port {}.\nMake sure it is available and is not in use.'.format(NET_PORT)
)
def advance(self):
self.server.advance()
def handler(self, request):
if self.log is not None:
self.log.write('[request]\n')
json.dump(request, self.log, indent=4, sort_keys=True)
self.log.write('\n\n')
name = request.get('action', '')
version = request.get('version', 4)
params = request.get('params', {})
reply = {'result': None, 'error': None}
try:
method = None
for methodName, methodInst in inspect.getmembers(self, predicate=inspect.ismethod):
apiVersionLast = 0
apiNameLast = None
if getattr(methodInst, 'api', False):
for apiVersion, apiName in getattr(methodInst, 'versions', []):
if apiVersionLast < apiVersion <= version:
apiVersionLast = apiVersion
apiNameLast = apiName
if apiNameLast is None and apiVersionLast == 0:
apiNameLast = methodName
if apiNameLast is not None and apiNameLast == name:
method = methodInst
break
if method is None:
raise Exception('unsupported action')
else:
reply['result'] = methodInst(**params)
except Exception as e:
reply['error'] = str(e)
if version <= 4:
reply = reply['result']
if self.log is not None:
self.log.write('[reply]\n')
json.dump(reply, self.log, indent=4, sort_keys=True)
self.log.write('\n\n')
return reply
def download(self, url):
try:
(code, contents) = download(url)
except Exception as e:
raise Exception('{} download failed with error {}'.format(url, str(e)))
if code == 200:
return contents
else:
raise Exception('{} download failed with return code {}'.format(url, code))
def window(self):
return aqt.mw
def reviewer(self):
reviewer = self.window().reviewer
if reviewer is None:
raise Exception('reviewer is not available')
else:
return reviewer
def collection(self):
collection = self.window().col
if collection is None:
raise Exception('collection is not available')
else:
return collection
def decks(self):
decks = self.collection().decks
if decks is None:
raise Exception('decks are not available')
else:
return decks
def scheduler(self):
scheduler = self.collection().sched
if scheduler is None:
raise Exception('scheduler is not available')
else:
return scheduler
def database(self):
database = self.collection().db
if database is None:
raise Exception('database is not available')
else:
return database
def media(self):
media = self.collection().media
if media is None:
raise Exception('media is not available')
else:
return media
def startEditing(self):
self.window().requireReset()
def stopEditing(self):
if self.collection() is not None:
self.window().maybeReset()
def createNote(self, note):
collection = self.collection()
model = collection.models.byName(note['modelName'])
if model is None:
raise Exception('model was not found: {}'.format(note['modelName']))
deck = collection.decks.byName(note['deckName'])
if deck is None:
raise Exception('deck was not found: {}'.format(note['deckName']))
ankiNote = anki.notes.Note(collection, model)
ankiNote.model()['did'] = deck['id']
ankiNote.tags = note['tags']
for name, value in note['fields'].items():
if name in ankiNote:
ankiNote[name] = value
allowDuplicate = False
if 'options' in note:
if 'allowDuplicate' in note['options']:
allowDuplicate = note['options']['allowDuplicate']
if type(allowDuplicate) is not bool:
raise Exception('option parameter \'allowDuplicate\' must be boolean')
duplicateOrEmpty = ankiNote.dupeOrEmpty()
if duplicateOrEmpty == 1:
raise Exception('cannot create note because it is empty')
elif duplicateOrEmpty == 2:
if not allowDuplicate:
raise Exception('cannot create note because it is a duplicate')
else:
return ankiNote
elif duplicateOrEmpty == False:
return ankiNote
else:
raise Exception('cannot create note for unknown reason')
#
# Miscellaneous
#
@api()
def version(self):
return API_VERSION
@api()
def upgrade(self):
response = QMessageBox.question(
self.window(),
'AnkiConnect',
'Upgrade to the latest version?',
QMessageBox.Yes | QMessageBox.No
)
if response == QMessageBox.Yes:
try:
data = self.download(URL_UPGRADE)
path = os.path.splitext(__file__)[0] + '.py'
with open(path, 'w') as fp:
fp.write(makeStr(data))
QMessageBox.information(
self.window(),
'AnkiConnect',
'Upgraded to the latest version, please restart Anki.'
)
return True
except Exception as e:
QMessageBox.critical(self.window(), 'AnkiConnect', 'Failed to download latest version.')
raise e
return False
@api()
def sync(self):
self.window().onSync()
@api()
def multi(self, actions):
return list(map(self.handler, actions))
#
# Decks
#
@api()
def deckNames(self):
return self.decks().allNames()
@api()
def deckNamesAndIds(self):
decks = {}
for deck in self.deckNames():
decks[deck] = self.decks().id(deck)
return decks
@api()
def getDecks(self, cards):
decks = {}
for card in cards:
did = self.database().scalar('select did from cards where id=?', card)
deck = self.decks().get(did)['name']
if deck in decks:
decks[deck].append(card)
else:
decks[deck] = [card]
return decks
@api()
def createDeck(self, deck):
try:
self.startEditing()
did = self.decks().id(deck)
finally:
self.stopEditing()
return did
@api()
def changeDeck(self, cards, deck):
self.startEditing()
did = self.collection().decks.id(deck)
mod = anki.utils.intTime()
usn = self.collection().usn()
# normal cards
scids = anki.utils.ids2str(cards)
# remove any cards from filtered deck first
self.collection().sched.remFromDyn(cards)
# then move into new deck
self.collection().db.execute('update cards set usn=?, mod=?, did=? where id in ' + scids, usn, mod, did)
self.stopEditing()
@api()
def deleteDecks(self, decks, cardsToo=False):
try:
self.startEditing()
decks = filter(lambda d: d in self.deckNames(), decks)
for deck in decks:
did = self.decks().id(deck)
self.decks().rem(did, cardsToo)
finally:
self.stopEditing()
@api()
def getDeckConfig(self, deck):
if not deck in self.deckNames():
return False
collection = self.collection()
did = collection.decks.id(deck)
return collection.decks.confForDid(did)
@api()
def saveDeckConfig(self, config):
collection = self.collection()
config['id'] = str(config['id'])
config['mod'] = anki.utils.intTime()
config['usn'] = collection.usn()
if not config['id'] in collection.decks.dconf:
return False
collection.decks.dconf[config['id']] = config
collection.decks.changed = True
return True
@api()
def setDeckConfigId(self, decks, configId):
configId = str(configId)
for deck in decks:
if not deck in self.deckNames():
return False
collection = self.collection()
if not configId in collection.decks.dconf:
return False
for deck in decks:
did = str(collection.decks.id(deck))
aqt.mw.col.decks.decks[did]['conf'] = configId
return True
@api()
def cloneDeckConfigId(self, name, cloneFrom='1'):
configId = str(cloneFrom)
if not configId in self.collection().decks.dconf:
return False
config = self.collection().decks.getConf(configId)
return self.collection().decks.confId(name, config)
@api()
def removeDeckConfigId(self, configId):
configId = str(configId)
collection = self.collection()
if configId == 1 or not configId in collection.decks.dconf:
return False
collection.decks.remConf(configId)
return True
@api()
def storeMediaFile(self, filename, data):
self.deleteMediaFile(filename)
self.media().writeData(filename, base64.b64decode(data))
@api()
def retrieveMediaFile(self, filename):
filename = os.path.basename(filename)
filename = normalize('NFC', filename)
filename = self.media().stripIllegal(filename)
path = os.path.join(self.media().dir(), filename)
if os.path.exists(path):
with open(path, 'rb') as file:
return base64.b64encode(file.read()).decode('ascii')
return False
@api()
def deleteMediaFile(self, filename):
self.media().syncDelete(filename)
@api()
def addNote(self, note):
ankiNote = self.createNote(note)
audio = note.get('audio')
if audio is not None and len(audio['fields']) > 0:
try:
data = self.download(audio['url'])
skipHash = audio.get('skipHash')
if skipHash is None:
skip = False
else:
m = hashlib.md5()
m.update(data)
skip = skipHash == m.hexdigest()
if not skip:
for field in audio['fields']:
if field in ankiNote:
ankiNote[field] += u'[sound:{}]'.format(audio['filename'])
self.media().writeData(audio['filename'], data)
except Exception as e:
errorMessage = str(e).replace("&", "&").replace("<", "<").replace(">", ">")
for field in audio['fields']:
if field in ankiNote:
ankiNote[field] += errorMessage
collection = self.collection()
self.startEditing()
collection.addNote(ankiNote)
collection.autosave()
self.stopEditing()
return ankiNote.id
@api()
def canAddNote(self, note):
try:
return bool(self.createNote(note))
except:
return False
@api()
def updateNoteFields(self, note):
ankiNote = self.collection().getNote(note['id'])
if ankiNote is None:
raise Exception('note was not found: {}'.format(note['id']))
for name, value in note['fields'].items():
if name in ankiNote:
ankiNote[name] = value
ankiNote.flush()
@api()
def addTags(self, notes, tags, add=True):
self.startEditing()
self.collection().tags.bulkAdd(notes, tags, add)
self.stopEditing()
@api()
def removeTags(self, notes, tags):
return self.addTags(notes, tags, False)
@api()
def getTags(self):
return self.collection().tags.all()
@api()
def suspend(self, cards, suspend=True):
for card in cards:
if self.suspended(card) == suspend:
cards.remove(card)
if len(cards) == 0:
return False
scheduler = self.scheduler()
self.startEditing()
if suspend:
scheduler.suspendCards(cards)
else:
scheduler.unsuspendCards(cards)
self.stopEditing()
return True
@api()
def unsuspend(self, cards):
self.suspend(cards, False)
@api()
def suspended(self, card):
card = self.collection().getCard(card)
return card.queue == -1
@api()
def areSuspended(self, cards):
suspended = []
for card in cards:
suspended.append(self.suspended(card))
return suspended
@api()
def areDue(self, cards):
due = []
for card in cards:
if self.findCards('cid:{} is:new'.format(card)):
due.append(True)
else:
date, ivl = self.collection().db.all('select id/1000.0, ivl from revlog where cid = ?', card)[-1]
if ivl >= -1200:
due.append(bool(self.findCards('cid:{} is:due'.format(card))))
else:
due.append(date - ivl <= time())
return due
@api()
def getIntervals(self, cards, complete=False):
intervals = []
for card in cards:
if self.findCards('cid:{} is:new'.format(card)):
intervals.append(0)
else:
interval = self.collection().db.list('select ivl from revlog where cid = ?', card)
if not complete:
interval = interval[-1]
intervals.append(interval)
return intervals
@api()
def modelNames(self):
return self.collection().models.allNames()
@api()
def modelNamesAndIds(self):
models = {}
for model in self.modelNames():
models[model] = int(self.collection().models.byName(model)['id'])
return models
@api()
def modelNameFromId(self, modelId):
model = self.collection().models.get(modelId)
if model is None:
raise Exception('model was not found: {}'.format(modelId))
else:
return model['name']
@api()
def modelFieldNames(self, modelName):
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
else:
return [field['name'] for field in model['flds']]
@api()
def modelFieldsOnTemplates(self, modelName):
model = self.collection().models.byName(modelName)
if model is None:
raise Exception('model was not found: {}'.format(modelName))
templates = {}
for template in model['tmpls']:
fields = []
for side in ['qfmt', 'afmt']:
fieldsForSide = []
# based on _fieldsOnTemplate from aqt/clayout.py
matches = re.findall('{{[^#/}]+?}}', template[side])
for match in matches:
# remove braces and modifiers
match = re.sub(r'[{}]', '', match)
match = match.split(':')[-1]
# for the answer side, ignore fields present on the question side + the FrontSide field
if match == 'FrontSide' or side == 'afmt' and match in fields[0]:
continue
fieldsForSide.append(match)
fields.append(fieldsForSide)
templates[template['name']] = fields
return templates
@api()
def deckNameFromId(self, deckId):
deck = self.collection().decks.get(deckId)
if deck is None:
raise Exception('deck was not found: {}'.format(deckId))
else:
return deck['name']
@api()
def findNotes(self, query=None):
if query is None:
return []
else:
return self.collection().findNotes(query)
@api()
def findCards(self, query=None):
if query is None:
return []
else:
return self.collection().findCards(query)
@api()
def cardsInfo(self, cards):
result = []
for cid in cards:
try:
card = self.collection().getCard(cid)
model = card.model()
note = card.note()
fields = {}
for info in model['flds']:
order = info['ord']
name = info['name']
fields[name] = {'value': note.fields[order], 'order': order}
result.append({
'cardId': card.id,
'fields': fields,
'fieldOrder': card.ord,
'question': card._getQA()['q'],
'answer': card._getQA()['a'],
'modelName': model['name'],
'deckName': self.deckNameFromId(card.did),
'css': model['css'],
'factor': card.factor,
#This factor is 10 times the ease percentage,
# so an ease of 310% would be reported as 3100
'interval': card.ivl,
'note': card.nid
})
except TypeError as e:
# Anki will give a TypeError if the card ID does not exist.
# Best behavior is probably to add an 'empty card' to the
# returned result, so that the items of the input and return
# lists correspond.
result.append({})
return result
@api()
def notesInfo(self, notes):
result = []
for nid in notes:
try:
note = self.collection().getNote(nid)
model = note.model()
fields = {}
for info in model['flds']:
order = info['ord']
name = info['name']
fields[name] = {'value': note.fields[order], 'order': order}
result.append({
'noteId': note.id,
'tags' : note.tags,
'fields': fields,
'modelName': model['name'],
'cards': self.collection().db.list('select id from cards where nid = ? order by ord', note.id)
})
except TypeError as e:
# Anki will give a TypeError if the note ID does not exist.
# Best behavior is probably to add an 'empty card' to the
# returned result, so that the items of the input and return
# lists correspond.
result.append({})
return result
@api()
def cardsToNotes(self, cards):
return self.collection().db.list('select distinct nid from cards where id in ' + anki.utils.ids2str(cards))
@api()
def guiBrowse(self, query=None):
browser = aqt.dialogs.open('Browser', self.window())
browser.activateWindow()