From ddc01ad8e81ab84fa1651208a6e695e5e635ba72 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 22 May 2013 12:27:37 +0900 Subject: [PATCH] log deletions in text file --- anki/collection.py | 3 ++- aqt/main.py | 28 ++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/anki/collection.py b/anki/collection.py index ba7975d37..fe2e9290b 100644 --- a/anki/collection.py +++ b/anki/collection.py @@ -6,7 +6,7 @@ import time, os, random, stat, datetime, copy from anki.lang import _, ngettext from anki.utils import ids2str, fieldChecksum, stripHTML, \ intTime, splitFields, joinFields, maxID, json -from anki.hooks import runFilter +from anki.hooks import runFilter, runHook from anki.sched import Scheduler from anki.models import ModelManager from anki.media import MediaManager @@ -258,6 +258,7 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""", strids = ids2str(ids) # we need to log these independently of cards, as one side may have # more card templates + runHook("remNotes", ids) self._logRem(ids, REM_NOTE) self.db.execute("delete from notes where id in %s" % strids) diff --git a/aqt/main.py b/aqt/main.py index 8514e9824..ae781c7b6 100644 --- a/aqt/main.py +++ b/aqt/main.py @@ -8,7 +8,7 @@ import zipfile from aqt.qt import * 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 import aqt, aqt.progress, aqt.webview, aqt.toolbar, aqt.stats @@ -74,7 +74,7 @@ class AnkiQt(QMainWindow): self.setupErrorHandler() self.setupSignals() self.setupAutoUpdate() - self.setupSchema() + self.setupHooks() self.setupRefreshTimer() self.updateTitleBar() # screens @@ -821,11 +821,31 @@ the problem and restart Anki.""") elif self.state == "overview": self.overview.refresh() - # Schema modifications + # Permanent libanki hooks ########################################################################## - def setupSchema(self): + def setupHooks(self): 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): # if triggered in sync, make sure we don't use the gui