Beispiel #1
0
 def run(self):
     u = Upgrader()
     # check
     res = u.check(self.file)
     if res == "invalid":
         self.log.append(_("File is invalid. Please restore from backup."))
         raise Exception("invalidFile")
     # upgrade
     if res != "ok":
         self.log.append("Problems fixed during upgrade:\n***\n%s\n***\n" %
                         res)
     try:
         deck = u.upgrade()
     except:
         traceback.print_exc()
         self.log.append(traceback.format_exc())
         return
     # save the conf for later
     conf = deck.decks.confForDid(1)
     # merge
     deck.close()
     mdir = re.sub(r"\.anki2?$", ".media", self.file)
     self.deckPrefix = re.sub(r"\.anki$", "", os.path.basename(self.file))
     self.file = deck.path
     Anki2Importer.run(self, mdir)
     # set imported deck to saved conf
     id = self.col.decks.confId(self.deckPrefix)
     conf['id'] = id
     conf['name'] = self.deckPrefix
     conf['usn'] = self.col.usn()
     self.col.decks.updateConf(conf)
     did = self.col.decks.id(self.deckPrefix)
     d = self.col.decks.get(did)
     self.col.decks.setConf(d, id)
Beispiel #2
0
 def run(self):
     u = Upgrader()
     # check
     res = u.check(self.file)
     if res == "invalid":
         self.log.append(_(
             "File is invalid. Please restore from backup."))
         raise Exception("invalidFile")
     # upgrade
     if res != "ok":
         self.log.append(
             "Problems fixed during upgrade:\n***\n%s\n***\n" % res)
     try:
         deck = u.upgrade()
     except:
         traceback.print_exc()
         self.log.append(traceback.format_exc())
         return
     # save the conf for later
     conf = deck.decks.confForDid(1)
     # merge
     deck.close()
     mdir = re.sub(r"\.anki2?$", ".media",  self.file)
     self.deckPrefix = re.sub(r"\.anki$", "", os.path.basename(self.file))
     self.file = deck.path
     Anki2Importer.run(self, mdir)
     # set imported deck to saved conf
     id = self.col.decks.confId(self.deckPrefix)
     conf['id'] = id
     conf['name'] = self.deckPrefix
     conf['usn'] = self.col.usn()
     self.col.decks.updateConf(conf)
     did = self.col.decks.id(self.deckPrefix)
     d = self.col.decks.get(did)
     self.col.decks.setConf(d, id)
Beispiel #3
0
def test_anki2():
    global srcNotes, srcCards
    # get the deck to import
    tmp = getUpgradeDeckPath()
    u = Upgrader()
    src = u.upgrade(tmp)
    srcpath = src.path
    srcNotes = src.noteCount()
    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()
    # import src into dst
    imp = Anki2Importer(dst, srcpath)
    imp.run()
    def check():
        assert dst.noteCount() == srcNotes
        assert dst.cardCount() == srcCards
        assert srcRev == dst.db.scalar("select count() from revlog")
        mids = [int(x) for x in dst.models.models.keys()]
        assert not dst.db.scalar(
            "select count() from notes where mid not in "+ids2str(mids))
        assert not dst.db.scalar(
            "select count() from cards where nid not in (select id from notes)")
        assert not dst.db.scalar(
            "select count() from revlog where cid not in (select id from cards)")
        assert dst.fixIntegrity()[0].startswith("Database rebuilt")
    check()
    # importing should be idempotent
    imp.run()
    check()
    assert len(os.listdir(dst.media.dir())) == 1
Beispiel #4
0
 def run(self):
     u = Upgrader()
     # check
     if not u.check(self.file):
         self.log.append(_(
             "File is old or damaged; please run Tools>Advanced>Check DB "
             "in Anki 1.2 first."))
         raise Exception("invalidFile")
     # upgrade
     try:
         deck = u.upgrade(self.file)
     except:
         traceback.print_exc()
         self.log.append(traceback.format_exc())
         return
     # save the conf for later
     conf = deck.decks.confForDid(1)
     # merge
     deck.close()
     mdir = self.file.replace(".anki", ".media")
     self.deckPrefix = os.path.basename(self.file).replace(".anki", "")
     self.file = deck.path
     Anki2Importer.run(self, mdir)
     # set imported deck to saved conf
     id = self.col.decks.confId(self.deckPrefix)
     conf['id'] = id
     conf['name'] = self.deckPrefix
     conf['usn'] = self.col.usn()
     self.col.decks.updateConf(conf)
     did = self.col.decks.id(self.deckPrefix)
     d = self.col.decks.get(did)
     self.col.decks.setConf(d, id)
