mirror of
https://github.com/ankitects/anki.git
synced 2025-09-25 09:16:38 -04:00
remove progress handling code; we'll do it in the GUI or provide cb
This commit is contained in:
parent
201e27dd58
commit
511d6e89a1
9 changed files with 13 additions and 124 deletions
20
anki/db.py
20
anki/db.py
|
@ -12,7 +12,6 @@ except ImportError:
|
|||
raise Exception("Please install pysqlite2 or python2.5")
|
||||
|
||||
from anki.hooks import runHook
|
||||
#FIXME: do we need the dbFinished hook?
|
||||
|
||||
class DB(object):
|
||||
def __init__(self, path, level="EXCLUSIVE", text=None):
|
||||
|
@ -32,18 +31,24 @@ class DB(object):
|
|||
else:
|
||||
# execute("...where id = ?", 5)
|
||||
res = self._db.execute(sql, a)
|
||||
runHook("dbFinished")
|
||||
return res
|
||||
|
||||
def executemany(self, sql, l):
|
||||
if self.echo:
|
||||
print sql, l
|
||||
self._db.executemany(sql, l)
|
||||
runHook("dbFinished")
|
||||
|
||||
def commit(self):
|
||||
self._db.commit()
|
||||
|
||||
def executescript(self, sql):
|
||||
if self.echo:
|
||||
print sql
|
||||
self._db.executescript(sql)
|
||||
|
||||
def rollback(self):
|
||||
self._db.rollback()
|
||||
|
||||
def scalar(self, *a, **kw):
|
||||
res = self.execute(*a, **kw).fetchone()
|
||||
if res:
|
||||
|
@ -62,15 +67,6 @@ class DB(object):
|
|||
def list(self, *a, **kw):
|
||||
return [x[0] for x in self.execute(*a, **kw)]
|
||||
|
||||
def executescript(self, sql):
|
||||
if self.echo:
|
||||
print sql
|
||||
self._db.executescript(sql)
|
||||
runHook("dbFinished")
|
||||
|
||||
def rollback(self):
|
||||
self._db.rollback()
|
||||
|
||||
def close(self):
|
||||
self._db.close()
|
||||
|
||||
|
|
50
anki/deck.py
50
anki/deck.py
|
@ -313,7 +313,6 @@ select id from facts where id not in (select distinct fid from cards)""")
|
|||
if not ids:
|
||||
return
|
||||
sids = ids2str(ids)
|
||||
self.startProgress()
|
||||
if self.schemaDirty():
|
||||
# immediate delete?
|
||||
self.db.execute("delete from cards where id in %s" % sids)
|
||||
|
@ -333,7 +332,6 @@ select id from facts where id not in (select distinct fid from cards)""")
|
|||
intTime())
|
||||
self.db.execute("delete from fsums where fid in "+sfids)
|
||||
self.db.execute("delete from revlog where cid in "+sids)
|
||||
self.finishProgress()
|
||||
|
||||
def emptyTrash(self):
|
||||
self.db.executescript("""
|
||||
|
@ -559,7 +557,6 @@ insert or ignore into tags (mod, name) values (%d, :t)""" % intTime(),
|
|||
|
||||
def addTags(self, ids, tags, add=True):
|
||||
"Add tags in bulk. TAGS is space-separated."
|
||||
self.startProgress()
|
||||
newTags = parseTags(tags)
|
||||
# cache tag names
|
||||
self.registerTags(newTags)
|
||||
|
@ -584,7 +581,6 @@ insert or ignore into tags (mod, name) values (%d, :t)""" % intTime(),
|
|||
update facts set tags = :t, mod = :n where id = :id""", [fix(row) for row in res])
|
||||
# update q/a cache
|
||||
self.registerTags(parseTags(tags))
|
||||
self.finishProgress()
|
||||
|
||||
def delTags(self, ids, tags):
|
||||
self.addTags(ids, tags, False)
|
||||
|
@ -604,41 +600,6 @@ update facts set tags = :t, mod = :n where id = :id""", [fix(row) for row in res
|
|||
import anki.find
|
||||
return anki.find.findDuplicates(self, fmids)
|
||||
|
||||
# Progress info
|
||||
##########################################################################
|
||||
|
||||
def startProgress(self, max=0, min=0, title=None):
|
||||
self.enableProgressHandler()
|
||||
runHook("startProgress", max, min, title)
|
||||
|
||||
def updateProgress(self, label=None, value=None):
|
||||
runHook("updateProgress", label, value)
|
||||
|
||||
def finishProgress(self):
|
||||
runHook("updateProgress")
|
||||
runHook("finishProgress")
|
||||
self.disableProgressHandler()
|
||||
|
||||
def progressHandler(self):
|
||||
if (time.time() - self.progressHandlerCalled) < 0.2:
|
||||
return
|
||||
self.progressHandlerCalled = time.time()
|
||||
if self.progressHandlerEnabled:
|
||||
# things which hook on this should be very careful not to touch
|
||||
# the db as they run
|
||||
runHook("dbProgress")
|
||||
|
||||
def setupProgressHandler(self):
|
||||
self.progressHandlerCalled = 0
|
||||
self.progressHandlerEnabled = False
|
||||
self.db.set_progress_handler(self.progressHandler, 100000)
|
||||
|
||||
def enableProgressHandler(self):
|
||||
self.progressHandlerEnabled = True
|
||||
|
||||
def disableProgressHandler(self):
|
||||
self.progressHandlerEnabled = False
|
||||
|
||||
# Timeboxing
|
||||
##########################################################################
|
||||
|
||||
|
@ -806,19 +767,11 @@ insert into undoLog values (null, 'insert into %(t)s (rowid""" % {'t': table}
|
|||
sql = self.db.list("""
|
||||
select sql from undoLog where
|
||||
seq > :s and seq <= :e order by seq desc""", s=start, e=end)
|
||||
mod = len(sql) / 35
|
||||
if mod:
|
||||
self.startProgress(36)
|
||||
self.updateProgress(_("Processing..."))
|
||||
newstart = self._latestUndoRow()
|
||||
for c, s in enumerate(sql):
|
||||
if mod and not c % mod:
|
||||
self.updateProgress()
|
||||
self.engine.execute(s)
|
||||
newend = self._latestUndoRow()
|
||||
dst.append([u[0], newstart, newend])
|
||||
if mod:
|
||||
self.finishProgress()
|
||||
|
||||
def undo(self):
|
||||
"Undo the last action(s)."
|
||||
|
@ -838,8 +791,6 @@ seq > :s and seq <= :e order by seq desc""", s=start, e=end)
|
|||
problems = []
|
||||
self.save()
|
||||
self.resetUndo()
|
||||
self.startProgress()
|
||||
self.updateProgress(_("Checking database..."))
|
||||
oldSize = os.stat(self.path)[stat.ST_SIZE]
|
||||
self.modSchema()
|
||||
# tags
|
||||
|
@ -857,7 +808,6 @@ seq > :s and seq <= :e order by seq desc""", s=start, e=end)
|
|||
txt += "\n" + _("Saved %dKB.") % save
|
||||
problems.append(txt)
|
||||
self.save()
|
||||
self.finishProgress()
|
||||
return "\n".join(problems)
|
||||
|
||||
def optimize(self):
|
||||
|
|
|
@ -30,8 +30,6 @@ class Exporter(object):
|
|||
if removeFields:
|
||||
# beautifulsoup is slow
|
||||
self._escapeCount += 1
|
||||
if self._escapeCount % 100 == 0:
|
||||
self.deck.updateProgress()
|
||||
try:
|
||||
s = BS(text)
|
||||
all = s('span', {'class': re.compile("fm.*")})
|
||||
|
@ -70,8 +68,6 @@ class AnkiExporter(Exporter):
|
|||
n = 3
|
||||
if not self.includeSchedulingInfo:
|
||||
n += 1
|
||||
self.deck.startProgress(n)
|
||||
self.deck.updateProgress(_("Exporting..."))
|
||||
try:
|
||||
os.unlink(path)
|
||||
except (IOError, OSError):
|
||||
|
@ -86,12 +82,9 @@ class AnkiExporter(Exporter):
|
|||
# set up a custom change list and sync
|
||||
lsum = self.localSummary()
|
||||
rsum = server.summary(0)
|
||||
self.deck.updateProgress()
|
||||
payload = client.genPayload((lsum, rsum))
|
||||
self.deck.updateProgress()
|
||||
res = server.applyPayload(payload)
|
||||
if not self.includeSchedulingInfo:
|
||||
self.deck.updateProgress()
|
||||
self.newDeck.resetCards()
|
||||
# media
|
||||
if self.includeMedia:
|
||||
|
@ -104,7 +97,6 @@ class AnkiExporter(Exporter):
|
|||
self.newDeck.utcOffset = -1
|
||||
self.newDeck.db.commit()
|
||||
self.newDeck.close()
|
||||
self.deck.finishProgress()
|
||||
|
||||
def localSummary(self):
|
||||
cardIds = self.cardIds()
|
||||
|
@ -149,13 +141,10 @@ class TextCardExporter(Exporter):
|
|||
def doExport(self, file):
|
||||
ids = self.cardIds()
|
||||
strids = ids2str(ids)
|
||||
self.deck.startProgress((len(ids) + 1) / 50)
|
||||
self.deck.updateProgress(_("Exporting..."))
|
||||
cards = self.deck.db.all("""
|
||||
select cards.question, cards.answer, cards.id from cards
|
||||
where cards.id in %s
|
||||
order by cards.created""" % strids)
|
||||
self.deck.updateProgress()
|
||||
if self.includeTags:
|
||||
self.cardTags = dict(self.deck.db.all("""
|
||||
select cards.id, facts.tags from cards, facts
|
||||
|
@ -188,8 +177,6 @@ class TextFactExporter(Exporter):
|
|||
|
||||
def doExport(self, file):
|
||||
cardIds = self.cardIds()
|
||||
self.deck.startProgress()
|
||||
self.deck.updateProgress(_("Exporting..."))
|
||||
facts = self.deck.db.all("""
|
||||
select factId, value, facts.created from facts, fields
|
||||
where
|
||||
|
@ -199,7 +186,6 @@ where cards.id in %s)
|
|||
and facts.id = fields.factId
|
||||
order by factId, ordinal""" % ids2str(cardIds))
|
||||
txt = ""
|
||||
self.deck.updateProgress()
|
||||
if self.includeTags:
|
||||
self.factTags = dict(self.deck.db.all(
|
||||
"select id, tags from facts where id in %s" %
|
||||
|
@ -210,7 +196,6 @@ order by factId, ordinal""" % ids2str(cardIds))
|
|||
"\t".join([self.escapeText(x[1]) for x in group]) +
|
||||
self.tags(group[0][0]))
|
||||
for group in groups]
|
||||
self.deck.updateProgress()
|
||||
groups.sort(key=itemgetter(0))
|
||||
out = [ret[1] for ret in groups]
|
||||
self.count = len(out)
|
||||
|
|
|
@ -56,32 +56,23 @@ class Importer(object):
|
|||
num = 6
|
||||
if random:
|
||||
num += 1
|
||||
self.deck.startProgress(num)
|
||||
self.deck.updateProgress(_("Importing..."))
|
||||
c = self.foreignCards()
|
||||
if self.importCards(c):
|
||||
self.deck.updateProgress()
|
||||
self.deck.updateCardTags(self.cardIds)
|
||||
if random:
|
||||
self.deck.updateProgress()
|
||||
self.deck.randomizeNewCards(self.cardIds)
|
||||
self.deck.finishProgress()
|
||||
if c:
|
||||
self.deck.setModified()
|
||||
|
||||
def doUpdate(self):
|
||||
self.deck.startProgress(7)
|
||||
# grab the data from the external file
|
||||
self.deck.updateProgress(_("Updating..."))
|
||||
cards = self.foreignCards()
|
||||
# grab data from db
|
||||
self.deck.updateProgress()
|
||||
fields = self.deck.db.all("""
|
||||
select factId, value from fields where fieldModelId = :id
|
||||
and value != ''""",
|
||||
id=self.updateKey[1])
|
||||
# hash it
|
||||
self.deck.updateProgress()
|
||||
vhash = {}
|
||||
fids = []
|
||||
for (fid, val) in fields:
|
||||
|
@ -96,7 +87,6 @@ and value != ''""",
|
|||
except ValueError:
|
||||
pass
|
||||
# look for matches
|
||||
self.deck.updateProgress()
|
||||
upcards = []
|
||||
newcards = []
|
||||
for c in cards:
|
||||
|
@ -127,7 +117,6 @@ and value != ''""",
|
|||
update fields set value = :v, chksum = :chk where factId = :fid
|
||||
and fieldModelId = :fmid""", data)
|
||||
# update tags
|
||||
self.deck.updateProgress()
|
||||
if tagsIdx is not None:
|
||||
data = [{'fid': fid,
|
||||
't': c.fields[tagsIdx]}
|
||||
|
@ -136,16 +125,13 @@ and fieldModelId = :fmid""", data)
|
|||
"update facts set tags = :t where id = :fid",
|
||||
data)
|
||||
# rebuild caches
|
||||
self.deck.updateProgress()
|
||||
cids = self.deck.db.column0(
|
||||
"select id from cards where factId in %s" %
|
||||
ids2str(fids))
|
||||
self.deck.updateCardTags(cids)
|
||||
self.deck.updateProgress()
|
||||
self.deck.updateCardsFromFactIds(fids)
|
||||
self.total = len(cards)
|
||||
self.deck.setModified()
|
||||
self.deck.finishProgress()
|
||||
|
||||
def fields(self):
|
||||
"The number of fields."
|
||||
|
@ -227,7 +213,6 @@ The current importer only supports a single active card template. Please disable
|
|||
except ValueError:
|
||||
pass
|
||||
# add facts
|
||||
self.deck.updateProgress()
|
||||
factIds = [genID() for n in range(len(cards))]
|
||||
factCreated = {}
|
||||
def fudgeCreated(d, tmp=[]):
|
||||
|
@ -246,7 +231,6 @@ The current importer only supports a single active card template. Please disable
|
|||
delete from factsDeleted
|
||||
where factId in (%s)""" % ",".join([str(s) for s in factIds]))
|
||||
# add all the fields
|
||||
self.deck.updateProgress()
|
||||
for fm in self.model.fieldModels:
|
||||
try:
|
||||
index = self.mapping.index(fm)
|
||||
|
@ -266,7 +250,6 @@ where factId in (%s)""" % ",".join([str(s) for s in factIds]))
|
|||
self.deck.db.execute(fieldsTable.insert(),
|
||||
data)
|
||||
# and cards
|
||||
self.deck.updateProgress()
|
||||
active = 0
|
||||
for cm in self.model.cardModels:
|
||||
if cm.active:
|
||||
|
@ -282,7 +265,6 @@ where factId in (%s)""" % ",".join([str(s) for s in factIds]))
|
|||
},cards[m]) for m in range(len(cards))]
|
||||
self.deck.db.execute(cardsTable.insert(),
|
||||
data)
|
||||
self.deck.updateProgress()
|
||||
self.deck.updateCardsFromFactIds(factIds)
|
||||
self.total = len(factIds)
|
||||
|
||||
|
|
|
@ -20,8 +20,6 @@ class Anki10Importer(Importer):
|
|||
num = 4
|
||||
if random:
|
||||
num += 1
|
||||
self.deck.startProgress(num)
|
||||
self.deck.updateProgress(_("Importing..."))
|
||||
src = DeckStorage.Deck(self.file, backup=False)
|
||||
client = SyncClient(self.deck)
|
||||
server = SyncServer(src)
|
||||
|
@ -47,13 +45,10 @@ class Anki10Importer(Importer):
|
|||
assert payload['deleted-facts'] == []
|
||||
assert payload['deleted-cards'] == []
|
||||
assert payload['deleted-models'] == []
|
||||
self.deck.updateProgress()
|
||||
res = server.applyPayload(payload)
|
||||
self.deck.updateProgress()
|
||||
client.applyPayloadReply(res)
|
||||
copyLocalMedia(server.deck, client.deck)
|
||||
# add tags
|
||||
self.deck.updateProgress()
|
||||
fids = [f[0] for f in res['added-facts']['facts']]
|
||||
self.deck.addTags(fids, self.tagsToAdd)
|
||||
# mark import material as newly added
|
||||
|
@ -72,10 +67,8 @@ class Anki10Importer(Importer):
|
|||
src.engine.dispose()
|
||||
# randomize?
|
||||
if random:
|
||||
self.deck.updateProgress()
|
||||
self.deck.randomizeNewCards([x[0] for x in res['added-cards']])
|
||||
self.deck.flushMod()
|
||||
self.deck.finishProgress()
|
||||
|
||||
def _clearDeleted(self, sum):
|
||||
sum['delcards'] = []
|
||||
|
|
|
@ -289,7 +289,7 @@ class SupermemoXmlImporter(Importer):
|
|||
|
||||
dLevels={0:'',1:u'Info',2:u'Verbose',3:u'Debug'}
|
||||
if level<=self.META.loggerLevel:
|
||||
self.deck.updateProgress(_(text))
|
||||
#self.deck.updateProgress(_(text))
|
||||
|
||||
if self.META.logToStdOutput:
|
||||
print self.__class__.__name__+ u" - " + dLevels[level].ljust(9) +u' -\t'+ _(text)
|
||||
|
|
|
@ -167,7 +167,6 @@ If a file with the same name exists, return a unique name."""
|
|||
mdir = self.mediaDir()
|
||||
if not mdir:
|
||||
return (0, 0)
|
||||
self.deck.startProgress()
|
||||
# delete all media entries in database
|
||||
self.deck.db.execute("delete from media")
|
||||
# look through cards for media references
|
||||
|
@ -199,7 +198,6 @@ If a file with the same name exists, return a unique name."""
|
|||
os.unlink(path)
|
||||
nohave = self.deck.db.list(
|
||||
"select file from media where csum = ''")
|
||||
self.deck.finishProgress()
|
||||
return (nohave, unused)
|
||||
|
||||
# Download missing
|
||||
|
@ -210,7 +208,6 @@ If a file with the same name exists, return a unique name."""
|
|||
if not urlbase:
|
||||
return None
|
||||
mdir = self.deck.mediaDir(create=True)
|
||||
self.deck.startProgress()
|
||||
missing = 0
|
||||
grabbed = 0
|
||||
for c, (f, sum) in enumerate(self.deck.db.all(
|
||||
|
@ -225,13 +222,11 @@ If a file with the same name exists, return a unique name."""
|
|||
except:
|
||||
if sum:
|
||||
# the file is supposed to exist
|
||||
self.deck.finishProgress()
|
||||
return (False, rpath)
|
||||
else:
|
||||
# ignore and keep going
|
||||
missing += 1
|
||||
self.deck.updateProgress(label=_("File %d...") % (grabbed+missing))
|
||||
self.deck.finishProgress()
|
||||
#self.deck.updateProgress(label=_("File %d...") % (grabbed+missing))
|
||||
return (True, grabbed, missing)
|
||||
|
||||
# Convert remote links to local ones
|
||||
|
@ -240,7 +235,6 @@ If a file with the same name exists, return a unique name."""
|
|||
def downloadRemote(self):
|
||||
mdir = self.deck.mediaDir(create=True)
|
||||
refs = {}
|
||||
self.deck.startProgress()
|
||||
for (question, answer) in self.deck.db.all(
|
||||
"select question, answer from cards"):
|
||||
for txt in (question, answer):
|
||||
|
@ -259,15 +253,14 @@ If a file with the same name exists, return a unique name."""
|
|||
passed.append([link, newpath])
|
||||
except:
|
||||
failed.append(link)
|
||||
self.deck.updateProgress(label=_("Download %d...") % c)
|
||||
#self.deck.updateProgress(label=_("Download %d...") % c)
|
||||
for (url, name) in passed:
|
||||
self.deck.db.execute(
|
||||
"update fields set value = replace(value, :url, :name)",
|
||||
url=url, name=name)
|
||||
self.deck.updateProgress(label=_("Updating references..."))
|
||||
self.deck.updateProgress(label=_("Updating cards..."))
|
||||
#self.deck.updateProgress(label=_("Updating references..."))
|
||||
#self.deck.updateProgress(label=_("Updating cards..."))
|
||||
# rebuild entire q/a cache
|
||||
for m in self.deck.models:
|
||||
self.deck.updateCardsFromModel(m, dirty=True)
|
||||
self.deck.finishProgress()
|
||||
return (passed, failed)
|
||||
|
|
|
@ -214,14 +214,12 @@ insert or replace into models values (?, ?, ?, ?, ?, ?, ?)""",
|
|||
f['ord'] = c
|
||||
|
||||
def _transformFields(self, fn):
|
||||
self.deck.startProgress()
|
||||
self.deck.modSchema()
|
||||
r = []
|
||||
for (id, flds) in self.deck.db.execute(
|
||||
"select id, flds from facts where mid = ?", self.id):
|
||||
r.append((joinFields(fn(splitFields(flds))), id))
|
||||
self.deck.db.executemany("update facts set flds = ? where id = ?", r)
|
||||
self.deck.finishProgress()
|
||||
|
||||
# Templates
|
||||
##################################################
|
||||
|
@ -261,7 +259,6 @@ where mid = ?) and ord > ?""", self.id, ord)
|
|||
raise Exception()
|
||||
self.modSchema()
|
||||
sfids = ids2str(fids)
|
||||
self.startProgress()
|
||||
# field remapping
|
||||
if fieldMap:
|
||||
seen = {}
|
||||
|
@ -298,9 +295,7 @@ update facts set
|
|||
mod = :t,
|
||||
mid = :id
|
||||
where id in %s""" % sfids, t=time.time(), id=newModel.id)
|
||||
self.finishProgress()
|
||||
# template remapping
|
||||
self.startProgress(len(cardMap)+3)
|
||||
toChange = []
|
||||
for (old, new) in cardMap.items():
|
||||
if not new:
|
||||
|
@ -324,4 +319,3 @@ where id in %s""" % ids2str(ids), new=new.id, ord=new.ord)
|
|||
cardIds = self.db.list(
|
||||
"select id from cards where fid in %s" %
|
||||
ids2str(fids))
|
||||
self.finishProgress()
|
||||
|
|
|
@ -703,21 +703,17 @@ limit 1""" % self.delay0))
|
|||
|
||||
def suspendCards(self, ids):
|
||||
"Suspend cards."
|
||||
self.startProgress()
|
||||
self.db.execute("""
|
||||
update cards
|
||||
set queue = -1, mod = :t
|
||||
where id in %s""" % ids2str(ids), t=time.time())
|
||||
self.finishProgress()
|
||||
|
||||
def unsuspendCards(self, ids):
|
||||
"Unsuspend cards."
|
||||
self.startProgress()
|
||||
self.db.execute("""
|
||||
update cards set queue = type, mod=:t
|
||||
where queue = -1 and id in %s""" %
|
||||
ids2str(ids), t=time.time())
|
||||
self.finishProgress()
|
||||
|
||||
def buryFact(self, fact):
|
||||
"Bury all cards for fact until next session."
|
||||
|
|
Loading…
Reference in a new issue