From b242b06052203a68a53a05e7acaf8c9d3b03530d Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 21 Oct 2011 07:53:22 +0900 Subject: [PATCH] import media too --- anki/importing/anki2.py | 7 +++++++ anki/media.py | 11 +++++++++++ tests/test_upgrade.py | 5 ++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/anki/importing/anki2.py b/anki/importing/anki2.py index 107856a5f..6600a94ce 100644 --- a/anki/importing/anki2.py +++ b/anki/importing/anki2.py @@ -39,6 +39,7 @@ class Anki2Importer(Importer): self._prepareModels() self._importFacts() self._importCards() + self._importMedia() # Facts ###################################################################### @@ -184,3 +185,9 @@ class Anki2Importer(Importer): insert into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""", cards) self.dst.db.executemany(""" insert into revlog values (?,?,?,?,?,?,?,?,?)""", revlog) + + # Media + ###################################################################### + + def _importMedia(self): + self.src.media.copyTo(self.dst.media.dir()) diff --git a/anki/media.py b/anki/media.py index 233d64abe..5637ed6a3 100644 --- a/anki/media.py +++ b/anki/media.py @@ -161,6 +161,17 @@ If the same name exists, compare checksums.""" files.add(f) return files + # Copying on import + ########################################################################## + + def copyTo(self, rdir): + ldir = self.dir() + for f in os.listdir(ldir): + src = os.path.join(ldir, f) + dst = os.path.join(rdir, f) + if not os.path.exists(dst): + shutil.copy2(src, dst) + # Tracking changes (public) ########################################################################## diff --git a/tests/test_upgrade.py b/tests/test_upgrade.py index 4a4da32d6..514729d4f 100644 --- a/tests/test_upgrade.py +++ b/tests/test_upgrade.py @@ -39,6 +39,8 @@ def test_import(): srcFacts = src.factCount() srcCards = src.cardCount() srcRev = src.db.scalar("select count() from revlog") + # add a media file for testing + open(os.path.join(src.media.dir(), "foo.jpg"), "w").write("foo") src.close() # create a new empty deck dst = getEmptyDeck() @@ -61,6 +63,3 @@ def test_import(): imp.run() check() print dst.path - - -