py/gadict_srs_anki.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Thu, 15 Sep 2016 19:19:34 +0300
changeset 555 4a3188fc8951
child 556 78bf1097106a
permissions -rw-r--r--
Generating Packaged Anki Desk files for Anki space repetition software.
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
# -*- coding: utf-8 -*-
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
import os
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
import io
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
import sys
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
import codecs
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
import tempfile
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
import shutil
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
import signal
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
import gadict
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
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
FINAME = None
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
FONAME = None
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
if len(sys.argv) >= 2:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
    FINAME = sys.argv[1]
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
if len(sys.argv) >= 3:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
    FONAME = sys.argv[2]
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
LANGS = None
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
if len(sys.argv) >= 4:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
    LANGS = set(sys.argv[3].split(","))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
PARSER = gadict.Parser()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
try:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
    DOM = PARSER.parse(FIN)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
except gadict.ParseException as ex:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
    sys.stdout.write("{:s}{:s}\n".format(FINAME, repr(ex)))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
    if __debug__:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
        import traceback
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
        traceback.print_exc()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
    exit(1)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
finally:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    36
    FIN.close()
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
def cleanup(collection, tmpdir):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
    if collection:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40
        collection.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
    if tmpdir:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
        shutil.rmtree(tmpdir, ignore_errors=True)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
# signal.signal(signal.SIGINT, lambda signal, frame: cleanup())
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
if FONAME is None:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
    raise Exception('Missing output file name')
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
# 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
    50
# 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
    51
FONAME = os.path.abspath(FONAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
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
    53
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
    54
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    55
import hashlib
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
import anki
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    58
from anki.exporting import AnkiPackageExporter
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    59
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    60
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    61
class AnkiDbBuilder:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    62
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    63
    def __init__(self, tmpdir, name):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    64
        self.tmpdir = tmpdir
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    65
        self.name = name
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    66
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    67
        self.collection = collection = anki.Collection(os.path.join(self.tmpdir, 'collection.anki2'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    68
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    69
        deck_id = collection.decks.id(self.name + "_deck")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    70
        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
    71
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    72
        model = collection.models.new(self.name + "_model")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    73
        model['tags'].append(self.name + "_tag")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    74
        model['did'] = deck_id
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    75
        model['css'] = """
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    76
.card {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    77
  font-family: arial;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    78
  font-size: 20px;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    79
  text-align: center;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    80
  color: black;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    81
  background-color: white;
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
span.headword {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    84
  font-style: italic;
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
.pron {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    87
  color: magenta;
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
.pos {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
  color: green;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
  font-style: italic;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    92
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    93
.lang {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
  color: red;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    95
  font-style: italic;
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
.ant {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    98
  color: red;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    99
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   100
.syn {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   101
  color: blue;
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
"""
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   104
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   105
        collection.models.addField(model, collection.models.newField('From'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   106
        collection.models.addField(model, collection.models.newField('To'))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   107
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   108
        tmpl = collection.models.newTemplate('From -> To')
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   109
        tmpl['qfmt'] = '<div class="from">{{From}}</div>'
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   110
        tmpl['afmt'] = '{{FrontSide}}\n\n<hr id=answer>\n\n<div class="to">{{To}}</div>'
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   111
        collection.models.addTemplate(model, tmpl)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   112
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   113
        # Equivalent of:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   114
        #   collection.models.add(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   115
        # without setting auto-generated ID. It is essential to keep model['id']
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   116
        # unchanged between upgrades or notes will be skipped!!
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   117
        model['id'] = int(hashlib.sha1(self.name).hexdigest(), 16) % (2**63)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   118
        collection.models.update(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   119
        collection.models.setCurrent(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   120
        collection.models.save(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   121
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   122
    def guid(self, type_, headword):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   123
        """
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   124
        :type_ used to generate different notes from same headword
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   125
        """
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   126
        h = hashlib.md5(":".join((self.name, type_, headword)))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   127
        return h.hexdigest()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   129
    def add_note(self, type_, id_, from_, to_):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130
        note = self.collection.newNote()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   131
        note['From'] = from_
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   132
        # print(from_)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   133
        note['To'] = to_
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   134
        # print(to_)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   135
        note.guid = self.guid(type_, id_)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   136
        self.collection.addNote(note)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   137
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   138
    def export(self, fname):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   139
        export = AnkiPackageExporter(self.collection)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   140
        export.exportInto(fname)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   141
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   142
    def close(self):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   143
        self.collection.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   144
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   145
try:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   146
    builder = AnkiDbBuilder(TMPDIR, FBASENAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   147
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   148
    for (headwords, translations) in DOM[1:]:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   149
        identity = headwords[0].headword
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   150
        buf = []
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   151
        for hw in headwords:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   152
            buf.append("<div clsas='headword'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   153
            buf.append("<span clsas='headword'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   154
            buf.append(hw.headword)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   155
            buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   156
            if hw.pron is not None:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   157
                buf.append(" <span class='pron'>[")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   158
                buf.append(hw.pron)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   159
                buf.append("]</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   160
            if len(hw.attrs) > 0:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   161
                l = [u"«"+x+u"»" for x in hw.attrs]
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   162
                l.sort()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   163
                buf.append("<span class='attrs'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   164
                buf.append(", ".join(l))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   165
                buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   166
            buf.append("</div>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   167
        direct_from = "".join(buf)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   168
        buf = []
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   169
        for sense in translations:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   170
            buf.append("<div class='sense'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   171
            if sense.pos:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   172
                buf.append("<span class='pos'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   173
                buf.append(sense.pos)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   174
                buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   175
            if sense.ant_list and len(sense.ant_list) > 0:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   176
                buf.append("<span class='ant'>ant: ")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   177
                buf.append("; ".join(["{"+s+"}" for s in sense.ant_list]))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   178
                buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   179
            if sense.syn_list and len(sense.syn_list) > 0:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   180
                buf.append("<span class='syn'>syn: ")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   181
                buf.append("; ".join(["{"+s+"}" for s in sense.syn_list]))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   182
                buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   183
            if len(sense.tr_list) > 1:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   184
                for (lang, tr) in sense.tr_list:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   185
                    buf.append("<div class='sense'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   186
                    buf.append(" <span class='lang'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   187
                    buf.append(lang)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   188
                    buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   189
                    buf.append(" <span class='tr'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   190
                    buf.append(tr)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   191
                    buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   192
                    buf.append("</div>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   193
            else:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   194
                for (lang, tr) in sense.tr_list:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   195
                    buf.append(" <span class='lang'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   196
                    buf.append(lang)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   197
                    buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   198
                    buf.append(" <span class='tr'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   199
                    buf.append(tr)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   200
                    buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   201
        direct_to = "".join(buf)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   202
        builder.add_note("en->tr", identity, direct_from, direct_to)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   203
        builder.add_note("tr->en", identity, direct_to, direct_from)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   204
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   205
    builder.export(FONAME)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   206
finally:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   207
    builder.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   208
    shutil.rmtree(TMPDIR, ignore_errors=True)