Beispiel #5
0
def test_check():
    dst = getUpgradeDeckPath()
    u = Upgrader()
    assert u.check(dst)
    # if it's corrupted, will fail
    open(dst, "w+").write("foo")
    assert not u.check(dst)
Beispiel #6
0
 def run(self):
     u = Upgrader()
     # check
     if not u.check(self.file):
         self.log.append(_(
             "File is old or damaged; please run Tools>Advanced>Check DB "
             "in Anki 1.2 first."))
         raise Exception("invalidFile")
     # upgrade
     try:
         deck = u.upgrade(self.file)
     except:
         traceback.print_exc()
         self.log.append(traceback.format_exc())
         return
     # save the conf for later
     conf = deck.decks.confForDid(1)
     # merge
     deck.close()
     mdir = re.sub(r"\.anki2?$", ".media2",  self.file)
     self.deckPrefix = re.sub(r"\.anki$", "", os.path.basename(self.file))
     self.file = deck.path
     Anki2Importer.run(self, mdir)
     # set imported deck to saved conf
     id = self.col.decks.confId(self.deckPrefix)
     conf['id'] = id
     conf['name'] = self.deckPrefix
     conf['usn'] = self.col.usn()
     self.col.decks.updateConf(conf)
     did = self.col.decks.id(self.deckPrefix)
     d = self.col.decks.get(did)
     self.col.decks.setConf(d, id)
Beispiel #7
0
def test_invalid_ords():
    dst = getUpgradeDeckPath("invalid-ords.anki")
    u = Upgrader()
    u.check(dst)
    deck = u.upgrade()
    assert deck.db.scalar("select count() from cards where ord = 0") == 1
    assert deck.db.scalar("select count() from cards where ord = 1") == 1
Beispiel #8
0
def test_check():
    dst = getUpgradeDeckPath()
    u = Upgrader()
    assert u.check(dst)
    # if it's corrupted, will fail
    open(dst, "w+").write("foo")
    assert not u.check(dst)
Beispiel #9
0
def test_anki2():
    global srcNotes, srcCards
    # get the deck to import
    tmp = getUpgradeDeckPath()
    u = Upgrader()
    src = u.upgrade(tmp)
    srcpath = src.path
    srcNotes = src.noteCount()
    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()
    # import src into dst
    imp = Anki2Importer(dst, srcpath)
    imp.run()
    def check():
        assert dst.noteCount() == srcNotes
        assert dst.cardCount() == srcCards
        assert srcRev == dst.db.scalar("select count() from revlog")
        mids = [int(x) for x in dst.models.models.keys()]
        assert not dst.db.scalar(
            "select count() from notes where mid not in "+ids2str(mids))
        assert not dst.db.scalar(
            "select count() from cards where nid not in (select id from notes)")
        assert not dst.db.scalar(
            "select count() from revlog where cid not in (select id from cards)")
        assert dst.fixIntegrity()[0].startswith("Database rebuilt")
    check()
    # importing should be idempotent
    imp.run()
    check()
    assert len(os.listdir(dst.media.dir())) == 1
Beispiel #10
0
def test_invalid_ords():
    dst = getUpgradeDeckPath("invalid-ords.anki")
    u = Upgrader()
    u.check(dst)
    deck = u.upgrade()
    assert deck.db.scalar("select count() from cards where ord = 0") == 1
    assert deck.db.scalar("select count() from cards where ord = 1") == 1
Beispiel #11
0
def test_check():
    dst = getUpgradeDeckPath()
    u = Upgrader()
    assert u.check(dst) == "ok"
    # if it's corrupted, will fail
    open(dst, "w+").write("foo")
    assert u.check(dst) == "invalid"
    # the upgrade should be able to fix non-fatal errors -
    # test with a deck that has cards with missing notes
    dst = getUpgradeDeckPath("anki12-broken.anki")
    assert "with missing fact" in u.check(dst)
