This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
forked from johnpincock/SpecialFields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialFields2.0.py
363 lines (336 loc) · 12.8 KB
/
SpecialFields2.0.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
from aqt import mw
from anki.exporting import AnkiExporter
from aqt.utils import showInfo
import re
import os
import zipfile
import shutil
import unicodedata
from anki.lang import _
from anki import Collection
import json
from anki.utils import ids2str, splitFields, json, namedtmp
from anki.importing import Anki2Importer
# #########################################################
# How to use:
# Go Tools -> Manage Note types..
# add a new note type
# choose a name
# select note type
# click "Fields.."
# Add Field
# Use the exact same name as you have set for SPECIAL_FIELD below... default is "Lecture Notes"
# Now when you export, your special field notes will be kept with you and not exported
# #########################################################
SPECIAL_FIELD = [u"Lecture Notes", u"Rx/UWORLD Details", u"Boards and Beyond Expansion",
u"Pathoma Details"] # add more between the brackets eg. u"Text",u"Extra",u"Front",u"Back"
COMBINE_TAGGING = False # change this to True if you would like to concatenate tags
GUID = 1
MID = 2
MOD = 3
def newExportInto(self, path):
# sched info+v2 scheduler not compatible w/ older clients
# showInfo("newtype of import")
try:
self._v2sched = self.col.schedVer() != 1 and self.includeSched
except AttributeError:
pass
# create a new collection at the target
try:
os.unlink(path)
except (IOError, OSError):
pass
self.dst = Collection(path)
self.src = self.col
# find cards
if not self.did:
cids = self.src.db.list("select id from cards")
else:
cids = self.src.decks.cids(self.did, children=True)
# copy cards, noting used nids
nids = {}
data = []
for row in self.src.db.execute(
"select * from cards where id in "+ids2str(cids)):
nids[row[1]] = True
data.append(row)
# clear flags
row = list(row)
row[-2] = 0
self.dst.db.executemany(
"insert into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
data)
# notes
########################################################################
# check if any models with special field exist
midCheck = []
models = mw.col.db.scalar("""select models from col""")
b = json.loads(models)
a = list(b.values())
# showInfo("%s"%SPECIAL_FIELD)
for i in a:
fields = i["flds"]
for n in fields:
if n['name'] in SPECIAL_FIELD:
midCheck.append(str(i["id"]))
########################################################################
# showInfo("%s"%midCheck)
strnids = ids2str(list(nids.keys()))
notedata = []
for row in self.src.db.all(
"select * from notes where id in "+strnids):
# remove system tags if not exporting scheduling info
if not self.includeSched:
row = list(row)
row[5] = self.removeSystemTags(row[5])
if str(row[2]) in midCheck:
# if this note belongs to a model with "Special Field"
trow = list(row)
for i in SPECIAL_FIELD:
try:
row = list(row)
items = mw.col.getNote(row[0]).items()
fieldOrd = [item for item in items if item[0] == i]
fieldOrd = items.index(fieldOrd[0])
fields = [item[1] for item in items]
splitRow = row[6].split("\x1f")
splitRow = splitRow[:len(fields)]
finalrow = ''
count = 0
for i in items:
if count == fieldOrd:
finalrow += "\x1f"
else:
i = list(i)
finalrow += str(i[1].encode("utf-8"))+"\x1f"
count = count+1
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
finalrow = rreplace(finalrow, """\x1f""", '', 1)
row[6] = str(finalrow)
row = tuple(row)
except IndexError:
pass
notedata.append(row)
# FOR TROUBLE SHOOTING ! Change to the card.id you are uncertain about
# showInfo("%s" % str(notedata))
self.dst.db.executemany(
"insert into notes values (?,?,?,?,?,?,?,?,?,?,?)",
notedata)
# models used by the notes
mids = self.dst.db.list("select distinct mid from notes where id in " +
strnids)
# card history and revlog
if self.includeSched:
data = self.src.db.all(
"select * from revlog where cid in "+ids2str(cids))
self.dst.db.executemany(
"insert into revlog values (?,?,?,?,?,?,?,?,?)",
data)
else:
# need to reset card state
self.dst.sched.resetCards(cids)
# models - start with zero
self.dst.models.models = {}
for m in self.src.models.all():
if int(m['id']) in mids:
self.dst.models.update(m)
# decks
if not self.did:
dids = []
else:
dids = [self.did] + [
x[1] for x in self.src.decks.children(self.did)]
dconfs = {}
for d in self.src.decks.all():
if str(d['id']) == "1":
continue
if dids and d['id'] not in dids:
continue
if not d['dyn'] and d['conf'] != 1:
if self.includeSched:
dconfs[d['conf']] = True
if not self.includeSched:
# scheduling not included, so reset deck settings to default
d = dict(d)
d['conf'] = 1
self.dst.decks.update(d)
# copy used deck confs
for dc in self.src.decks.allConf():
if dc['id'] in dconfs:
self.dst.decks.updateConf(dc)
# find used media
media = {}
self.mediaDir = self.src.media.dir()
if self.includeMedia:
for row in notedata:
flds = row[6]
mid = row[2]
for file in self.src.media.filesInStr(mid, flds):
# skip files in subdirs
if file != os.path.basename(file):
continue
media[file] = True
if self.mediaDir:
for fname in os.listdir(self.mediaDir):
path = os.path.join(self.mediaDir, fname)
if os.path.isdir(path):
continue
if fname.startswith("_"):
# Scan all models in mids for reference to fname
for m in self.src.models.all():
if int(m['id']) in mids:
if self._modelHasMedia(m, fname):
media[fname] = True
break
self.mediaFiles = list(media.keys())
self.dst.crt = self.src.crt
# todo: tags?
self.count = self.dst.cardCount()
self.dst.setMod()
self.postExport()
self.dst.close()
def newImportNotes(self):
# build guid -> (id,mod,mid) hash & map of existing note ids
self._notes = {}
existing = {}
for id, guid, mod, mid in self.dst.db.execute(
"select id, guid, mod, mid from notes"):
self._notes[guid] = (id, mod, mid)
existing[id] = True
# we may need to rewrite the guid if the model schemas don't match,
# so we need to keep track of the changes for the card import stage
self._changedGuids = {}
# we ignore updates to changed schemas. we need to note the ignored
# guids, so we avoid importing invalid cards
self._ignoredGuids = {}
# iterate over source collection
add = []
update = []
dirty = []
usn = self.dst.usn()
dupes = 0
dupesIgnored = []
########################################################################
# check if any models with special field exist
midCheck = []
models = mw.col.db.scalar("""select models from col""")
b = json.loads(models)
a = list(b.values())
for i in a:
fields = i["flds"]
for n in fields:
if n['name'] in SPECIAL_FIELD:
midCheck.append(str(i["id"]))
########################################################################
for note in self.src.db.execute(
"select * from notes"):
# turn the db result into a mutable list
note = list(note)
shouldAdd = self._uniquifyNote(note)
if shouldAdd:
# ensure id is unique
while note[0] in existing:
note[0] += 999
existing[note[0]] = True
# bump usn
note[4] = usn
# update media references in case of dupes
note[6] = self._mungeMedia(note[MID], note[6])
add.append(note)
dirty.append(note[0])
# note we have the added the guid
self._notes[note[GUID]] = (note[0], note[3], note[MID])
else:
# a duplicate or changed schema - safe to update?
dupes += 1
if self.allowUpdate:
oldNid, oldMod, oldMid = self._notes[note[GUID]]
# will update if incoming note more recent
if oldMod < note[MOD]:
# safe if note types identical
if oldMid == note[MID]:
# incoming note should use existing id
note[0] = oldNid
note[4] = usn
note[6] = self._mungeMedia(note[MID], note[6])
update.append(note)
dirty.append(note[0])
else:
dupesIgnored.append("%s: %s" % (
self.col.models.get(oldMid)['name'],
note[6].replace("\x1f", ",")
))
self._ignoredGuids[note[GUID]] = True
if dupes:
up = len(update)
self.log.append(_("Updated %(a)d of %(b)d existing notes.") % dict(
a=len(update), b=dupes))
if dupesIgnored:
self.log.append(
_("Some updates were ignored because note type has changed:"))
self.log.extend(dupesIgnored)
newUpdate = []
for row in update:
oldnote = mw.col.getNote(row[0])
newTags = [t for t in row[5].replace('\u3000', ' ').split(" ") if t]
for tag in oldnote.tags:
for i in newTags:
if i.lower() == tag.lower():
tag = i
newTags.append(tag)
newTags = set(newTags)
togetherTags = " %s " % " ".join(newTags)
if str(row[2]) in midCheck:
# if this note belongs to a model with "Special Field"
trow = list(row)
for i in SPECIAL_FIELD:
try:
row = list(row)
items = mw.col.getNote(row[0]).items()
fieldOrd = [item for item in items if item[0] == i]
fieldOrd = items.index(fieldOrd[0])
fields = [item[1] for item in items]
splitRow = row[6].split("\x1f")
# valueLocal = mw.col.getNote(row[0]).values()
# splitRow[indexOfField] = valueLocal[indexOfField]
finalrow = ''
count = 0
for a in splitRow:
if count == fieldOrd:
finalrow += str(fields[fieldOrd]) + "\x1f"
else:
finalrow += a+"\x1f"
count = count + 1
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
finarow = rreplace(finalrow, """\x1f""", '', 1)
row[6] = str(finarow)
row = tuple(row)
# if row[0] == 1558556384609: #FOR TROUBLE SHOOTING ! Change to the card.id you are uncertain about
# showInfo("%s"%str(splitRow))
# showInfo("%s"%str(indexOfField))
# showInfo("%s"%str(valueLocal))
except:
pass
if COMBINE_TAGGING:
row = list(row)
row[5] = togetherTags
row = tuple(row)
newUpdate.append(row)
self.dupes = dupes
self.added = len(add)
self.updated = len(update)
# add to col
self.dst.db.executemany(
"insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)",
add)
self.dst.db.executemany(
"insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)",
newUpdate)
self.dst.updateFieldCache(dirty)
self.dst.tags.registerNotes(dirty)
Anki2Importer._importNotes = newImportNotes
AnkiExporter.exportInto = newExportInto