1 # -*- coding: utf-8 -*-
3 from __future__
import absolute_import
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
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.
32 # Clear previous entries
34 keys
= [k
for k
in prefs
if k
.find(option_key
) == 0]
37 #print "%s: %s" % (k, str(prefs[k]))
40 for i
, de
in enumerate(d_entries
):
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
):
77 print "%d. %s" % (i
, desc
)
80 print ("Please choose which dictionaries to use. More than one "
81 "can be specified if you put spaces between them.")
86 sys
.stdout
.write("Your choice [Default: %s]: "
87 % " ".join(map(str, default
)))
89 choices
= map(int, sys
.stdin
.readline().strip().split())
94 if all([i
in range(1, 1+len(dicts
)) for i
in choices
]):
97 return [DictEntry(dicts
[i
-1][1]) for i
in choices
]
100 print "J-Ben supports the following word dictionary files:"
102 dicts
= run_chooser(word_dicts
, [1])
103 set_word_dicts(dicts
)
106 print "J-Ben supports the following kanji dictionary files:"
108 dicts
= run_chooser(kanji_dicts
, [1])
109 set_kanji_dicts(dicts
)
111 # ...confirm changes?
112 print "You've chosen the following settings:"
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
120 print "\t%s: %s" % (k
, prefs
[k
])
122 choice
= y_or_n("Is this okay?", "n")
128 # Check existence of dictionary files. If some are not present,
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
)]
139 print "The following dictionary files could not be found:"
141 for i
, f
in enumerate(needed_files
):
143 print "%d. %s" % (i
, f
)
145 choice
= y_or_n("Do you wish to download them?", "y")
148 for f
in needed_files
:
151 if __name__
== "__main__":