diff --git a/anki/deck.py b/anki/deck.py index 99764dd2a..b2c9842eb 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -125,12 +125,14 @@ crt=?, mod=?, scm=?, dty=?, lastSync=?, conf=?""", self.rollback() self.db.close() self.db = None + self.media.close() def reopen(self): "Reconnect to DB (after changing threads, etc). Doesn't reload." import anki.db if not self.db: self.db = anki.db.DB(self.path) + self.media.connect() def rollback(self): self.db.rollback() diff --git a/anki/media.py b/anki/media.py index d3800ba63..19a6aa48c 100644 --- a/anki/media.py +++ b/anki/media.py @@ -22,12 +22,19 @@ class MediaManager(object): os.makedirs(self._dir) os.chdir(self._dir) # change database - path = os.path.join(self.dir(), "media.db") + self.connect() + + def connect(self): + path = self.dir()+"db" create = not os.path.exists(path) self.db = DB(path) if create: self._initDB() + def close(self): + self.db.close() + self.db = None + def dir(self): return self._dir @@ -124,7 +131,7 @@ If the same name exists, compare checksums.""" # loop through directory and find unused & missing media unused = [] for file in os.listdir(mdir): - if file.startswith("latex-") or file.startswith("media.db"): + if file.startswith("latex-"): continue path = os.path.join(mdir, file) if not os.path.isfile(path): @@ -159,8 +166,8 @@ If the same name exists, compare checksums.""" # in the log, a mod time of zero indicates a delete self.db.executescript(""" create table media (fname text primary key, csum text, mod int); -create table meta (dirMod int); -insert into meta values (0); +create table meta (dirMod int, inSync int); +insert into meta values (0, 0); create table log (id int, fname text, mod int); create index ix_log_id on log (id); """) @@ -209,8 +216,8 @@ create index ix_log_id on log (id); changed = [] # loop through on-disk files for f in os.listdir(self.dir()): - # ignore our db and folders - if f.startswith("media.db") or os.path.isdir(f): + # ignore folders + if os.path.isdir(f): continue # newly added? if f not in self.cache: @@ -255,3 +262,5 @@ create index ix_log_id on log (id); self.db.executemany("delete from media where fname = ?", mediaRem) self.db.execute("update meta set dirMod = ?", self._mtime(self.dir())) + self.db.commit() + diff --git a/tests/test_latex.py b/tests/test_latex.py index d3e8c4457..e2b71ff42 100644 --- a/tests/test_latex.py +++ b/tests/test_latex.py @@ -14,8 +14,8 @@ def test_latex(): f = d.newFact() f['Front'] = u"[latex]hello[/latex]" d.addFact(f) - # but since latex couldn't run, it will only have the media.db - assert len(os.listdir(d.media.dir())) == 1 + # but since latex couldn't run, there's nothing there + assert len(os.listdir(d.media.dir())) == 0 # check the error message msg = f.cards()[0].q() assert "executing latex" in msg