Beispiel #1
0
def ImportToAnki(model_name, import_to_deck, *args, **kwargs):
    # get file
    file = kwargs.get("file", None)
    if not file:
        file = getFile(mw,
                       _("Import"),
                       None,
                       key="import",
                       filter=Importers[0][0])
    if not file:
        return
    file = str(file)

    # check default model
    try:
        model = mw.col.models.byName(model_name)
        if not model:
            raise Exception("没有找到【{}】".format(model_name))
    except:
        importFile(mw, settings.deck_template_file)
        try:
            model = mw.col.models.byName(model_name)
        except:
            model = None

    importer = TextImporter(mw.col, file)
    importer.delimiter = "\t"
    importer.importMode = 0
    importer.allowHTML = True
    importer.model = model

    did = mw.col.decks.id(import_to_deck)
    mw.col.conf['curDeck'] = did
    importer.model['did'] = did
    mw.col.decks.select(did)
    importer.mapping = [kwargs.get("first")]
    importer.run()
    mw.reset()
    txt = _("Importing complete.") + "\n"
    if importer.log:
        txt += "\n".join(importer.log)
    showText(txt)
Beispiel #2
0
        def makeDeck(parent,prefix,deck):
            name = deck.csvname
            csvfile = "%s%s%s.csv" % (tmpPath,prefix,name)

            if not os.path.exists(csvfile) and deck.cardType != None:
                print('Skipping deck "%s" because no file "%s" was found.' % (name, csvfile))
                return
                # raise Exception('No csv file "' + csvfile + '" found.')

            did = tcol.decks.id(parent + deck.name)
            d = tcol.decks.get(did)
            tcol.decks.select(did)

            confId = tcol.decks.confId(parent + deck.name, cloneFrom=deck.conf)

            if not deck.cardType:
                conf = tcol.decks.getConf(confId)
                conf['new']['perDay'] = 999
                tcol.decks.updateConf(conf)
            elif deck.perDay:
                conf = tcol.decks.getConf(confId)
                conf['new']['perDay'] = deck.perDay
                tcol.decks.updateConf(conf)

            tcol.decks.setConf(d,confId)

            if deck.cardType:
                ct = deck.cardType

                if not tcol.models.byName(ct.name):
                    m = tcol.models.new(ct.name)
                    m['req'] = [[0, 'all', [0]]]
                    m['css'] = ct.css()
                    m['tmpls'] = [
                        {
                            'name': 'Card 1',
                            'qfmt': ct.front(),
                            'afmt': ct.back(),
                            'bfont': 'Lucida Sans Unicode',
                            'bamft': '',
                            'bqmft': '',
                            'ord': 0,
                            'did': None,
                            'bsize': 12
                        }
                    ]
                    tcol.models.add(m)

                    for i,field in enumerate(ct.fields):
                        f = tcol.models.newField(field.anki_name)
                        f['ord'] = i
                        tcol.models.addField(m,f)
                else:
                    m = tcol.models.byName(ct.name)

                # So that we can reuse already-present models
                # todo: this doesn't actually work but would be a big part of
                # updating
                # if m['id'] != ct.mid:
                # 	m = tcol.models.get(m['id'])
                # 	m['id'] = ct.mid
                # 	m.save(m)

                tcol.save()

                m['did'] = did
                tcol.decks.select(did)
                ti = TextImporter(tcol,csvfile)
                ti.model = m
                ti.allowHTML = True
                ti.initMapping()
                ti.delimiter = "\t"
                ti.updateDelimiter()

                ti.run()
                tcol.save()

            for sd in deck.subdecks:
                makeDeck(parent + deck.name + '::',prefix + name + '-', sd)