Beispiel #12
0
def test_check():
    dst = getUpgradeDeckPath()
    u = Upgrader()
    assert u.check(dst) == "ok"
    # if it's corrupted, will fail
    open(dst, "w+").write("foo")
    assert u.check(dst) == "invalid"
    # the upgrade should be able to fix non-fatal errors -
    # test with a deck that has cards with missing notes
    dst = getUpgradeDeckPath("anki12-broken.anki")
    assert "with missing fact" in u.check(dst)
Beispiel #13
0
def test_upgrade():
    dst = getUpgradeDeckPath()
    csum = checksum(open(dst).read())
    u = Upgrader()
    deck = u.upgrade(dst)
    # src file must not have changed
    assert csum == checksum(open(dst).read())
    # creation time should have been adjusted
    d = datetime.datetime.fromtimestamp(deck.crt)
    assert d.hour == 4 and d.minute == 0
    # 3 new, 2 failed, 1 due
    deck.reset()
    deck.conf['counts'] = COUNT_REMAINING
    assert deck.sched.counts() == (3,4,1)
    # now's a good time to test the integrity check too
    deck.fixIntegrity()
Beispiel #14
0
def test_upgrade():
    dst = getUpgradeDeckPath()
    csum = checksum(open(dst).read())
    u = Upgrader()
    deck = u.upgrade(dst)
    # src file must not have changed
    assert csum == checksum(open(dst).read())
    # creation time should have been adjusted
    d = datetime.datetime.fromtimestamp(deck.crt)
    assert d.hour == 4 and d.minute == 0
    # 3 new, 2 failed, 1 due
    deck.reset()
    deck.conf['counts'] = COUNT_REMAINING
    assert deck.sched.counts() == (3, 4, 1)
    # now's a good time to test the integrity check too
    deck.fixIntegrity()
Beispiel #15
0
def test_upgrade():
    dst = getUpgradeDeckPath()
    csum = checksum(open(dst).read())
    u = Upgrader()
    deck = u.upgrade(dst)
    # src file must not have changed
    assert csum == checksum(open(dst).read())
    # creation time should have been adjusted
    d = datetime.datetime.fromtimestamp(deck.crt)
    assert d.hour == 4 and d.minute == 0
    # 3 new, 2 failed, 1 due
    deck.reset()
    deck.conf['counts'] = COUNT_REMAINING
    assert deck.sched.counts() == (3, 4, 1)
    # modifying each note should not cause new cards to be generated
    assert deck.cardCount() == 6
    for nid in deck.db.list("select id from notes"):
        note = deck.getNote(nid)
        note.flush()
    assert deck.cardCount() == 6
    # now's a good time to test the integrity check too
    deck.fixIntegrity()
Beispiel #16
0
def test_upgrade1():
    dst = getUpgradeDeckPath()
    csum = checksum(open(dst).read())
    u = Upgrader()
    deck = u.upgrade(dst)
    # src file must not have changed
    assert csum == checksum(open(dst).read())
    # creation time should have been adjusted
    d = datetime.datetime.fromtimestamp(deck.crt)
    assert d.hour == 4 and d.minute == 0
    # 3 new, 2 failed, 1 due
    deck.reset()
    deck.conf['counts'] = COUNT_REMAINING
    assert deck.sched.counts() == (3,4,1)
    # modifying each note should not cause new cards to be generated
    assert deck.cardCount() == 6
    for nid in deck.db.list("select id from notes"):
        note = deck.getNote(nid)
        note.flush()
    assert deck.cardCount() == 6
    # now's a good time to test the integrity check too
    deck.fixIntegrity()
Beispiel #17
0
def test_upgrade1_due():
    dst = getUpgradeDeckPath("anki12-due.anki")
    u = Upgrader()
    u.check(dst)
    deck = u.upgrade()
    assert not deck.db.scalar("select 1 from cards where due != 1")
Beispiel #18
0
def test_upgrade1_due():
    dst = getUpgradeDeckPath("anki12-due.anki")
    u = Upgrader()
    deck = u.upgrade(dst)
    assert not deck.db.scalar("select 1 from cards where due != 1")