py/gadict_srs_anki.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sun, 07 Apr 2019 23:55:26 +0300
changeset 1128 a1fb78029d20
parent 1008 dc218f2b784d
child 1215 d7394ed6693d
permissions -rw-r--r--
Updated release notes.
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 -*-
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
     2
"""Anki card writer"""
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
import os
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
import io
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
import sys
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
872
b201a02dc707 'regex' module isn't easy to install on Cygwin. Stick to standard 're' module.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 750
diff changeset
    10
import re
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
    12
import hashlib
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
    13
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
    14
import anki
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
    15
from anki.exporting import AnkiPackageExporter
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
    16
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
import gadict
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    18
import gadict_freq
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
FINAME = None
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
FONAME = None
674
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    23
FDELNAME = None
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    24
NAME = None
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
LANGS = None
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    26
FREQ_SOURCES = []
673
ca1d4a6e201b Option to include Irregular verb and plural form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 657
diff changeset
    27
RICH_MODE = False
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    28
872
b201a02dc707 'regex' module isn't easy to install on Cygwin. Stick to standard 're' module.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 750
diff changeset
    29
ARG_NAME_RE = re.compile("-name=(.+)")
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    30
# -lang:ru,uk
872
b201a02dc707 'regex' module isn't easy to install on Cygwin. Stick to standard 're' module.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 750
diff changeset
    31
ARG_LANG_RE = re.compile("-lang:(.+)")
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    32
# -freq:var:TAG=FILE or -freq:freq:TAG=FILE
872
b201a02dc707 'regex' module isn't easy to install on Cygwin. Stick to standard 're' module.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 750
diff changeset
    33
ARG_FREQ_RE = re.compile("-freq:(freq|var):([^=]+)=(.+)")
b201a02dc707 'regex' module isn't easy to install on Cygwin. Stick to standard 're' module.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 750
diff changeset
    34
ARG_DELFILE_RE = re.compile("-delfile=(.+)")
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    35
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    36
look_for_files = False
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    37
for idx in range(1, len(sys.argv)):
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    38
    arg = sys.argv[idx]
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    39
    if arg == "--":
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    40
        look_for_files = True
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    41
        continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    42
    if not look_for_files:
674
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    43
        m = ARG_NAME_RE.match(arg)
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    44
        if m:
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    45
            NAME = m.group(1)
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    46
            continue
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    47
        m = ARG_LANG_RE.match(arg)
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    48
        if m:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    49
            LANGS = set(m.group(1).split(","))
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    50
            for lang in LANGS:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    51
                if len(lang) != 2:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    52
                    raise Exception("Incorrect language specification: '{:s}'".format(arg))
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    53
            continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    54
        m = ARG_FREQ_RE.match(arg)
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    55
        if m:
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    56
            mode = m.group(1)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    57
            tag = m.group(2)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    58
            fname = m.group(3)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    59
            with io.open(fname, mode='r', buffering=1, encoding="utf-8") as stream:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    60
                if mode == "var":
736
e7f0c03887e1 Fix after rename.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 713
diff changeset
    61
                    parser = gadict_freq.WordformParser(stream)
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    62
                elif mode == "freq":
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    63
                    parser = gadict_freq.FreqlistParser(stream)
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    64
                else:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    65
                    raise Exception("Unsupported mode: '{:s}'".format(mode))
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    66
                wlist = parser.parse()
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
    67
            FREQ_SOURCES.append((tag, set(wlist)))
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    68
            continue
674
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    69
        m = ARG_DELFILE_RE.match(arg)
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    70
        if m:
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    71
            FDELNAME = m.group(1)
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    72
            continue
673
ca1d4a6e201b Option to include Irregular verb and plural form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 657
diff changeset
    73
        if arg == "-rich":
ca1d4a6e201b Option to include Irregular verb and plural form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 657
diff changeset
    74
            RICH_MODE = True
ca1d4a6e201b Option to include Irregular verb and plural form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 657
diff changeset
    75
            continue
