obsolete/exp_anki.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Tue, 05 Dec 2023 13:24:46 +0200
changeset 1353 dcda231188dc
parent 670 b86e70406247
permissions -rw-r--r--
New articles.

# -*- coding: utf-8 -*-

import sys
import os
import tempfile
import shutil
import traceback

FONAME = "test.apkg"

# Looks like anki libs change working directory to media directory of current deck
# Therefore absolute path should be stored before creating temporary deck
FONAME = os.path.abspath(FONAME)
FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))

CWD = os.getcwd()
TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))

def cleanup():
    os.chdir(CWD)
    shutil.rmtree(TMPDIR)

def exception_handler(etype, ex, tb):
    cleanup()
    traceback.print_exception(etype, ex, tb)

sys.excepthook = exception_handler

sys.path.append('/usr/share/anki')
import anki
from anki.exporting import AnkiPackageExporter

collection = anki.Collection(os.path.join(TMPDIR, 'collection.anki2'))

deck_id = collection.decks.id(FBASENAME + "_deck")
deck = collection.decks.get(deck_id)
# deck = collection.decks.confForDid(deck_id)
# collection.decks.update(deck)
# print(dir(deck))
# print(type(deck))
# print(deck)

model = collection.models.new(FBASENAME + "_model")
model['tags'].append(FBASENAME + "_tag")
model['did'] = deck_id
model['css'] = """
.card {
  font-family: arial;
  font-size: 20px;
  text-align: center;
  color: black;
  background-color: white;
}
.from {
  font-style: italic;
}
"""

collection.models.addField(model, collection.models.newField('en'))
collection.models.addField(model, collection.models.newField('ru'))

tmpl = collection.models.newTemplate('en -> ru')
tmpl['qfmt'] = '<div class="from">{{en}}</div>'
tmpl['afmt'] = '{{FrontSide}}<hr id=answer>{{ru}}'
collection.models.addTemplate(model, tmpl)
tmpl = collection.models.newTemplate('ru -> en')
tmpl['qfmt'] = '{{ru}}'
tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="from">{{en}}</div>'
collection.models.addTemplate(model, tmpl)

# print(dir(model))
# print(type(model))
# print(model)
# Equivalent of:
# collection.models.add(model)
# without setting auto-generated ID:
model['id'] = 12345678  # essential for upgrade detection
collection.models.update(model)
collection.models.setCurrent(model)
collection.models.save(model)

# collection.decks.select(deck_id)

note = anki.notes.Note(collection, model)
# print(dir(note))
# print(type(note))
# print(note._fmap)
# print(note)


note['en'] = "hello"
note['ru'] = u"[heləʊ] int. привет"
note.guid = "enru_1"
collection.addNote(note)
# print(dir(note))

note = collection.newNote()
note['en'] = "bye"
note['ru'] = u"[baɪ] int. пока"
note.guid = "enru_2"
collection.addNote(note)

# model.add(deck)
# model.save()

################################################################
# Add another model and new types of notes into same storage!
model_irrverb = collection.models.new(FBASENAME + "_model_irrverb")
model_irrverb['tags'].append(FBASENAME + "_tag_irrverb")
model_irrverb['did'] = deck_id
model_irrverb['css'] = """
.card {
  font-family: arial;
  font-size: 20px;
  text-align: center;
  color: black;
  background-color: white;
}
.v1 { color: red; }
.v2 { color: green; }
.v3 { color: blue; }
"""

collection.models.addField(model_irrverb, collection.models.newField('v1'))
collection.models.addField(model_irrverb, collection.models.newField('v2'))
collection.models.addField(model_irrverb, collection.models.newField('v3'))

ANSWER_MSG = '{{FrontSide}}<hr id=answer>{{v1}}<br>{{v2}}<br>{{v3}}'
tmpl = collection.models.newTemplate('v1')
tmpl['qfmt'] = '<div class="v1">{{v1}}</div>'
tmpl['afmt'] = ANSWER_MSG
collection.models.addTemplate(model_irrverb, tmpl)

tmpl = collection.models.newTemplate('v2')
tmpl['qfmt'] = '<div class="v2">{{v2}}</div>'
tmpl['afmt'] = ANSWER_MSG
collection.models.addTemplate(model_irrverb, tmpl)
tmpl = collection.models.newTemplate('v3')
tmpl['qfmt'] = '<div class="v3">{{v3}}</div>'
tmpl['afmt'] = ANSWER_MSG
collection.models.addTemplate(model_irrverb, tmpl)

# We mustn't define 'id' before adding tmpl because addTemplate() fail because
# it tries to update DB but we have no DB entry yet...
model_irrverb['id'] = 12345678+1  # essential for upgrade detection
collection.models.update(model_irrverb)
collection.models.setCurrent(model_irrverb)
collection.models.save(model_irrverb)

note = anki.notes.Note(collection, model_irrverb)
note['v1'] = "go"
note['v2'] = "went"
note['v3'] = "gone"
note.guid = "irrverb_1"
collection.addNote(note)

note = anki.notes.Note(collection, model_irrverb)
note['v1'] = "drive"
note['v2'] = "drove"
note['v3'] = "driven"
note.guid = "irrverb_2"
collection.addNote(note)

export = AnkiPackageExporter(collection)
try:
    os.remove(FONAME)
except OSError as ex:
    if ex.errno not in [2]:
        raise
export.exportInto(FONAME)

cleanup()