py/gadialog_srs_anki.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Wed, 01 Sep 2021 11:00:18 +0300
changeset 1322 59b94ff17754
parent 1228 e38d1d191339
permissions -rw-r--r--
Fixed: Anki decks were not included into .zip release.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1146
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     2
"""Anki card writer from gaphrase format"""
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
import re
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
import hashlib
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
import os
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
import io
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
import sys
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
import tempfile
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
import shutil
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
1228
e38d1d191339 Fixed import.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1223
diff changeset
    13
from gadialog import Parser
1148
263e9e066981 Fixed: NameError: name 'ParseException' is not defined.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 1146
diff changeset
    14
1146
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
import anki
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
from anki.exporting import AnkiPackageExporter
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
FINAME = None
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
FONAME = None
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
FDELNAME = None
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
ARG_NAME_RE = re.compile("-name=(.+)")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
ARG_DELFILE_RE = re.compile("-delfile=(.+)")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
look_for_files = False
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
for idx in range(1, len(sys.argv)):
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
    arg = sys.argv[idx]
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
    if arg == "--":
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
        look_for_files = True
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
        continue
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
    if not look_for_files:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
        m = ARG_NAME_RE.match(arg)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
        if m:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
            NAME = m.group(1)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
            continue
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    36
        m = ARG_DELFILE_RE.match(arg)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    37
        if m:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    38
            FDELNAME = m.group(1)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
            continue
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40
        if arg.startswith("-"):
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
            raise Exception("Unsupported option format: '{:s}'".format(arg))
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
    if not FINAME:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
        FINAME = arg
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
        continue
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
    if not FONAME:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
        FONAME = arg
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
        continue
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
    raise Exception("Unnecessary argument: '{:s}'".format(arg))
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
if not FINAME:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
    raise Exception("Input file name is not passed...")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
if FONAME is None:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    53
    raise Exception("Output file name is not passed...")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    54
if not NAME:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    55
    NAME, _ = os.path.splitext(os.path.basename(FINAME))
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    56
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    57
# if FDELNAME:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    58
#     FDEL = io.open(FDELNAME, mode='r', buffering=1, encoding="utf-8")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    59
# else:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    60
#     FDEL = None
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    61
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    62
################################################################
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    63
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    64
FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    65
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    66
PARSER = Parser()
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    67
try:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
    DOM = PARSER.parse(FIN)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
finally:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
    FIN.close()
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    71
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
################################################################
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
MODEL_CSS = """
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
.card {
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
  font-family: arial;
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
  font-size: 20px;
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    78
  text-align: left;
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    79
  color: black;
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
  background-color: white;
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
}
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    82
.line {
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    83
  margin-bottom: 0.5em;
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
}
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    85
.odd {
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    86
  color: #004000;
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    87
}
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    88
.even {
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    89
  color: #000080;
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
}
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
"""
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    92
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    93
class AnkiDbBuilder:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    95
    def __init__(self, tmpdir, name):
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    96
        self.tmpdir = tmpdir
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    97
        self.name = name
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    98
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    99
        self.collection = collection = anki.Collection(os.path.join(self.tmpdir, 'collection.anki2'))
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   100
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   101
        deck_id = collection.decks.id(self.name)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   102
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   103
        # It is essential to keep model['id'] unchanged between upgrades!!
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   104
        model_id = int(hashlib.sha1(self.name.encode('utf-8')).hexdigest(), 16) % (2**63)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   105
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   106
        ################################################################
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   107
        # One face card model.
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   108
        model = collection.models.new(self.name + "_front")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   109
        model['did'] = deck_id
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   110
        model['css'] = MODEL_CSS
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   111
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   112
        collection.models.addField(model, collection.models.newField('Front'))
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   113
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   114
        tmpl = collection.models.newTemplate('Front')
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   115
        tmpl['qfmt'] = '<div class="front">{{Front}}</div>'
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   116
        tmpl['afmt'] = '{{FrontSide}}'
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   117
        collection.models.addTemplate(model, tmpl)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   118
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   119
        model['id'] = model_id
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   120
        collection.models.update(model)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   121
        collection.models.save(model)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   122
        self.model = model
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   123
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   124
    def guid(self, num):
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   125
        h = hashlib.md5(":".join((self.name, str(num))).encode('utf-8'))
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   126
        return h.hexdigest()
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   127
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
    def add_note(self, num, front):
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   129
        note = anki.notes.Note(self.collection, self.model)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130
        note['Front'] = front
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   131
        note.guid = self.guid(num)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   132
        self.collection.addNote(note)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   133
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   134
    def export(self, fname):
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   135
        export = AnkiPackageExporter(self.collection)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   136
        export.exportInto(fname)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   137
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   138
    def close(self):
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   139
        self.collection.close()
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   140
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   141
def write_lines(buf, lines):
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   142
    odd = True
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   143
    for line in lines:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   144
        if odd:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   145
            buf.append("<div class='line odd'>")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   146
        else:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   147
            buf.append("<div class='line even'>")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   148
        buf.append("- ")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   149
        buf.append(line)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   150
        buf.append("</div>")
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   151
        odd = not odd
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   152
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   153
# Looks like anki libs change working directory to media directory of current deck
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   154
# Therefore absolute path should be stored before creating temporary deck
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   155
FONAME = os.path.abspath(FONAME)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   156
TMPDIR = tempfile.mkdtemp(dir=os.path.dirname(FONAME))
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   157
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   158
try:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   159
    BUILDER = AnkiDbBuilder(TMPDIR, NAME)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   160
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   161
    for num, lines in DOM.items():
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   162
        buf = []
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   163
        write_lines(buf, lines)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   164
        front = "".join(buf)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   165
        BUILDER.add_note(num, front)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   166
    BUILDER.export(FONAME)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   167
finally:
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   168
    BUILDER.close()
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   169
    shutil.rmtree(TMPDIR, ignore_errors=True)
50007cd95972 Added generation of Anki cards with mini-dialogs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   170