644
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    76
        if arg.startswith("-"):
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    77
            raise Exception("Unsupported option format: '{:s}'".format(arg))
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    78
    if not FINAME:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    79
        FINAME = arg
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    80
        continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    81
    if not FONAME:
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    82
        FONAME = arg
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    83
        continue
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    84
    raise Exception("Unnecessary argument: '{:s}'".format(arg))
e38cd6112193 New command line format for passing language list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 618
diff changeset
    85
674
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    86
if not FINAME:
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
    87
    raise Exception("Input file name is not passed...")
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    88
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
    89
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    90
PARSER = gadict.Parser()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    91
try:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    92
    DOM = PARSER.parse(FIN)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    93
except gadict.ParseException as ex:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    94
    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
    95
    if __debug__:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    96
        import traceback
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    97
        traceback.print_exc()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    98
    exit(1)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    99
finally:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   100
    FIN.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   101
699
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   102
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   103
def cleanup(collection, tmpdir):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   104
    if collection:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   105
        collection.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   106
    if tmpdir:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   107
        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
   108
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   109
# 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
   110
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   111
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   112
if FONAME is None:
674
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
   113
    raise Exception("Output file name is not passed...")
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   114
# 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
   115
# 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
   116
FONAME = os.path.abspath(FONAME)
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   117
TMPDIR = tempfile.mkdtemp(dir=os.path.dirname(FONAME))
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   118
674
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
   119
if not NAME:
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
   120
    NAME, _ = os.path.splitext(os.path.basename(FINAME))
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
   121
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
   122
if FDELNAME:
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
   123
    FDEL = io.open(FDELNAME, mode='r', buffering=1, encoding="utf-8")
fe57401d8109 Externalize deck name and delete file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 673
diff changeset
   124
