obsolete/exp_anki.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Thu, 15 Sep 2016 20:13:18 +0300
changeset 558 53fd793e345d
parent 555 4a3188fc8951
child 667 5f69f0776c37
permissions -rw-r--r--
Add shortcut to deploy to HG repos.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
FONAME = "test.apkg"
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     2
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
# Looks like anki libs change working directory to media directory of current deck
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
# Therefore absolute path should be stored before creating temporary deck
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
FONAME = os.path.abspath(FONAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
import anki
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
from anki.exporting import AnkiPackageExporter
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
collection = anki.Collection(os.path.join(TMPDIR, 'collection.anki2'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    13
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    14
deck_id = collection.decks.id(FBASENAME + "_deck")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
deck = collection.decks.get(deck_id)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
# deck = collection.decks.confForDid(deck_id)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
# collection.decks.update(deck)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
# print(dir(deck))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
# print(type(deck))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
# print(deck)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
model = collection.models.new(FBASENAME + "_model")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
model['tags'].append(FBASENAME + "_tag")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
model['did'] = deck_id
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
model['css'] = """
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
.card {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
  font-family: arial;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
  font-size: 20px;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
  text-align: center;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
  color: black;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
  background-color: white;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
.from {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
  font-style: italic;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    36
"""
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    37
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    38
collection.models.addField(model, collection.models.newField('en'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
collection.models.addField(model, collection.models.newField('tr'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
tmpl = collection.models.newTemplate('en -> tr')
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
tmpl['qfmt'] = '<div class="from">{{en}}</div>'
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n{{tr}}'
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
collection.models.addTemplate(model, tmpl)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
tmpl = collection.models.newTemplate('tr -> en')
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
tmpl['qfmt'] = '{{tr}}'
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n<div class="from">{{en}}</div>'
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
collection.models.addTemplate(model, tmpl)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
print(dir(model))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
print(type(model))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    53
print(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    54
# Equivalent of:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    55
# collection.models.add(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    56
# without setting auto-generated ID:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    57
model['id'] = 12345678  # essential for upgrade detection
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    58
collection.models.update(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    59
collection.models.setCurrent(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    60
collection.models.save(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    61
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    62
# collection.decks.select(deck_id)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    63
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    64
note = anki.notes.Note(collection, model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    65
print(dir(note))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    66
print(type(note))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    67
print(note._fmap)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
print(note)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    71
note['en'] = "hello"
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
note['tr'] = u"[heləʊ]\nint. привет"
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
note.guid = "xxx1"
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
collection.addNote(note)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
print(dir(note))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
note = collection.newNote()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    78
note['en'] = "bye"
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    79
note['tr'] = u"[baɪ]\nint. пока"
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
note.guid = "xxx2"
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
collection.addNote(note)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    82
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    83
# model.add(deck)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
# model.save()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    85
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    86
export = AnkiPackageExporter(collection)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    87
export.exportInto(FONAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    88
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    89
cleanup()