obsolete/exp_anki.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sat, 14 Apr 2018 23:22:04 +0300
changeset 1044 30985a28d2b1
parent 670 b86e70406247
permissions -rw-r--r--
Added new articles. Fixed some old.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
     1
# -*- coding: utf-8 -*-
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
     2
667
5f69f0776c37 Add missing imports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
     3
import sys
668
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
     4
import os
667
5f69f0776c37 Add missing imports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
     5
import tempfile
668
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
     6
import shutil
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
     7
import traceback
667
5f69f0776c37 Add missing imports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
     8
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
FONAME = "test.apkg"
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
# 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
    12
# 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
    13
FONAME = os.path.abspath(FONAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    14
FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))
668
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
    15
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
    16
CWD = os.getcwd()
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
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
    18
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    19
def cleanup():
668
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
    20
    os.chdir(CWD)
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
    21
    shutil.rmtree(TMPDIR)
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
    22
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    23
def exception_handler(etype, ex, tb):
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    24
    cleanup()
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    25
    traceback.print_exception(etype, ex, tb)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    26
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    27
sys.excepthook = exception_handler
668
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
    28
667
5f69f0776c37 Add missing imports.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
    29
sys.path.append('/usr/share/anki')
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
import anki
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
from anki.exporting import AnkiPackageExporter
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
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
    34
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
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
    36
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
    37
# 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
    38
# collection.decks.update(deck)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
# print(dir(deck))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40
# print(type(deck))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
# print(deck)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
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
    44
model['tags'].append(FBASENAME + "_tag")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
model['did'] = deck_id
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
model['css'] = """
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
.card {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
  font-family: arial;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
  font-size: 20px;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
  text-align: center;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
  color: black;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
  background-color: white;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    53
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    54
.from {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    55
  font-style: italic;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    56
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    57
"""
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    58
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    59
collection.models.addField(model, collection.models.newField('en'))
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    60
collection.models.addField(model, collection.models.newField('ru'))
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    61
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    62
tmpl = collection.models.newTemplate('en -> ru')
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    63
tmpl['qfmt'] = '<div class="from">{{en}}</div>'
670
b86e70406247 Update HTML formatting to make example more realistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 669
diff changeset
    64
tmpl['afmt'] = '{{FrontSide}}<hr id=answer>{{ru}}'
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    65
collection.models.addTemplate(model, tmpl)
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    66
tmpl = collection.models.newTemplate('ru -> en')
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    67
tmpl['qfmt'] = '{{ru}}'
670
b86e70406247 Update HTML formatting to make example more realistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 669
diff changeset
    68
tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="from">{{en}}</div>'
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
collection.models.addTemplate(model, tmpl)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    71
# print(dir(model))
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    72
# print(type(model))
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    73
# print(model)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
# Equivalent of:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
# collection.models.add(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
# without setting auto-generated ID:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
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
    78
collection.models.update(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    79
collection.models.setCurrent(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
collection.models.save(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    82
# collection.decks.select(deck_id)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    83
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
note = anki.notes.Note(collection, model)
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    85
# print(dir(note))
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    86
# print(type(note))
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    87
# print(note._fmap)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    88
# print(note)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    89
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
note['en'] = "hello"
670
b86e70406247 Update HTML formatting to make example more realistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 669
diff changeset
    92
note['ru'] = u"[heləʊ] int. привет"
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    93
note.guid = "enru_1"
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
collection.addNote(note)
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
    95
# print(dir(note))
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    96
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    97
note = collection.newNote()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    98
note['en'] = "bye"
670
b86e70406247 Update HTML formatting to make example more realistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 669
diff changeset
    99
note['ru'] = u"[baɪ] int. пока"
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   100
note.guid = "enru_2"
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   101
collection.addNote(note)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   102
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   103
# model.add(deck)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   104
# model.save()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   105
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   106
################################################################
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   107
# Add another model and new types of notes into same storage!
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   108
model_irrverb = collection.models.new(FBASENAME + "_model_irrverb")
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   109
model_irrverb['tags'].append(FBASENAME + "_tag_irrverb")
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   110
model_irrverb['did'] = deck_id
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   111
model_irrverb['css'] = """
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   112
.card {
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   113
  font-family: arial;
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   114
  font-size: 20px;
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   115
  text-align: center;
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   116
  color: black;
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   117
  background-color: white;
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   118
}
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   119
.v1 { color: red; }
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   120
.v2 { color: green; }
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   121
.v3 { color: blue; }
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   122
"""
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   123
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   124
collection.models.addField(model_irrverb, collection.models.newField('v1'))
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   125
collection.models.addField(model_irrverb, collection.models.newField('v2'))
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   126
collection.models.addField(model_irrverb, collection.models.newField('v3'))
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   127
670
b86e70406247 Update HTML formatting to make example more realistic.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 669
diff changeset
   128
ANSWER_MSG = '{{FrontSide}}<hr id=answer>{{v1}}<br>{{v2}}<br>{{v3}}'
669
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   129
tmpl = collection.models.newTemplate('v1')
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   130
tmpl['qfmt'] = '<div class="v1">{{v1}}</div>'
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   131
tmpl['afmt'] = ANSWER_MSG
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   132
collection.models.addTemplate(model_irrverb, tmpl)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   133
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   134
tmpl = collection.models.newTemplate('v2')
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   135
tmpl['qfmt'] = '<div class="v2">{{v2}}</div>'
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   136
tmpl['afmt'] = ANSWER_MSG
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   137
collection.models.addTemplate(model_irrverb, tmpl)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   138
tmpl = collection.models.newTemplate('v3')
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   139
tmpl['qfmt'] = '<div class="v3">{{v3}}</div>'
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   140
tmpl['afmt'] = ANSWER_MSG
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   141
collection.models.addTemplate(model_irrverb, tmpl)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   142
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   143
# We mustn't define 'id' before adding tmpl because addTemplate() fail because
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   144
# it tries to update DB but we have no DB entry yet...
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   145
model_irrverb['id'] = 12345678+1  # essential for upgrade detection
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   146
collection.models.update(model_irrverb)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   147
collection.models.setCurrent(model_irrverb)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   148
collection.models.save(model_irrverb)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   149
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   150
note = anki.notes.Note(collection, model_irrverb)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   151
note['v1'] = "go"
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   152
note['v2'] = "went"
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   153
note['v3'] = "gone"
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   154
note.guid = "irrverb_1"
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   155
collection.addNote(note)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   156
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   157
note = anki.notes.Note(collection, model_irrverb)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   158
note['v1'] = "drive"
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   159
note['v2'] = "drove"
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   160
note['v3'] = "driven"
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   161
note.guid = "irrverb_2"
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   162
collection.addNote(note)
82da364db365 Add another model and new types of notes into same storage!
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 668
diff changeset
   163
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   164
export = AnkiPackageExporter(collection)
668
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
   165
try:
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
   166
    os.remove(FONAME)
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
   167
except OSError as ex:
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
   168
    if ex.errno not in [2]:
e22ffba8ecc0 Add missing stuff to make this code working.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 667
diff changeset
   169
        raise
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   170
export.exportInto(FONAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   171
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   172
cleanup()