def setup_local(loadDecks=None): global deck1, deck2, client, server if loadDecks: deck1 = Deck(loadDecks[0], backup=False) deck2 = Deck(loadDecks[1], backup=False) else: deck1 = Deck() deck1.addModel(BasicModel()) deck1.currentModel.cardModels[1].active = True deck1.newCardOrder = 1 f = deck1.newFact() f['Front'] = u"foo" f['Back'] = u"bar" f.tags = u"foo" deck1.addFact(f) deck2 = Deck() deck2.addModel(BasicModel()) deck2.currentModel.cardModels[1].active = True f = deck2.newFact() f['Front'] = u"baz" f['Back'] = u"qux" f.tags = u"bar" deck2.addFact(f) deck2.newCardOrder = 1 # normally such syncs would trigger a conflict, but we ignore it deck1.setVar("schemaMod", 0) deck2.setVar("schemaMod", 0) client = SyncClient(deck1) server = SyncServer(deck2) client.setServer(server)
def setup_local(loadDecks=None): global deck1, deck2, client, server if loadDecks: deck1 = DeckStorage.Deck(loadDecks[0], backup=False) deck2 = DeckStorage.Deck(loadDecks[1], backup=False) else: deck1 = DeckStorage.Deck() deck1.addModel(BasicModel()) deck1.currentModel.cardModels[1].active = True deck1.newCardOrder = 1 f = deck1.newFact() f['Front'] = u"foo" f['Back'] = u"bar" f.tags = u"foo" deck1.addFact(f) deck2 = DeckStorage.Deck() deck2.addModel(BasicModel()) deck2.currentModel.cardModels[1].active = True f = deck2.newFact() f['Front'] = u"baz" f['Back'] = u"qux" f.tags = u"bar" deck2.addFact(f) deck2.newCardOrder = 1 client = SyncClient(deck1) server = SyncServer(deck2) client.setServer(server)
def test_modelAddDelete(): deck = DeckStorage.Deck() deck.addModel(BasicModel()) deck.addModel(BasicModel()) f = deck.newFact() f['Front'] = u'1' f['Back'] = u'2' deck.addFact(f) assert deck.cardCount == 1 deck.deleteModel(deck.currentModel) assert deck.cardCount == 0 deck.s.refresh(deck)
def Deck(path, queue=True, lock=True): "Open a new or existing deck. Path must be unicode." path = os.path.abspath(path) create = not os.path.exists(path) if create: base = os.path.basename(path) for c in ("/", ":", "\\"): assert c not in base # connect db = DB(path) if create: ver = _createDB(db) else: ver = _upgradeSchema(db) db.execute("pragma cache_size = 20000") # add db to deck and do any remaining upgrades deck = _Deck(db) if ver < CURRENT_VERSION: _upgradeDeck(deck, ver) elif create: deck.addModel(BasicModel(deck)) deck.addModel(ClozeModel(deck)) # default to basic deck.conf['currentModelId'] = 1 deck.save() if lock: deck.lock() if not queue: return deck # rebuild queue deck.reset() return deck
def test_saveAs(): path = "/tmp/test_saveAs.anki" try: os.unlink(path) except OSError: pass path2 = "/tmp/test_saveAs2.anki" try: os.unlink(path2) except OSError: pass # start with an in-memory deck deck = DeckStorage.Deck() deck.addModel(BasicModel()) # add a card f = deck.newFact() f['Front'] = u"foo" f['Back'] = u"bar" deck.addFact(f) assert deck.cardCount == 1 # save in new deck newDeck = deck.saveAs(path) assert newDeck.cardCount == 1 # delete card id = newDeck.s.scalar("select id from cards") newDeck.deleteCard(id) # save into new deck newDeck2 = newDeck.saveAs(path2) # new deck should have zero cards assert newDeck2.cardCount == 0 # but old deck should have reverted the unsaved changes newDeck = DeckStorage.Deck(path) assert newDeck.cardCount == 1 newDeck.close()
def test_anki10_modtime(): deck1 = Deck() deck2 = Deck() client = SyncClient(deck1) server = SyncServer(deck2) client.setServer(server) deck1.addModel(BasicModel()) f = deck1.newFact() f['Front'] = u"foo" f['Back'] = u"bar" deck1.addFact(f) assert deck1.cardCount() == 1 assert deck2.cardCount() == 0 client.sync() assert deck1.cardCount() == 1 assert deck2.cardCount() == 1 file_ = unicode(os.path.join(testDir, "importing/test10-3.anki")) file = "/tmp/test10-3.anki" shutil.copy(file_, file) i = anki10.Anki10Importer(deck1, file) i.doImport() client.sync() assert i.total == 1 assert deck2.db.scalar("select count(*) from cards") == 2 assert deck2.db.scalar("select count(*) from facts") == 2 assert deck2.db.scalar("select count(*) from models") == 2
def test_mnemosyne10(): deck = Deck() deck.addModel(BasicModel()) file = unicode(os.path.join(testDir, "importing/test.mem")) i = mnemosyne10.Mnemosyne10Importer(deck, file) i.doImport() assert i.total == 5 deck.close()
def test_findCards(): deck = DeckStorage.Deck() deck.addModel(BasicModel()) f = deck.newFact() f['Front'] = u'dog' f['Back'] = u'cat' f.tags = u"monkey" deck.addFact(f) f = deck.newFact() f['Front'] = u'goats are fun' f['Back'] = u'sheep' f.tags = u"sheep goat horse" deck.addFact(f) f = deck.newFact() f['Front'] = u'cat' f['Back'] = u'sheep' deck.addFact(f) assert not deck.findCards("tag:donkey") assert len(deck.findCards("tag:sheep")) == 1 assert len(deck.findCards("tag:sheep tag:goat")) == 1 assert len(deck.findCards("tag:sheep tag:monkey")) == 0 assert len(deck.findCards("tag:monkey")) == 1 assert len(deck.findCards("tag:sheep -tag:monkey")) == 1 assert len(deck.findCards("-tag:sheep")) == 2 assert len(deck.findCards("cat")) == 2 assert len(deck.findCards("cat -dog")) == 1 assert len(deck.findCards("cat -dog")) == 1 assert len(deck.findCards("are goats")) == 1 assert len(deck.findCards('"are goats"')) == 0 assert len(deck.findCards('"goats are"')) == 1 # make sure card templates and models match too assert len(deck.findCards('tag:basic')) == 3 assert len(deck.findCards('tag:forward')) == 3 deck.addModel(BasicModel()) f = deck.newFact() f['Front'] = u'foo' f['Back'] = u'bar' deck.addFact(f) deck.currentModel.cardModels[1].active = True f = deck.newFact() f['Front'] = u'baz' f['Back'] = u'qux' c = deck.addFact(f) assert len(deck.findCards('tag:forward')) == 5 assert len(deck.findCards('tag:reverse')) == 1
def test_dingsbums(): deck = Deck() deck.addModel(BasicModel()) startNumberOfFacts = deck.factCount() file = unicode(os.path.join(testDir, "importing/dingsbums.xml")) i = dingsbums.DingsBumsImporter(deck, file) i.doImport() assert 7 == i.total deck.close()
def test_csv_tags(): deck = Deck() deck.addModel(BasicModel()) file = unicode(os.path.join(testDir, "importing/text-tags.txt")) i = csvfile.TextImporter(deck, file) i.doImport() facts = deck.db.query(Fact).all() assert len(facts) == 2 assert facts[0].tags == "baz qux" or facts[1].tags == "baz qux" deck.close()
def test_supermemo_xml_01_unicode(): deck = Deck() deck.addModel(BasicModel()) file = unicode(os.path.join(testDir, "importing/supermemo1.xml")) i = supermemo_xml.SupermemoXmlImporter(deck, file) #i.META.logToStdOutput = True i.doImport() # only returning top-level elements? assert i.total == 1 deck.close()
def test_csv(): deck = Deck() deck.addModel(BasicModel()) file = unicode(os.path.join(testDir, "importing/text-2fields.txt")) i = csvfile.TextImporter(deck, file) i.doImport() # four problems - missing front, dupe front, wrong num of fields assert len(i.log) == 4 assert i.total == 5 deck.close()
def test_factAddDelete(): deck = DeckStorage.Deck() deck.addModel(BasicModel()) # set rollback point deck.s.commit() f = deck.newFact() # empty fields try: deck.addFact(f) except Exception, e: pass
def test_modelChange(): deck = DeckStorage.Deck() m = Model(u"Japanese") m1 = m f = FieldModel(u'Expression', True, True) m.addFieldModel(f) m.addFieldModel(FieldModel(u'Meaning', False, False)) f = FieldModel(u'Reading', False, False) m.addFieldModel(f) m.addCardModel( CardModel(u"Recognition", u"%(Expression)s", u"%(Reading)s<br>%(Meaning)s")) m.addCardModel( CardModel(u"Recall", u"%(Meaning)s", u"%(Expression)s<br>%(Reading)s", active=False)) m.tags = u"Japanese" m1.cardModels[1].active = True deck.addModel(m1) f = deck.newFact() f['Expression'] = u'e' f['Meaning'] = u'm' f['Reading'] = u'r' f = deck.addFact(f) f2 = deck.newFact() f2['Expression'] = u'e2' f2['Meaning'] = u'm2' f2['Reading'] = u'r2' deck.addFact(f2) m2 = BasicModel() m2.cardModels[1].active = True deck.addModel(m2) # convert to basic assert deck.modelUseCount(m1) == 2 assert deck.modelUseCount(m2) == 0 assert deck.cardCount == 4 assert deck.factCount == 2 fmap = { m1.fieldModels[0]: m2.fieldModels[0], m1.fieldModels[1]: None, m1.fieldModels[2]: m2.fieldModels[1] } cmap = {m1.cardModels[0]: m2.cardModels[0], m1.cardModels[1]: None} deck.changeModel([f.id], m2, fmap, cmap) assert deck.modelUseCount(m1) == 1 assert deck.modelUseCount(m2) == 1 assert deck.cardCount == 3 assert deck.factCount == 2 (q, a) = deck.s.first(""" select question, answer from cards where factId = :id""", id=f.id) assert stripHTML(q) == u"e" assert stripHTML(a) == u"r"
def test_localsync_models(): client.sync() # add a model deck1.addModel(BasicModel()) assert len(deck1.models) == 3 assert len(deck2.models) == 2 client.sync() assert len(deck2.models) == 3 assert deck1.currentModel.id == deck2.currentModel.id # delete the recently added model deck2.deleteModel(deck2.currentModel) assert len(deck2.models) == 2 client.sync() assert len(deck1.models) == 2 assert deck1.currentModel.id == deck2.currentModel.id # make a card model inactive assert deck1.currentModel.cardModels[1].active == True deck2.currentModel.cardModels[1].active = False deck2.currentModel.setModified() deck2.setModified() client.sync() assert deck1.currentModel.cardModels[1].active == False # remove a card model deck1.deleteCardModel(deck1.currentModel, deck1.currentModel.cardModels[1]) deck1.currentModel.setModified() deck1.setModified() assert len(deck1.currentModel.cardModels) == 1 client.sync() assert len(deck2.currentModel.cardModels) == 1 # add a field c = deck1.getCard() deck1.addFieldModel(c.fact.model, FieldModel(u"yo")) deck1.s.refresh(c.fact) assert len(c.fact.fields) == 3 assert c.fact['yo'] == u"" client.sync() c2 = deck2.s.query(Card).get(c.id) deck2.s.refresh(c2.fact) assert c2.fact['yo'] == u"" # remove the field assert "yo" in c2.fact.keys() deck2.deleteFieldModel(c2.fact.model, c2.fact.model.fieldModels[2]) deck2.s.refresh(c2.fact) assert "yo" not in c2.fact.keys() client.sync() deck1.s.refresh(c.fact) assert "yo" not in c.fact.keys() # rename a field assert u"Front" in c.fact.keys() deck1.renameFieldModel(deck1.currentModel, deck1.currentModel.fieldModels[0], u"Sideways") client.sync() assert deck2.currentModel.fieldModels[0].name == u"Sideways"
def test_modelCopy(): deck = DeckStorage.Deck() m = BasicModel() assert len(m.fieldModels) == 2 assert len(m.cardModels) == 2 deck.addModel(m) f = deck.newFact() f['Front'] = u'1' deck.addFact(f) m2 = deck.copyModel(m) assert m2.name == "Basic copy" assert m2.id != m.id assert m2.fieldModels[0].id != m.fieldModels[0].id assert m2.cardModels[0].id != m.cardModels[0].id assert len(m2.fieldModels) == 2 assert len(m.fieldModels) == 2 assert len(m2.fieldModels) == len(m.fieldModels) assert len(m.cardModels) == 2 assert len(m2.cardModels) == 2
def test_localsync_models(): client.sync() # add a model deck1.addModel(BasicModel()) assert len(deck1.models) == 3 assert len(deck2.models) == 2 deck1.setVar("schemaMod", 0) client.sync() assert len(deck2.models) == 3 assert deck1.currentModel.id == deck2.currentModel.id # delete the recently added model deck2.deleteModel(deck2.currentModel) assert len(deck2.models) == 2 deck2.setVar("schemaMod", 0) client.sync() assert len(deck1.models) == 2 assert deck1.currentModel.id == deck2.currentModel.id # make a card model inactive assert deck1.currentModel.cardModels[1].active == True deck2.currentModel.cardModels[1].active = False deck2.currentModel.setModified() deck2.flushMod() client.sync() assert deck1.currentModel.cardModels[1].active == False # remove a card model deck1.deleteCardModel(deck1.currentModel, deck1.currentModel.cardModels[1]) deck1.currentModel.setModified() deck1.setModified() assert len(deck1.currentModel.cardModels) == 1 deck1.setVar("schemaMod", 0) client.sync() assert len(deck2.currentModel.cardModels) == 1 # rename a field c = deck1.getCard() assert u"Front" in c.fact.keys() deck1.renameFieldModel(deck1.currentModel, deck1.currentModel.fieldModels[0], u"Sideways") client.sync() assert deck2.currentModel.fieldModels[0].name == u"Sideways"
def test_updating(): # get the standard csv deck first deck = Deck() deck.addModel(BasicModel()) file = unicode(os.path.join(testDir, "importing/text-2fields.txt")) i = csvfile.TextImporter(deck, file) i.doImport() # now update file = unicode(os.path.join(testDir, "importing/text-update.txt")) i = csvfile.TextImporter(deck, file) # first field i.updateKey = (0, deck.currentModel.fieldModels[0].id) i.multipleCardsAllowed = False i.doImport() ans = deck.db.scalar( u"select answer from cards where question like '%食べる%'") assert "to ate" in ans # try again with tags i.updateKey = (0, deck.currentModel.fieldModels[0].id) i.mapping[1] = 0 i.doImport() deck.close()
]) + n * 3 + q['explanation'] card = ForeignCard() card.fields = [ unicode(front.replace(n, br), "utf-8"), unicode(back.replace(n, br), "utf-8") ] cards.append(card) return cards def fields(seld): return 2 deck = DeckStorage.Deck(r'test.anki') deck.addModel(BasicModel()) imp = VceImporter(deck, '') imp.doImport() deck.save() deck.close() ''' with open("C:\\output_v2.csv", 'w') as f: for e in exams: for q in e: the_hash = hashlib.sha1(q['question']).digest() if the_hash in hashes: continue hashes.append(the_hash)