remove progress handling code; we'll do it in the GUI or provide cb

This commit is contained in:
Damien Elmes 2011-03-14 20:58:11 +09:00
parent 201e27dd58
commit 511d6e89a1
9 changed files with 13 additions and 124 deletions

View file

@ -12,7 +12,6 @@ except ImportError:
raise Exception("Please install pysqlite2 or python2.5") raise Exception("Please install pysqlite2 or python2.5")
from anki.hooks import runHook from anki.hooks import runHook
#FIXME: do we need the dbFinished hook?
class DB(object): class DB(object):
def __init__(self, path, level="EXCLUSIVE", text=None): def __init__(self, path, level="EXCLUSIVE", text=None):
@ -32,18 +31,24 @@ class DB(object):
else: else:
# execute("...where id = ?", 5) # execute("...where id = ?", 5)
res = self._db.execute(sql, a) res = self._db.execute(sql, a)
runHook("dbFinished")
return res return res
def executemany(self, sql, l): def executemany(self, sql, l):
if self.echo: if self.echo:
print sql, l print sql, l
self._db.executemany(sql, l) self._db.executemany(sql, l)
runHook("dbFinished")
def commit(self): def commit(self):
self._db.commit() 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): def scalar(self, *a, **kw):
res = self.execute(*a, **kw).fetchone() res = self.execute(*a, **kw).fetchone()
if res: if res:
@ -62,15 +67,6 @@ class DB(object):
def list(self, *a, **kw): def list(self, *a, **kw):
return [x[0] for x in self.execute(*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): def close(self):
self._db.close() self._db.close()

View file

@ -313,7 +313,6 @@ select id from facts where id not in (select distinct fid from cards)""")
if not ids: if not ids:
return return
sids = ids2str(ids) sids = ids2str(ids)
self.startProgress()
if self.schemaDirty(): if self.schemaDirty():
# immediate delete? # immediate delete?
self.db.execute("delete from cards where id in %s" % sids) 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()) intTime())
self.db.execute("delete from fsums where fid in "+sfids) self.db.execute("delete from fsums where fid in "+sfids)
self.db.execute("delete from revlog where cid in "+sids) self.db.execute("delete from revlog where cid in "+sids)
self.finishProgress()
def emptyTrash(self): def emptyTrash(self):
self.db.executescript(""" 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): def addTags(self, ids, tags, add=True):
"Add tags in bulk. TAGS is space-separated." "Add tags in bulk. TAGS is space-separated."
self.startProgress()
newTags = parseTags(tags) newTags = parseTags(tags)
# cache tag names # cache tag names
self.registerTags(newTags) 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 facts set tags = :t, mod = :n where id = :id""", [fix(row) for row in res])
# update q/a cache # update q/a cache
self.registerTags(parseTags(tags)) self.registerTags(parseTags(tags))
self.finishProgress()
def delTags(self, ids, tags): def delTags(self, ids, tags):
self.addTags(ids, tags, False) 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 import anki.find
return anki.find.findDuplicates(self, fmids) 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 # Timeboxing
########################################################################## ##########################################################################
@ -806,19 +767,11 @@ insert into undoLog values (null, 'insert into %(t)s (rowid""" % {'t': table}
sql = self.db.list(""" sql = self.db.list("""
select sql from undoLog where select sql from undoLog where
seq > :s and seq <= :e order by seq desc""", s=start, e=end) 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() newstart = self._latestUndoRow()
for c, s in enumerate(sql): for c, s in enumerate(sql):
if mod and not c % mod:
self.updateProgress()
self.engine.execute(s) self.engine.execute(s)
newend = self._latestUndoRow() newend = self._latestUndoRow()
dst.append([u[0], newstart, newend]) dst.append([u[0], newstart, newend])
if mod:
self.finishProgress()
def undo(self): def undo(self):
"Undo the last action(s)." "Undo the last action(s)."
@ -838,8 +791,6 @@ seq > :s and seq <= :e order by seq desc""", s=start, e=end)
problems = [] problems = []
self.save() self.save()
self.resetUndo() self.resetUndo()
self.startProgress()
self.updateProgress(_("Checking database..."))
oldSize = os.stat(self.path)[stat.ST_SIZE] oldSize = os.stat(self.path)[stat.ST_SIZE]
self.modSchema() self.modSchema()
# tags # tags
@ -857,7 +808,6 @@ seq > :s and seq <= :e order by seq desc""", s=start, e=end)
txt += "\n" + _("Saved %dKB.") % save txt += "\n" + _("Saved %dKB.") % save
problems.append(txt) problems.append(txt)
self.save() self.save()
self.finishProgress()
return "\n".join(problems) return "\n".join(problems)
def optimize(self): def optimize(self):

View file

@ -30,8 +30,6 @@ class Exporter(object):
if removeFields: if removeFields:
# beautifulsoup is slow # beautifulsoup is slow
self._escapeCount += 1 self._escapeCount += 1
if self._escapeCount % 100 == 0:
self.deck.updateProgress()
try: try:
s = BS(text) s = BS(text)
all = s('span', {'class': re.compile("fm.*")}) all = s('span', {'class': re.compile("fm.*")})
@ -70,8 +68,6 @@ class AnkiExporter(Exporter):
n = 3 n = 3
if not self.includeSchedulingInfo: if not self.includeSchedulingInfo:
n += 1 n += 1
self.deck.startProgress(n)
self.deck.updateProgress(_("Exporting..."))
try: try:
os.unlink(path) os.unlink(path)
except (IOError, OSError): except (IOError, OSError):
@ -86,12 +82,9 @@ class AnkiExporter(Exporter):
# set up a custom change list and sync # set up a custom change list and sync
lsum = self.localSummary() lsum = self.localSummary()
rsum = server.summary(0) rsum = server.summary(0)
self.deck.updateProgress()
payload = client.genPayload((lsum, rsum)) payload = client.genPayload((lsum, rsum))
self.deck.updateProgress()
res = server.applyPayload(payload) res = server.applyPayload(payload)
if not self.includeSchedulingInfo: if not self.includeSchedulingInfo:
self.deck.updateProgress()
self.newDeck.resetCards() self.newDeck.resetCards()
# media # media
if self.includeMedia: if self.includeMedia:
@ -104,7 +97,6 @@ class AnkiExporter(Exporter):
self.newDeck.utcOffset = -1 self.newDeck.utcOffset = -1
self.newDeck.db.commit() self.newDeck.db.commit()
self.newDeck.close() self.newDeck.close()
self.deck.finishProgress()
def localSummary(self): def localSummary(self):
cardIds = self.cardIds() cardIds = self.cardIds()
@ -149,13 +141,10 @@ class TextCardExporter(Exporter):
def doExport(self, file): def doExport(self, file):
ids = self.cardIds() ids = self.cardIds()
strids = ids2str(ids) strids = ids2str(ids)
self.deck.startProgress((len(ids) + 1) / 50)
self.deck.updateProgress(_("Exporting..."))
cards = self.deck.db.all(""" cards = self.deck.db.all("""
select cards.question, cards.answer, cards.id from cards select cards.question, cards.answer, cards.id from cards
where cards.id in %s where cards.id in %s
order by cards.created""" % strids) order by cards.created""" % strids)
self.deck.updateProgress()
if self.includeTags: if self.includeTags:
self.cardTags = dict(self.deck.db.all(""" self.cardTags = dict(self.deck.db.all("""
select cards.id, facts.tags from cards, facts select cards.id, facts.tags from cards, facts
@ -188,8 +177,6 @@ class TextFactExporter(Exporter):
def doExport(self, file): def doExport(self, file):
cardIds = self.cardIds() cardIds = self.cardIds()
self.deck.startProgress()
self.deck.updateProgress(_("Exporting..."))
facts = self.deck.db.all(""" facts = self.deck.db.all("""
select factId, value, facts.created from facts, fields select factId, value, facts.created from facts, fields
where where
@ -199,7 +186,6 @@ where cards.id in %s)
and facts.id = fields.factId and facts.id = fields.factId
order by factId, ordinal""" % ids2str(cardIds)) order by factId, ordinal""" % ids2str(cardIds))
txt = "" txt = ""
self.deck.updateProgress()
if self.includeTags: if self.includeTags:
self.factTags = dict(self.deck.db.all( self.factTags = dict(self.deck.db.all(
"select id, tags from facts where id in %s" % "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]) + "\t".join([self.escapeText(x[1]) for x in group]) +
self.tags(group[0][0])) self.tags(group[0][0]))
for group in groups] for group in groups]
self.deck.updateProgress()
groups.sort(key=itemgetter(0)) groups.sort(key=itemgetter(0))
out = [ret[1] for ret in groups] out = [ret[1] for ret in groups]
self.count = len(out) self.count = len(out)

View file

@ -56,32 +56,23 @@ class Importer(object):
num = 6 num = 6
if random: if random:
num += 1 num += 1
self.deck.startProgress(num)
self.deck.updateProgress(_("Importing..."))
c = self.foreignCards() c = self.foreignCards()
if self.importCards(c): if self.importCards(c):
self.deck.updateProgress()
self.deck.updateCardTags(self.cardIds) self.deck.updateCardTags(self.cardIds)
if random: if random:
self.deck.updateProgress()
self.deck.randomizeNewCards(self.cardIds) self.deck.randomizeNewCards(self.cardIds)
self.deck.finishProgress()
if c: if c:
self.deck.setModified() self.deck.setModified()
def doUpdate(self): def doUpdate(self):
self.deck.startProgress(7)
# grab the data from the external file # grab the data from the external file
self.deck.updateProgress(_("Updating..."))
cards = self.foreignCards() cards = self.foreignCards()
# grab data from db # grab data from db
self.deck.updateProgress()
fields = self.deck.db.all(""" fields = self.deck.db.all("""
select factId, value from fields where fieldModelId = :id select factId, value from fields where fieldModelId = :id
and value != ''""", and value != ''""",
id=self.updateKey[1]) id=self.updateKey[1])
# hash it # hash it
self.deck.updateProgress()
vhash = {} vhash = {}
fids = [] fids = []
for (fid, val) in fields: for (fid, val) in fields:
@ -96,7 +87,6 @@ and value != ''""",
except ValueError: except ValueError:
pass pass
# look for matches # look for matches
self.deck.updateProgress()
upcards = [] upcards = []
newcards = [] newcards = []
for c in cards: for c in cards:
@ -127,7 +117,6 @@ and value != ''""",
update fields set value = :v, chksum = :chk where factId = :fid update fields set value = :v, chksum = :chk where factId = :fid
and fieldModelId = :fmid""", data) and fieldModelId = :fmid""", data)
# update tags # update tags
self.deck.updateProgress()
if tagsIdx is not None: if tagsIdx is not None:
data = [{'fid': fid, data = [{'fid': fid,
't': c.fields[tagsIdx]} 't': c.fields[tagsIdx]}
@ -136,16 +125,13 @@ and fieldModelId = :fmid""", data)
"update facts set tags = :t where id = :fid", "update facts set tags = :t where id = :fid",
data) data)
# rebuild caches # rebuild caches
self.deck.updateProgress()
cids = self.deck.db.column0( cids = self.deck.db.column0(
"select id from cards where factId in %s" % "select id from cards where factId in %s" %
ids2str(fids)) ids2str(fids))
self.deck.updateCardTags(cids) self.deck.updateCardTags(cids)
self.deck.updateProgress()
self.deck.updateCardsFromFactIds(fids) self.deck.updateCardsFromFactIds(fids)
self.total = len(cards) self.total = len(cards)
self.deck.setModified() self.deck.setModified()
self.deck.finishProgress()
def fields(self): def fields(self):
"The number of fields." "The number of fields."
@ -227,7 +213,6 @@ The current importer only supports a single active card template. Please disable
except ValueError: except ValueError:
pass pass
# add facts # add facts
self.deck.updateProgress()
factIds = [genID() for n in range(len(cards))] factIds = [genID() for n in range(len(cards))]
factCreated = {} factCreated = {}
def fudgeCreated(d, tmp=[]): def fudgeCreated(d, tmp=[]):
@ -246,7 +231,6 @@ The current importer only supports a single active card template. Please disable
delete from factsDeleted delete from factsDeleted
where factId in (%s)""" % ",".join([str(s) for s in factIds])) where factId in (%s)""" % ",".join([str(s) for s in factIds]))
# add all the fields # add all the fields
self.deck.updateProgress()
for fm in self.model.fieldModels: for fm in self.model.fieldModels:
try: try:
index = self.mapping.index(fm) 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(), self.deck.db.execute(fieldsTable.insert(),
data) data)
# and cards # and cards
self.deck.updateProgress()
active = 0 active = 0
for cm in self.model.cardModels: for cm in self.model.cardModels:
if cm.active: 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))] },cards[m]) for m in range(len(cards))]
self.deck.db.execute(cardsTable.insert(), self.deck.db.execute(cardsTable.insert(),
data) data)
self.deck.updateProgress()
self.deck.updateCardsFromFactIds(factIds) self.deck.updateCardsFromFactIds(factIds)
self.total = len(factIds) self.total = len(factIds)

