py/gadict_srs_anki.py
changeset 674 fe57401d8109
parent 673 ca1d4a6e201b
child 675 f3465e45b958
equal deleted inserted replaced
673:ca1d4a6e201b 674:fe57401d8109
    14 import gadict_freq
    14 import gadict_freq
    15 
    15 
    16 
    16 
    17 FINAME = None
    17 FINAME = None
    18 FONAME = None
    18 FONAME = None
       
    19 FDELNAME = None
       
    20 NAME = None
    19 LANGS = None
    21 LANGS = None
    20 FREQ_SOURCES = []
    22 FREQ_SOURCES = []
    21 RICH_MODE = False
    23 RICH_MODE = False
    22 
    24 
       
    25 ARG_NAME_RE = regex.compile("-name=(.+)")
    23 # -lang:ru,uk
    26 # -lang:ru,uk
    24 ARG_LANG_RE = regex.compile("-lang:(.+)")
    27 ARG_LANG_RE = regex.compile("-lang:(.+)")
    25 # -freq:var:TAG=FILE or -freq:freq:TAG=FILE
    28 # -freq:var:TAG=FILE or -freq:freq:TAG=FILE
    26 ARG_FREQ_RE = regex.compile("-freq:(freq|var):([^=]+)=(.+)")
    29 ARG_FREQ_RE = regex.compile("-freq:(freq|var):([^=]+)=(.+)")
       
    30 ARG_DELFILE_RE = regex.compile("-delfile=(.+)")
    27 
    31 
    28 look_for_files = False
    32 look_for_files = False
    29 for idx in range(1, len(sys.argv)):
    33 for idx in range(1, len(sys.argv)):
    30     arg = sys.argv[idx]
    34     arg = sys.argv[idx]
    31     if arg == "--":
    35     if arg == "--":
    32         look_for_files = True
    36         look_for_files = True
    33         continue
    37         continue
    34     if not look_for_files:
    38     if not look_for_files:
       
    39         m = ARG_NAME_RE.match(arg)
       
    40         if m:
       
    41             NAME = m.group(1)
       
    42             continue
    35         m = ARG_LANG_RE.match(arg)
    43         m = ARG_LANG_RE.match(arg)
    36         if m:
    44         if m:
    37             LANGS = set(m.group(1).split(","))
    45             LANGS = set(m.group(1).split(","))
    38             for lang in LANGS:
    46             for lang in LANGS:
    39                 if len(lang) != 2:
    47                 if len(lang) != 2:
    52                 else:
    60                 else:
    53                     raise Exception("Unsupported mode: '{:s}'".format(mode))
    61                     raise Exception("Unsupported mode: '{:s}'".format(mode))
    54                 wlist = parser.parse()
    62                 wlist = parser.parse()
    55             FREQ_SOURCES.append((tag, set(wlist)))
    63             FREQ_SOURCES.append((tag, set(wlist)))
    56             continue
    64             continue
       
    65         m = ARG_DELFILE_RE.match(arg)
       
    66         if m:
       
    67             FDELNAME = m.group(1)
       
    68             continue
    57         if arg == "-rich":
    69         if arg == "-rich":
    58             RICH_MODE = True
    70             RICH_MODE = True
    59             continue
    71             continue
    60         if arg.startswith("-"):
    72         if arg.startswith("-"):
    61             raise Exception("Unsupported option format: '{:s}'".format(arg))
    73             raise Exception("Unsupported option format: '{:s}'".format(arg))
    65     if not FONAME:
    77     if not FONAME:
    66         FONAME = arg
    78         FONAME = arg
    67         continue
    79         continue
    68     raise Exception("Unnecessary argument: '{:s}'".format(arg))
    80     raise Exception("Unnecessary argument: '{:s}'".format(arg))
    69 
    81 
    70 
    82 if not FINAME:
       
    83     raise Exception("Input file name is not passed...")
    71 FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")
    84 FIN = io.open(FINAME, mode='r', buffering=1, encoding="utf-8")
    72 
    85 
    73 PARSER = gadict.Parser()
    86 PARSER = gadict.Parser()
    74 try:
    87 try:
    75     DOM = PARSER.parse(FIN)
    88     DOM = PARSER.parse(FIN)
    90 
   103 
    91 # signal.signal(signal.SIGINT, lambda signal, frame: cleanup())
   104 # signal.signal(signal.SIGINT, lambda signal, frame: cleanup())
    92 
   105 
    93 
   106 
    94 if FONAME is None:
   107 if FONAME is None:
    95     raise Exception('Missing output file name')
   108     raise Exception("Output file name is not passed...")
    96 # Looks like anki libs change working directory to media directory of current deck
   109 # Looks like anki libs change working directory to media directory of current deck
    97 # Therefore absolute path should be stored before creating temporary deck
   110 # Therefore absolute path should be stored before creating temporary deck
    98 FONAME = os.path.abspath(FONAME)
   111 FONAME = os.path.abspath(FONAME)
    99 FBASENAME, _ = os.path.splitext(os.path.basename(FONAME))
       
   100 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))
   112 TMPDIR = tempfile.mkdtemp(dir = os.path.dirname(FONAME))
   101 
   113 
   102 try:
   114 if not NAME:
   103     FDEL = io.open(FBASENAME+".del", mode='r', buffering=1, encoding="utf-8")
   115     NAME, _ = os.path.splitext(os.path.basename(FINAME))
   104 except:
   116 
       
   117 if FDELNAME:
       
   118     FDEL = io.open(FDELNAME, mode='r', buffering=1, encoding="utf-8")
       
   119 else:
   105     FDEL = None
   120     FDEL = None
   106 
       
   107 
   121 
   108 import hashlib
   122 import hashlib
   109 
   123 
   110 import anki
   124 import anki
   111 from anki.exporting import AnkiPackageExporter
   125 from anki.exporting import AnkiPackageExporter
   297         buf.append(tr)
   311         buf.append(tr)
   298         buf.append("</span>")
   312         buf.append("</span>")
   299         buf.append("</div>")
   313         buf.append("</div>")
   300 
   314 
   301 try:
   315 try:
   302     builder = AnkiDbBuilder(TMPDIR, FBASENAME)
   316     builder = AnkiDbBuilder(TMPDIR, NAME)
   303 
   317 
   304     for identity in FDEL or []:
   318     for identity in FDEL or []:
   305         identity = identity.strip()
   319         identity = identity.strip()
   306         warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity)
   320         warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity)
   307         builder.add_note("en->tr", identity, warnmsg, warnmsg+" en->tr", "del")
   321         builder.add_note("en->tr", identity, warnmsg, warnmsg+" en->tr", "del")