Added jblite-compatible stubs and removed jbparse code. Program starts up again.
[jben2_gui.git] / jben / dict_chooser.py
blob1d10da6df78378c5f4e29927b6925ea700a17f14
1 # -*- coding: utf-8 -*-
3 from __future__ import absolute_import
5 import sys, os
6 from jben.preferences import Preferences, DictEntry
7 from jben.console import y_or_n
8 from jben.dict_downloader import download_dict
11 def set_dicts(option_key, d_entries):
13 """Updates preferences with selected dictionary objects.
15 Given a preference key (dictfile.kanji for example) and a list of
16 dictionary entry objects, create appropriate keys.
18 For example, with dictfile.kanji as the key and a list of
19 [kanjidic, kanjd212] entry objects, the following keys may be
20 created:
22 dictfile.kanji.1.file: kanjidic.gz
23 dictfile.kanji.1.encoding: euc-jp
24 dictfile.kanji.2.file: kanjd212.gz
25 dictfile.kanji.2.encoding: euc-jp
27 On update, any existing entries which start with the specified
28 option key will be dropped.
30 """
32 # Clear previous entries
33 prefs = refs.prefs
34 keys = [k for k in prefs if k.find(option_key) == 0]
35 keys.sort()
36 for k in keys:
37 #print "%s: %s" % (k, str(prefs[k]))
38 del prefs[k]
39 # Store new entries
40 for i, de in enumerate(d_entries):
41 i += 1
42 basekey = "%s.%d" % (option_key, i)
43 filekey = "%s.filename" % basekey
44 encodekey = "%s.encoding" % basekey
45 prefs[filekey] = de.filename
46 prefs[encodekey] = de.encoding
48 def set_word_dicts(dicts):
49 set_dicts("dictfile.word", dicts)
51 def set_kanji_dicts(dicts):
52 set_dicts("dictfile.kanji", dicts)
56 def console_chooser():
57 """Select dictionaries from the console."""
59 word_dicts = (("EDICT: 172143 entries. Small, fast, commonly used by "
60 "other programs.", "edict.gz"),
61 #("EDICT2: 144541 entries. Newer, more compact version of "
62 # "EDICT.", "edict2.gz"),
63 ("JMdict: 144727 entries. Multiple language glosses. "
64 "Big, slow, detailed.", "jmdict.gz"),
65 ("JMdict_e: English-only version of JMdict.", "jmdict_e.gz"))
66 kanji_dicts = (("KANJIDIC: Standard kanji dictionary. 6355 entries. "
67 "Small, very fast", "kanjidic.gz"),
68 ("KANJD212: Extension to KANJIDIC. 5801 entries. "
69 "Not commonly needed.", "kanjd212.gz"),
70 ("KANJIDIC2: Newer XML-based dictionary. 13108 entries. "
71 "A little slow.", "kanjidic2.xml.gz"))
73 def run_chooser(dicts, default):
74 # Print dictionary descriptions
75 for i, (desc, fname) in enumerate(dicts):
76 i += 1
77 print "%d. %s" % (i, desc)
78 print
80 print ("Please choose which dictionaries to use. More than one "
81 "can be specified if you put spaces between them.")
82 print
83 while True:
84 # Get selection
85 default = [1]
86 sys.stdout.write("Your choice [Default: %s]: "
87 % " ".join(map(str, default)))
88 try:
89 choices = map(int, sys.stdin.readline().strip().split())
90 except:
91 continue
92 if len(choices) == 0:
93 choices = default
94 if all([i in range(1, 1+len(dicts)) for i in choices]):
95 break
97 return [DictEntry(dicts[i-1][1]) for i in choices]
99 # Choose word dicts
100 print "J-Ben supports the following word dictionary files:"
101 print
102 dicts = run_chooser(word_dicts, [1])
103 set_word_dicts(dicts)
105 # Choose kanji dicts
106 print "J-Ben supports the following kanji dictionary files:"
107 print
108 dicts = run_chooser(kanji_dicts, [1])
109 set_kanji_dicts(dicts)
111 # ...confirm changes?
112 print "You've chosen the following settings:"
113 print
114 prefs = refs.prefs
115 word_keys = [k for k in prefs if k.find("dict.word") == 0]
116 kanji_keys = [k for k in prefs if k.find("dict.kanji") == 0]
117 keys = word_keys + kanji_keys
118 keys.sort()
119 for k in keys:
120 print "\t%s: %s" % (k, prefs[k])
121 print
122 choice = y_or_n("Is this okay?", "n")
124 if choice != 'y':
125 return
126 prefs.save()
128 # Check existence of dictionary files. If some are not present,
129 # offer to download.
130 dpath = prefs.get_dict_path()
131 fn_keys = [k for k in keys if "filename" in k]
132 files = [prefs[k] for k in fn_keys]
133 fnames = [os.path.join(dpath, fname) for fname in files]
134 needed_files = [os.path.split(fname)[-1] for fname in fnames
135 if not os.path.exists(fname)]
136 if needed_files:
137 needed_files.sort()
138 print
139 print "The following dictionary files could not be found:"
140 print
141 for i, f in enumerate(needed_files):
142 i += 1
143 print "%d. %s" % (i, f)
144 print
145 choice = y_or_n("Do you wish to download them?", "y")
147 if choice == 'y':
148 for f in needed_files:
149 download_dict(f)
151 if __name__ == "__main__":
152 console_chooser()