View file

@ -20,8 +20,6 @@ class Anki10Importer(Importer):
num = 4 num = 4
if random: if random:
num += 1 num += 1
self.deck.startProgress(num)
self.deck.updateProgress(_("Importing..."))
src = DeckStorage.Deck(self.file, backup=False) src = DeckStorage.Deck(self.file, backup=False)
client = SyncClient(self.deck) client = SyncClient(self.deck)
server = SyncServer(src) server = SyncServer(src)
@ -47,13 +45,10 @@ class Anki10Importer(Importer):
assert payload['deleted-facts'] == [] assert payload['deleted-facts'] == []
assert payload['deleted-cards'] == [] assert payload['deleted-cards'] == []
assert payload['deleted-models'] == [] assert payload['deleted-models'] == []
self.deck.updateProgress()
res = server.applyPayload(payload) res = server.applyPayload(payload)
self.deck.updateProgress()
client.applyPayloadReply(res) client.applyPayloadReply(res)
copyLocalMedia(server.deck, client.deck) copyLocalMedia(server.deck, client.deck)
# add tags # add tags
self.deck.updateProgress()
fids = [f[0] for f in res['added-facts']['facts']] fids = [f[0] for f in res['added-facts']['facts']]
self.deck.addTags(fids, self.tagsToAdd) self.deck.addTags(fids, self.tagsToAdd)
# mark import material as newly added # mark import material as newly added
@ -72,10 +67,8 @@ class Anki10Importer(Importer):
src.engine.dispose() src.engine.dispose()
# randomize? # randomize?
if random: if random:
self.deck.updateProgress()
self.deck.randomizeNewCards([x[0] for x in res['added-cards']]) self.deck.randomizeNewCards([x[0] for x in res['added-cards']])
self.deck.flushMod() self.deck.flushMod()
self.deck.finishProgress()
def _clearDeleted(self, sum): def _clearDeleted(self, sum):
sum['delcards'] = [] sum['delcards'] = []

