Beispiel #1
0
def do_import_notes(bcol,
                    deck_name,
                    data_file,
                    note_type,
                    delimiter="\t",
                    import_mode=0):
    col = bcol.col

    existingNotes = {}

    #load existing notes
    bdeck = None
    for dummy, bdk in sorted(bcol.bdecks.items()):
        if deck_name == bdk.name:
            bdeck = bdk
    assert bdeck
    notes = bdeck.queryNotes()
    for n in notes:
        note_id, note_subject, note_content, note_tags = n
        note_content = note_content.split("\x1f")[1]
        note_tags = note_tags.strip()
        existingNotes[note_subject] = [
            note_id, note_subject, note_content, note_tags
        ]

    nochangeNotes = {}
    toBeImportNotes = {}
    toBeUpdatedNotes = {}
    #load data fiel
    fp = open(data_file, "r")
    for line in fp.readlines():
        line = line.strip()
        parts = line.split("\t")
        subject = parts[0]
        content = parts[1]
        if len(content) > int(131072 * 0.8):  #131072 is limit of ANKI field
            logging.error("Content too long to import: %d, note: %s",
                          len(content), subject)
            sys.exit(1)
        if len(parts) == 3:
            tags = parts[2]
            tags = tags.strip()
        else:
            tags = ""
        if subject in existingNotes:
            #compare content and tags
            exist_note = existingNotes[subject]
            if content == exist_note[2] and tags == exist_note[3]:
                #doesn't need to be updated
                nochangeNotes[subject] = True
                pass
            else:
                logging.info("Updated note: %s", subject)
                toBeUpdatedNotes[subject] = [
                    subject, content, tags, exist_note[0]
                ]
        else:
            logging.info("New note: %s", subject)
            toBeImportNotes[subject] = [subject, content, tags]
    fp.close()

    logging.info("%d notes wll be kept without any change", len(nochangeNotes))
    logging.info("%d notes need to be updated.", len(toBeUpdatedNotes))
    logging.info("%d notes need to be added.", len(toBeImportNotes))

    if not toBeUpdatedNotes and not toBeImportNotes:
        col.close()
        logging.info("No new note need to be imported! Bye!")
        sys.exit(1)

    new_data_file = filter_import_data_file(data_file, toBeImportNotes,
                                            toBeUpdatedNotes)
    assert new_data_file

    #set current model
    logging.info("setting current deck name: %s", deck_name)
    deck_id = col.decks.id(deck_name)
    logging.info("setting current deck id: %s", deck_id)
    logging.info("setting note_type : %s", note_type)
    model = col.models.byName(note_type)

    #select deck
    col.decks.select(deck_id)

    #update deck
    deck = col.decks.get(deck_id)
    deck['mid'] = model['id']
    col.decks.save(deck)

    #update model
    model['did'] = deck_id
    col.models.save(model)
    col.models.setCurrent(model)

    logging.info("directly import: %s", new_data_file)
    ti = TextImporter(col, new_data_file)
    ti.allowHTML = True
    ti.needDelimiter = True
    ti.delimiter = "\t"
    ti.importMode = import_mode  #0, UPDATE_MODE; 1, IGNORE_MODE; 2, ADD_MODE
    ti.initMapping()
    ti.run()

    col.save()
    col.close()

    logging.info("Total %d imported,%d updated successfully." %
                 (len(toBeImportNotes), len(toBeUpdatedNotes)))

    return