from anki.utils import tmpdir, namedtmp
from anki.importing.anki2 import Anki2Importer
from anki import Collection

class DirectoryImporter(Anki2Importer):

    def run(self, media=None):
        path = namedtmp("dir_importer.anki2")
        deck = Collection(path)
        f = deck.newNote()
        f['Front'] = u"one"; f['Back'] = u"two"
        n = deck.addNote(f)
        assert n == 1
        deck.close()
        # anki/tests/test_collection.py

        self.file = path
        Anki2Importer.run(self)

@@ -109,16 +109,289 @@
 ection%0A%0A
+model_css = %22%22%22%5C%0A.card %7B%0A font-family: arial;%0A font-size: 20px;%0A text-align: center;%0A color: black;%0A background-color: white;%0A%7D%0A%0A#source %7B%0A font-size: 12px;%0A position: relative;%0A margin-top: 50px;%0A margin-left: 300px;%0A color: grey;%0A%7D%0A%22%22%22%0A%0A# TODO updating a deck is a mess%0A%0A
 class Di
@@ -508,20 +508,19 @@
         
-deck
+col
  = Colle
@@ -543,240 +543,1755 @@
     
-f = deck.newNote()%0A        f%5B'Front'%5D = u%22one%22; f%5B'Back'%5D = u%22two%22%0A        n = deck.addNote(f)%0A        assert n == 1%0A        deck.close()%0A        # anki/tests/test_collection.py%0A%0A        self.file = path%0A        Anki2Importer.run(self)
+# Setup a deck%0A        old_did = col.decks.selected()%0A        col.decks.rename(col.decks.get(old_did), %22MyDeck%22)%0A        # Make a new model%0A        m = col.models.copy(col.models.current())%0A        fields = %7B%7D%0A        fields%5B%22Filename%22%5D = col.models.newField(%22Filename%22)%0A        fields%5B%22Source%22%5D = col.models.newField(%22Source%22)%0A        col.models.addField(m, fields%5B%22Filename%22%5D)%0A        col.models.addField(m, fields%5B%22Source%22%5D)%0A        col.models.setSortIdx(m, m%5B%22flds%22%5D.index(fields%5B%22Filename%22%5D))%0A        # Set styling and templates for model m%0A        m%5B%22css%22%5D = model_css%0A        t = col.models.newTemplate(%22MyTemplate%22)%0A        t%5B%22qfmt%22%5D = %22%7B%7BFront%7D%7D%22%0A        t%5B%22afmt%22%5D = %22%7B%7BFrontSide%7D%7D %3Chr id=answer%3E %7B%7BBack%7D%7D %3Cp id='source'%3E%7B%7BSource%7D%7D%3C/p%3E%22%0A        # Remove old template, set new template%0A        t_old = m%5B%22tmpls%22%5D%5B0%5D%0A        col.models.addTemplate(m, t)%0A        col.models.remTemplate(m, t_old)%0A        col.models.add(m)%0A        col.models.setCurrent(m)%0A        #%0A        m%5B%22id%22%5D = %22ourmodel%22%0A        # Test note%0A        f = col.newNote()%0A        f%5B%22Filename%22%5D = u%22test.txt%22%0A        f%5B%22Front%22%5D = u%22CHANGE Some ALL AGAIN%22%0A        f%5B%22Back%22%5D = u%22ALL OF Differ HOPEFULLY%22%0A        f%5B%22Source%22%5D = u%22YET NOTHING  Stuff SHOULD CHANGE IT WILL FUCKING WORK%22%0A        col.addNote(f)%0A        # Always close the collection%0A        col.close()%0A%0A        self.file = path%0A        Anki2Importer.run(self)%0A%0A    # Our desired method of handling duplicates is much different%0A    def _uniquifyNote(self, note):%0A        # For now we'll just compare the filename field%0A        # But we will need to check that models match up%0A        for n in self._notes:%0A            if note.fields%5B%22Filename%22%5D == n.fields%5B%22Filename%22%5D:%0A                return False%0A        return True
 %0A

Add more detail and styling to collection