View file

@ -289,7 +289,7 @@ class SupermemoXmlImporter(Importer):
dLevels={0:'',1:u'Info',2:u'Verbose',3:u'Debug'} dLevels={0:'',1:u'Info',2:u'Verbose',3:u'Debug'}
if level<=self.META.loggerLevel: if level<=self.META.loggerLevel:
self.deck.updateProgress(_(text)) #self.deck.updateProgress(_(text))
if self.META.logToStdOutput: if self.META.logToStdOutput:
print self.__class__.__name__+ u" - " + dLevels[level].ljust(9) +u' -\t'+ _(text) print self.__class__.__name__+ u" - " + dLevels[level].ljust(9) +u' -\t'+ _(text)

View file

@ -167,7 +167,6 @@ If a file with the same name exists, return a unique name."""
mdir = self.mediaDir() mdir = self.mediaDir()
if not mdir: if not mdir:
return (0, 0) return (0, 0)
self.deck.startProgress()
# delete all media entries in database # delete all media entries in database
self.deck.db.execute("delete from media") self.deck.db.execute("delete from media")
# look through cards for media references # 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) os.unlink(path)
nohave = self.deck.db.list( nohave = self.deck.db.list(
"select file from media where csum = ''") "select file from media where csum = ''")
self.deck.finishProgress()
return (nohave, unused) return (nohave, unused)
# Download missing # Download missing
@ -210,7 +208,6 @@ If a file with the same name exists, return a unique name."""
if not urlbase: if not urlbase:
return None return None
mdir = self.deck.mediaDir(create=True) mdir = self.deck.mediaDir(create=True)
self.deck.startProgress()
missing = 0 missing = 0
grabbed = 0 grabbed = 0
for c, (f, sum) in enumerate(self.deck.db.all( 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: except:
if sum: if sum:
# the file is supposed to exist # the file is supposed to exist
self.deck.finishProgress()
return (False, rpath) return (False, rpath)
else: else:
# ignore and keep going # ignore and keep going
missing += 1 missing += 1
self.deck.updateProgress(label=_("File %d...") % (grabbed+missing)) #self.deck.updateProgress(label=_("File %d...") % (grabbed+missing))
self.deck.finishProgress()
return (True, grabbed, missing) return (True, grabbed, missing)
# Convert remote links to local ones # 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): def downloadRemote(self):
mdir = self.deck.mediaDir(create=True) mdir = self.deck.mediaDir(create=True)
refs = {} refs = {}
self.deck.startProgress()
for (question, answer) in self.deck.db.all( for (question, answer) in self.deck.db.all(
"select question, answer from cards"): "select question, answer from cards"):
for txt in (question, answer): 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]) passed.append([link, newpath])
except: except:
failed.append(link) failed.append(link)
self.deck.updateProgress(label=_("Download %d...") % c) #self.deck.updateProgress(label=_("Download %d...") % c)
for (url, name) in passed: for (url, name) in passed:
self.deck.db.execute( self.deck.db.execute(
"update fields set value = replace(value, :url, :name)", "update fields set value = replace(value, :url, :name)",
url=url, name=name) url=url, name=name)
self.deck.updateProgress(label=_("Updating references...")) #self.deck.updateProgress(label=_("Updating references..."))
self.deck.updateProgress(label=_("Updating cards...")) #self.deck.updateProgress(label=_("Updating cards..."))
# rebuild entire q/a cache # rebuild entire q/a cache
for m in self.deck.models: for m in self.deck.models:
self.deck.updateCardsFromModel(m, dirty=True) self.deck.updateCardsFromModel(m, dirty=True)
self.deck.finishProgress()
return (passed, failed) return (passed, failed)

