py/gadict_srs_anki.py
changeset 646 2d488cfc4c0c
parent 644 e38cd6112193
child 647 6ae5399c8087
--- a/py/gadict_srs_anki.py	Tue Nov 08 18:12:50 2016 +0200
+++ b/py/gadict_srs_anki.py	Tue Nov 08 19:01:27 2016 +0200
@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+"""Anki card writer"""
 
 import os
 import io
@@ -10,11 +11,13 @@
 import regex
 
 import gadict
+import gadict_freq
 
 
 FINAME = None
 FONAME = None
 LANGS = None
+FREQ_SOURCES = []
 
 # -lang:ru,uk
 ARG_LANG_RE = regex.compile("-lang:(.+)")
@@ -37,10 +40,18 @@
             continue
         m = ARG_FREQ_RE.match(arg)
         if m:
-            LANGS = set(arg.split(","))
-            for lang in LANGS:
-                if len(lang) != 2:
-                    raise Exception("Incorrect language specification: '{:s}'".format(arg))
+            mode = m.group(1)
+            tag = m.group(2)
+            fname = m.group(3)
+            with io.open(fname, mode='r', buffering=1, encoding="utf-8") as stream:
+                if mode == "var":
+                    parser = gadict_freq.HeadVarParser(stream)
+                elif mode == "freq":
+                    parser = gadict_freq.FreqlistParser(stream)
+                else:
+                    raise Exception("Unsupported mode: '{:s}'".format(mode))
+                wlist = parser.parse()
+            FREQ_SOURCES.append((tag, set(wlist)))
             continue
         if arg.startswith("-"):
             raise Exception("Unsupported option format: '{:s}'".format(arg))
@@ -166,6 +177,10 @@
 span.glos {
   font-size: .95em;
 }
+.freq {
+  color: red;
+  font-weight: bold;
+}
 .del {
   color: red;
   font-weight: bold;
@@ -294,6 +309,14 @@
 
     for (headwords, translations) in DOM[1:]:
         identity = headwords[0].headword
+        freqtags = []
+        for (freqtag, freqset) in FREQ_SOURCES:
+            if identity in freqset:
+                freqtags.append(freqtag)
+        freqmsg = None
+        if len(freqtags) > 0:
+            freqmsg = ",".join(freqtags)
+            freqmsg = "<div class='freq'>{:s}</div>".format(freqmsg)
         buf = []
         v1, v2, v3 = (None, None, None)
         singular, plural = (None, None)
@@ -323,6 +346,8 @@
             if 'pl' in hw.attrs:
                 plural = (hw.headword, hw.pron)
             buf.append("</div>")
+        if freqmsg:
+            buf.append(freqmsg)
         direct_from = "".join(buf)
         buf = []
         for sense in translations: