log deletions in text file

This commit is contained in:
Damien Elmes 2013-05-22 12:27:37 +09:00
parent 30507d9e29
commit ddc01ad8e8
2 changed files with 26 additions and 5 deletions

View file

@ -6,7 +6,7 @@ import time, os, random, stat, datetime, copy
from anki.lang import _, ngettext from anki.lang import _, ngettext
from anki.utils import ids2str, fieldChecksum, stripHTML, \ from anki.utils import ids2str, fieldChecksum, stripHTML, \
intTime, splitFields, joinFields, maxID, json intTime, splitFields, joinFields, maxID, json
from anki.hooks import runFilter from anki.hooks import runFilter, runHook
from anki.sched import Scheduler from anki.sched import Scheduler
from anki.models import ModelManager from anki.models import ModelManager
from anki.media import MediaManager from anki.media import MediaManager
@ -258,6 +258,7 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""",
strids = ids2str(ids) strids = ids2str(ids)
# we need to log these independently of cards, as one side may have # we need to log these independently of cards, as one side may have
# more card templates # more card templates
runHook("remNotes", ids)
self._logRem(ids, REM_NOTE) self._logRem(ids, REM_NOTE)
self.db.execute("delete from notes where id in %s" % strids) self.db.execute("delete from notes where id in %s" % strids)

View file

@ -8,7 +8,7 @@ import zipfile
from aqt.qt import * from aqt.qt import *
from anki import Collection from anki import Collection
from anki.utils import isWin, isMac, intTime from anki.utils import isWin, isMac, intTime, splitFields, ids2str
from anki.hooks import runHook, addHook from anki.hooks import runHook, addHook
import aqt, aqt.progress, aqt.webview, aqt.toolbar, aqt.stats import aqt, aqt.progress, aqt.webview, aqt.toolbar, aqt.stats
@ -74,7 +74,7 @@ class AnkiQt(QMainWindow):
self.setupErrorHandler() self.setupErrorHandler()
self.setupSignals() self.setupSignals()
self.setupAutoUpdate() self.setupAutoUpdate()
self.setupSchema() self.setupHooks()
self.setupRefreshTimer() self.setupRefreshTimer()
self.updateTitleBar() self.updateTitleBar()
# screens # screens
@ -821,11 +821,31 @@ the problem and restart Anki.""")
elif self.state == "overview": elif self.state == "overview":
self.overview.refresh() self.overview.refresh()
# Schema modifications # Permanent libanki hooks
########################################################################## ##########################################################################
def setupSchema(self): def setupHooks(self):
addHook("modSchema", self.onSchemaMod) addHook("modSchema", self.onSchemaMod)
addHook("remNotes", self.onRemNotes)
# Log note deletion
##########################################################################
def onRemNotes(self, nids):
path = os.path.join(self.pm.profileFolder(), "deleted.txt")
existed = os.path.exists(path)
with open(path, "a") as f:
if not existed:
f.write("nid\tmid\tfields\n")
for id, mid, flds in self.col.db.execute(
"select id, mid, flds from notes where id in %s" %
ids2str(nids)):
fields = splitFields(flds)
f.write(("\t".join([str(id), str(mid)] + fields)).encode("utf8"))
f.write("\n")
# Schema modifications
##########################################################################
def onSchemaMod(self, arg): def onSchemaMod(self, arg):
# if triggered in sync, make sure we don't use the gui # if triggered in sync, make sure we don't use the gui