View file

@ -214,14 +214,12 @@ insert or replace into models values (?, ?, ?, ?, ?, ?, ?)""",
f['ord'] = c f['ord'] = c
def _transformFields(self, fn): def _transformFields(self, fn):
self.deck.startProgress()
self.deck.modSchema() self.deck.modSchema()
r = [] r = []
for (id, flds) in self.deck.db.execute( for (id, flds) in self.deck.db.execute(
"select id, flds from facts where mid = ?", self.id): "select id, flds from facts where mid = ?", self.id):
r.append((joinFields(fn(splitFields(flds))), id)) r.append((joinFields(fn(splitFields(flds))), id))
self.deck.db.executemany("update facts set flds = ? where id = ?", r) self.deck.db.executemany("update facts set flds = ? where id = ?", r)
self.deck.finishProgress()
# Templates # Templates
################################################## ##################################################
@ -261,7 +259,6 @@ where mid = ?) and ord > ?""", self.id, ord)
raise Exception() raise Exception()
self.modSchema() self.modSchema()
sfids = ids2str(fids) sfids = ids2str(fids)
self.startProgress()
# field remapping # field remapping
if fieldMap: if fieldMap:
seen = {} seen = {}
@ -298,9 +295,7 @@ update facts set
mod = :t, mod = :t,
mid = :id mid = :id
where id in %s""" % sfids, t=time.time(), id=newModel.id) where id in %s""" % sfids, t=time.time(), id=newModel.id)
self.finishProgress()
# template remapping # template remapping
self.startProgress(len(cardMap)+3)
toChange = [] toChange = []
for (old, new) in cardMap.items(): for (old, new) in cardMap.items():
if not new: if not new:
@ -324,4 +319,3 @@ where id in %s""" % ids2str(ids), new=new.id, ord=new.ord)
cardIds = self.db.list( cardIds = self.db.list(
"select id from cards where fid in %s" % "select id from cards where fid in %s" %
ids2str(fids)) ids2str(fids))
self.finishProgress()

View file

@ -703,21 +703,17 @@ limit 1""" % self.delay0))
def suspendCards(self, ids): def suspendCards(self, ids):
"Suspend cards." "Suspend cards."
self.startProgress()
self.db.execute(""" self.db.execute("""
update cards update cards
set queue = -1, mod = :t set queue = -1, mod = :t
where id in %s""" % ids2str(ids), t=time.time()) where id in %s""" % ids2str(ids), t=time.time())
self.finishProgress()
def unsuspendCards(self, ids): def unsuspendCards(self, ids):
"Unsuspend cards." "Unsuspend cards."
self.startProgress()
self.db.execute(""" self.db.execute("""
update cards set queue = type, mod=:t update cards set queue = type, mod=:t
where queue = -1 and id in %s""" % where queue = -1 and id in %s""" %
ids2str(ids), t=time.time()) ids2str(ids), t=time.time())
self.finishProgress()
def buryFact(self, fact): def buryFact(self, fact):
"Bury all cards for fact until next session." "Bury all cards for fact until next session."