else:
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   125
    FDEL = None
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   126
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   127
################################################################
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   128
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   129
MODEL_CSS = """
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   130
.card {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   131
  font-family: arial;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   132
  font-size: 20px;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   133
  text-align: center;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   134
  color: black;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   135
  background-color: white;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   136
}
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   137
.ask {
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   138
  margin-bottom: 1.2em;
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   139
}
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   140
span.headword {
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   141
  font-weight: bolt;
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   142
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   143
.pron {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   144
  color: magenta;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   145
}
931
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   146
.attrs {
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   147
  color: blue;
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   148
}
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   149
.homo {
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   150
  font-style: italic;
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   151
}
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   152
.homo i {
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   153
  color: green;
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   154
}
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   155
.pos {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   156
  color: green;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   157
  font-style: italic;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   158
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   159
.lang {
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   160
  color: red;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   161
  font-style: italic;
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   162
}
618
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   163
.topic {
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   164
  color: #B04080;
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   165
}
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   166
.ant {
565
ac68f2680ea0 Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 562
diff changeset
   167
  color: #404080;
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   168
}
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   169
.syn {
565
ac68f2680ea0 Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 562
diff changeset
   170
  color: #804040;
ac68f2680ea0 Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 562
diff changeset
   171
}
ac68f2680ea0 Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 562
diff changeset
   172
.rel {
618
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   173
  color: #604080;
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   174
}
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   175
.hyper {
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   176
  color: #600080;
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   177
}
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   178
.hypo {
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   179
  color: #606080;
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   180
}
560
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   181
span.ex {
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   182
  font-style: italic;
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   183
  font-size: .85em;
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   184
}
569
5709d5bd349b Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 566
diff changeset
   185
div.glos .lang {
5709d5bd349b Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 566
diff changeset
   186
  color: brown;
5709d5bd349b Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 566
diff changeset
   187
}
566
0bba61492c37 Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 565
diff changeset
   188
span.glos {
569
5709d5bd349b Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 566
diff changeset
   189
  font-size: .95em;
566
0bba61492c37 Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 565
diff changeset
   190
}
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   191
.freq {
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   192
  color: red;
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   193
  font-weight: bold;
650
b13c09aaa9a3 Fix typo and decrease font size of frequency list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 647
diff changeset
   194
  font-size: small;
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   195
}
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   196
.del {
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   197
  color: red;
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   198
  font-weight: bold;
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   199
}
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   200
"""
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   201
699
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   202
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   203
def note_add_tags(note, tags):
699
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   204
    if isinstance(tags, str):
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   205
        note.tags = [tags]
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   206
    elif isinstance(tags, list):
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   207
        note.tags = tags
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   208
    elif tags is None:
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   209
        pass
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   210
    else:
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   211
        raise Exception('Expecting string or list of tags...')
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   212
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   213
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   214
class AnkiDbBuilder:
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   215
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   216
    def __init__(self, tmpdir, name):
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   217
        self.tmpdir = tmpdir
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   218
        self.name = name
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   219
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   220
        self.collection = collection = anki.Collection(os.path.join(self.tmpdir, 'collection.anki2'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   221
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   222
        deck_id = collection.decks.id(self.name)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   223
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   224
        # It is essential to keep model['id'] unchanged between upgrades!!
1008
dc218f2b784d Ported build of Anki apkg to Python 3.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 931
diff changeset
   225
        model_id = int(hashlib.sha1(self.name.encode('utf-8')).hexdigest(), 16) % (2**63)
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   226
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   227
        ################################################################
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   228
        # Regular card model. SafeBack doesn't include examples to not spoil
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   229
        # word spelling.
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   230
        model = collection.models.new(self.name + "_frontback")
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   231
        model['did'] = deck_id
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   232
        model['css'] = MODEL_CSS
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   233
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   234
        collection.models.addField(model, collection.models.newField('Front'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   235
        collection.models.addField(model, collection.models.newField('Back'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   236
        collection.models.addField(model, collection.models.newField('SafeBack'))
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   237
        collection.models.addField(model, collection.models.newField('Freq'))
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   238
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   239
        tmpl = collection.models.newTemplate('Front -> Back')
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   240
        tmpl['qfmt'] = '<div class="front">{{Front}}</div><div class="freq">{{Freq}}</div>'
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   241
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Back}}</div>'
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   242
        collection.models.addTemplate(model, tmpl)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   243
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   244
        tmpl = collection.models.newTemplate('SafeBack -> Front')
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   245
        tmpl['qfmt'] = '<div class="safe-back">{{SafeBack}}</div>'
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   246
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="front">{{Front}}</div><div class="freq">{{Freq}}</div>'
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   247
        collection.models.addTemplate(model, tmpl)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   248
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   249
        # Equivalent of ``collection.models.add(model)`` without setting
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   250
        # auto-generated ID.
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   251
        # Increment +1 is for keeping model['id'] unique from previous v0.9 release.
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   252
        model['id'] = model_id + 1
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   253
        collection.models.update(model)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   254
        collection.models.save(model)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   255
        self.model = model
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   256
        # collection.models.setCurrent(model)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   257
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   258
        if not RICH_MODE:
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   259
            return
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   260
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   261
        ################################################################
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   262
        # Model for irregular verbs.
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   263
        model = collection.models.new(self.name + "_irrverb")
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   264
        model['did'] = deck_id
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   265
        model['css'] = MODEL_CSS
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   266
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   267
        collection.models.addField(model, collection.models.newField('V1'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   268
        collection.models.addField(model, collection.models.newField('V2'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   269
        collection.models.addField(model, collection.models.newField('V3'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   270
        collection.models.addField(model, collection.models.newField('V2alt'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   271
        collection.models.addField(model, collection.models.newField('V3alt'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   272
        collection.models.addField(model, collection.models.newField('Front'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   273
        collection.models.addField(model, collection.models.newField('Back'))
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   274
        collection.models.addField(model, collection.models.newField('Freq'))
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   275
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   276
        question = u"<div class='ask'>Find irregular verb:</div>"
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   277
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   278
        tmpl = collection.models.newTemplate('V1 -> Back')
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   279
        tmpl['qfmt'] = question + '<div class="front">{{V1}}</div>'
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   280
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   281
        collection.models.addTemplate(model, tmpl)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   282
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   283
        tmpl = collection.models.newTemplate('V2 -> Back')
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   284
        tmpl['qfmt'] = question + '<div class="front">{{V2}}</div>'
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   285
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   286
        collection.models.addTemplate(model, tmpl)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   287
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   288
        tmpl = collection.models.newTemplate('V3 -> Back')
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   289
        tmpl['qfmt'] = question + '<div class="front">{{V3}}</div>'
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   290
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   291
        collection.models.addTemplate(model, tmpl)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   292
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   293
        tmpl = collection.models.newTemplate('V2alt -> Back')
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   294
        tmpl['qfmt'] = question + '<div class="front">{{V2alt}}</div>'
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   295
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   296
        collection.models.addTemplate(model, tmpl)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   297
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   298
        tmpl = collection.models.newTemplate('V3alt -> Back')
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   299
        tmpl['qfmt'] = question + '<div class="front">{{V3alt}}</div>'
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   300
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   301
        collection.models.addTemplate(model, tmpl)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   302
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   303
        model['id'] = model_id + 2          # Keep model['id'] unique.
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   304
        collection.models.update(model)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   305
        collection.models.save(model)
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   306
        self.model_irr = model
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   307
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   308
        ################################################################
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   309
        # Model for irregular plurals.
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   310
        model = collection.models.new(self.name + "_plural")
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   311
        model['did'] = deck_id
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   312
        model['css'] = MODEL_CSS
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   313
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   314
        collection.models.addField(model, collection.models.newField('Singular'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   315
        collection.models.addField(model, collection.models.newField('Plural'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   316
        collection.models.addField(model, collection.models.newField('Front'))
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   317
        collection.models.addField(model, collection.models.newField('Back'))
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   318
        collection.models.addField(model, collection.models.newField('Freq'))
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   319
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   320
        question = u"<div class='ask'>Find singular/plural form:</div>"
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   321
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   322
        tmpl = collection.models.newTemplate('Singular -> Back')
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   323
        tmpl['qfmt'] = question + '<div class="front">{{Singular}}</div>'
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   324
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   325
        collection.models.addTemplate(model, tmpl)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   326
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   327
        tmpl = collection.models.newTemplate('Plural -> Back')
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   328
        tmpl['qfmt'] = question + '<div class="front">{{Plural}}</div>'
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   329
        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   330
        collection.models.addTemplate(model, tmpl)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   331
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   332
        model['id'] = model_id + 3          # Keep model['id'] unique.
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   333
        collection.models.update(model)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   334
        collection.models.save(model)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   335
        self.model_pl = model
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   336
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   337
    def guid(self, nodetype, headword, unambiguous):
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   338
        """
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   339
        :nodetype     used to generate different notes from same headword
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   340
        :unambiguous  used if several subsequent article with same headword (and different pronunciation)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   341
        """
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   342
        if unambiguous > 0:
1008
dc218f2b784d Ported build of Anki apkg to Python 3.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 931
diff changeset
   343
            h = hashlib.md5(":".join((self.name, nodetype, headword, str(unambiguous))).encode('utf-8'))
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   344
        else:
1008
dc218f2b784d Ported build of Anki apkg to Python 3.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 931
diff changeset
   345
            h = hashlib.md5(":".join((self.name, nodetype, headword)).encode('utf-8'))
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   346
        return h.hexdigest()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   347
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   348
    def add_note(self, headword, unambiguous, front, back, safeback, freq="", tags=None):
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   349
        note = anki.notes.Note(self.collection, self.model)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   350
        note['Front'] = front
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   351
        note['Back'] = back
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   352
        note['SafeBack'] = safeback
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   353
        note['Freq'] = freq
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   354
        note_add_tags(note, tags)
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   355
        note.guid = self.guid("front/back", headword, unambiguous)
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   356
        self.collection.addNote(note)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   357
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   358
    def add_note_irr(self, headword, unambiguous, v1, v2, v2alt, v3, v3alt, front, back, freq, tags=None):
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   359
        note = anki.notes.Note(self.collection, self.model_irr)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   360
        note['V1'] = v1
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   361
        note['V2'] = v2
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   362
        note['V3'] = v3
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   363
        note['V2alt'] = v2alt
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   364
        note['V3alt'] = v3alt
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   365
        note['Front'] = front
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   366
        note['Back'] = back
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   367
        note['Freq'] = freq
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   368
        note_add_tags(note, tags)
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   369
        note.guid = self.guid("irregular verb", headword, unambiguous)
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   370
        self.collection.addNote(note)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   371
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   372
    def add_note_pl(self, headword, unambiguous, singular, plural, front, back, freq, tags=None):
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   373
        note = anki.notes.Note(self.collection, self.model_pl)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   374
        note['Singular'] = singular
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   375
        note['Plural'] = plural
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   376
        note['Front'] = front
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   377
        note['Back'] = back
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   378
        note['Freq'] = freq
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   379
        note_add_tags(note, tags)
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   380
        note.guid = self.guid("singular/plural noun", headword, unambiguous)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   381
        self.collection.addNote(note)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   382
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   383
    def export(self, fname):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   384
        export = AnkiPackageExporter(self.collection)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   385
        export.exportInto(fname)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   386
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   387
    def close(self):
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   388
        self.collection.close()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   389
699
38ff2e2ffd5c Fix warning from pep8: E701 multiple statements on one line (colon).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 698
diff changeset
   390
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   391
def write_sense(buf, sense, with_examples=True):
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   392
    buf.append("<div class='sense'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   393
    if sense.pos:
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   394
        buf.append("<span class='pos'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   395
        buf.append(sense.pos)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   396
        buf.append("</span>")
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   397
    have_ref = False
686
a9e3b6050544 Add topic to dictd and Anki.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 678
diff changeset
   398
    if sense.topic_list and len(sense.topic_list) > 0:
a9e3b6050544 Add topic to dictd and Anki.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 678
diff changeset
   399
        have_ref = True
877
340482715bb0 LEFT/RIGHT FLOOR symbols aren't present in Windows default fonts, replaced
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 872
diff changeset
   400
        buf.append(u" <span class='topic'>【")
686
a9e3b6050544 Add topic to dictd and Anki.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 678
diff changeset
   401
        buf.append(u", ".join(sense.topic_list))
877
340482715bb0 LEFT/RIGHT FLOOR symbols aren't present in Windows default fonts, replaced
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 872
diff changeset
   402
        buf.append(u"】</span>")
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   403
    if sense.ant_list and len(sense.ant_list) > 0:
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   404
        have_ref = True
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   405
        buf.append(" <span class='ant'>ant: ")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   406
        buf.append("; ".join(sense.ant_list))
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   407
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   408
    if sense.syn_list and len(sense.syn_list) > 0:
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   409
        have_ref = True
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   410
        buf.append(" <span class='syn'>syn: ")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   411
        buf.append("; ".join(sense.syn_list))
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   412
        buf.append("</span>")
618
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   413
    if sense.hyper_list and len(sense.hyper_list) > 0:
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   414
        have_ref = True
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   415
        buf.append(" <span class='hyper'>hyper: ")
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   416
        buf.append("; ".join(sense.hyper_list))
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   417
        buf.append("</span>")
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   418
    if sense.hypo_list and len(sense.hypo_list) > 0:
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   419
        have_ref = True
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   420
        buf.append(" <span class='hypo'>hypo: ")
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   421
        buf.append("; ".join(sense.hypo_list))
6ad7203ac9dc Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 608
diff changeset
   422
        buf.append("</span>")
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   423
    if sense.rel_list and len(sense.rel_list) > 0:
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   424
        have_ref = True
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   425
        buf.append(" <span class='rel'>rel: ")
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   426
        buf.append("; ".join(sense.rel_list))
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   427
        buf.append("</span>")
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   428
    for (lang, tr) in sense.tr_list or []:
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   429
        if len(sense.tr_list) > 1 or have_ref:
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   430
            buf.append("<div class='sense'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   431
        buf.append(" <span class='lang'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   432
        buf.append(lang)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   433
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   434
        buf.append(" <span class='tr'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   435
        buf.append(tr)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   436
        buf.append("</span>")
574
5478477ab2ae Add related words to Anki cards. Break line if any reference is provided.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 570
diff changeset
   437
        if len(sense.tr_list) > 1 or have_ref:
570
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   438
            buf.append("</div>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   439
    for (lang, tr) in sense.glos_list or []:
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   440
        buf.append("<div class='glos'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   441
        buf.append(" <span class='lang'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   442
        buf.append(lang)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   443
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   444
        buf.append(" <span class='glos'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   445
        buf.append(tr)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   446
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   447
        buf.append("</div>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   448
    for (lang, tr) in (with_examples and sense.ex_list) or []:
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   449
        buf.append("<div class='ex'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   450
        buf.append(" <span class='lang'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   451
        buf.append(lang)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   452
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   453
        buf.append(" <span class='ex'>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   454
        buf.append(tr)
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   455
        buf.append("</span>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   456
        buf.append("</div>")
181897a421e9 Remove code duplication.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 569
diff changeset
   457
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   458
try:
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   459
    BUILDER = AnkiDbBuilder(TMPDIR, NAME)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   460
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   461
    for identity in FDEL or []:
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   462
        identity = identity.strip()
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   463
        warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity)
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   464
        BUILDER.add_note(identity, 0, warnmsg, warnmsg, warnmsg, tags="del")
750
b68dfe0c2804 Add missing argument to call. It happened in untested execution branch.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 739
diff changeset
   465
        BUILDER.add_note_irr(identity, 0, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, freq="", tags="del")
b68dfe0c2804 Add missing argument to call. It happened in untested execution branch.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 739
diff changeset
   466
        BUILDER.add_note_pl(identity, 0, warnmsg, warnmsg, warnmsg, warnmsg, freq="", tags="del")
608
01fcbb1d5cea Add a way to mark card for deletion.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 574
diff changeset
   467
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   468
    prev_identity = None
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   469
    unambiguous = 0
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   470
    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
   471
        identity = headwords[0].headword
650
b13c09aaa9a3 Fix typo and decrease font size of frequency list.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 647
diff changeset
   472
        if 'rare' in headwords[0].attrs:
647
6ae5399c8087 Add ``rare`` attribute to headword to filter low frequency headwords out from
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 646
diff changeset
   473
            continue
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   474
        freqtags = []
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   475
        for (freqtag, freqset) in FREQ_SOURCES:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   476
            if identity in freqset:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   477
                freqtags.append(freqtag)
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   478
        if prev_identity == identity:
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   479
            unambiguous += 1
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   480
        else:
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   481
            prev_identity = identity
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   482
            unambiguous = 0
678
28e04408c0c0 Add frequency tags as separate field and as native Anki tags.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 677
diff changeset
   483
        freqmsg = " "
646
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   484
        if len(freqtags) > 0:
2d488cfc4c0c Add frequency markers to dictd dictionary and Anki cards.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 644
diff changeset
   485
            freqmsg = ",".join(freqtags)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   486
        buf = []
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   487
        v1, v2, v3, v2alt, v3alt = (None, None, None, None, None)
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   488
        singular, plural = (None, None)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   489
        for hw in headwords:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   490
            buf.append("<div clsas='headword'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   491
            buf.append("<span clsas='headword'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   492
            buf.append(hw.headword)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   493
            buf.append("</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   494
            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
   495
                buf.append(" <span class='pron'>[")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   496
                buf.append(hw.pron)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   497
                buf.append("]</span>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   498
            if len(hw.attrs) > 0:
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   499
                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
   500
                l.sort()
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   501
                buf.append("<span class='attrs'>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   502
                buf.append(", ".join(l))
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   503
                buf.append("</span>")
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   504
            if 'v1' in hw.attrs:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   505
                v1 = (hw.headword, hw.pron)
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   506
            if 'v2' in hw.attrs:
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   507
                if v2:
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   508
                    v2alt = (hw.headword, hw.pron)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   509
                else:
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   510
                    v2 = (hw.headword, hw.pron)
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   511
            if 'v3' in hw.attrs:
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   512
                if v3:
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   513
                    v3alt = (hw.headword, hw.pron)
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   514
                else:
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   515
                    v3 = (hw.headword, hw.pron)
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   516
            if 's' in hw.attrs:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   517
                singular = (hw.headword, hw.pron)
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   518
            if 'pl' in hw.attrs:
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   519
                plural = (hw.headword, hw.pron)
931
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   520
            if hw.homo is not None and len(hw.homo) > 0:
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   521
                buf.append("<span class='homo'> <i>homo:</i>")
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   522
                buf.append("; ".join(hw.homo))
9a5f97027ee7 Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 877
diff changeset
   523
                buf.append("</span>")
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   524
            buf.append("</div>")
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   525
        direct_from = "".join(buf)
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   526
        buf = []
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   527
        for sense in translations:
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   528
            write_sense(buf, sense, with_examples=True)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   529
        direct_to = "".join(buf)
560
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   530
        buf = []
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   531
        for sense in translations:
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   532
            write_sense(buf, sense, with_examples=False)
560
dad049a0f585 Include examples into dictionary articles and Packaged Anki Desk files.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 559
diff changeset
   533
        reverse_from = "".join(buf)         # without examples!!
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   534
        BUILDER.add_note(identity, unambiguous, direct_from, direct_to, reverse_from, freqmsg)
673
ca1d4a6e201b Option to include Irregular verb and plural form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 657
diff changeset
   535
        if v1 and v2 and v3 and RICH_MODE:
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   536
            riddle1 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v1</span>".format(v1[0], v1[1])
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   537
            riddle2 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2[0], v2[1])
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   538
            riddle3 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3[0], v3[1])
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   539
            if v2alt:
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   540
                riddle2alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2alt[0], v2alt[1])
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   541
            else:
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   542
                riddle2alt = u""
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   543
            if v3alt:
713
c6cb8d720b51 Fix typo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 699
diff changeset
   544
                riddle3alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3alt[0], v3alt[1])
677
e330e7f195b7 Switched to new models for notes in order to use Anki feature of preventing
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 676
diff changeset
   545
            else:
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   546
                riddle3alt = u""
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   547
            BUILDER.add_note_irr(identity, unambiguous, riddle1, riddle2, riddle2alt, riddle3, riddle3alt, direct_from, direct_to, freqmsg)
673
ca1d4a6e201b Option to include Irregular verb and plural form.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 657
diff changeset
   548
        if singular and plural and RICH_MODE:
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   549
            riddle_s = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>s</span>".format(singular[0], singular[1])
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   550
            riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1])
739
4c70daa48492 Handling for sequence of article with same headword (as workaround for
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 736
diff changeset
   551
            BUILDER.add_note_pl(identity, unambiguous, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
556
78bf1097106a Add question for plural form and irregular verbs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 555
diff changeset
   552
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   553
    BUILDER.export(FONAME)
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   554
finally:
698
5fc9bc68fb9c Fix warning from pylint.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents: 697
diff changeset
   555
    BUILDER.close()
555
4a3188fc8951 Generating Packaged Anki Desk files for Anki space repetition software.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
   556
    shutil.rmtree(TMPDIR, ignore_errors=True)