-
Notifications
You must be signed in to change notification settings - Fork 2
/
dictc.py
executable file
·283 lines (234 loc) · 8.59 KB
/
dictc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @TODO: replace raw_input by cmd module
# @TODO: 在键盘中断时关闭线程
# @TODO: stardict is too slow
import subprocess
import tempfile
import os
import sys
import readline
import threading
from DictC import (
BaseDict,
DictCnDict,
BingDict,
StarDict,
SpellCheck,
External,
)
import unicodedata
import argparse
def width(string):
return sum(1 + (unicodedata.east_asian_width(c) in "WF") for c in string)
class SoundThread(threading.Thread):
def __init__(self, s):
threading.Thread.__init__(self)
self.s = s
self._uri = ''
def set_uri(self, uri):
self._uri = uri
self.s.do(self.uri)
def get_uri(self):
return self._uri
def del_uri(self):
del self._uri
uri = property(get_uri, set_uri, del_uri, "Change src uri!")
def run(self):
pass
class FetchThread(threading.Thread):
def __init__(self, keyword, args):
threading.Thread.__init__(self)
self.keyword = keyword
self.args = args
def run(self):
global dict_instance
if dict_instance is None:
class_object = self.args['dict']
dict_instance = class_object()
dict_instance.setKeyword(self.keyword)
status, content = dict_instance.getOutput()
link = dict_instance.getLink(self.keyword)
if status:
content += "\n\n%s" % link
output(content)
else:
print u'无解释'
print link
class Completer(threading.Thread):
def __init__(self, args):
threading.Thread.__init__(self)
self.prefix = None
self.caches = {}
self.format_string = "%s %s"
self.args = args
def complete(self, prefix, index):
if prefix in self.caches:
try:
w, t = self.caches[prefix][index]
if not t.strip() or w.strip() == t.strip():
return w
else:
return self.format_string % (w, t)
except IndexError:
return None
except KeyError:
return None
if not prefix in self.caches or prefix != self.prefix:
words = self.args['sugg'].fetchSuggestion(prefix)
if not len(words):
return None
self.words = words
# we have a new prefix!
self.matching_words = [
(w.encode('utf8'), t.encode('utf8')) for w, t in self.words
]
widest = max([width(w.decode('utf8')) for w, t in
self.matching_words])
self.matching_words = [
("%s%s" % (w, ' ' * (widest - width(w.decode('utf8')))), t) for
w, t in self.matching_words]
self.prefix = prefix
self.caches[prefix] = self.matching_words
try:
w, t = self.matching_words[index]
if not t.strip() or t.strip() == w.strip():
return w
else:
return self.format_string % (w, t)
except IndexError:
return None
def run(self, prefix, index):
self.complete(prefix, index)
def paging(content, pager):
f = tempfile.NamedTemporaryFile()
f.write(content)
f.flush()
proc = subprocess.Popen("%s %s" % (pager, f.name), shell=True)
proc.communicate()
f.close()
def output(content):
try:
pager = os.environ.get('PAGER', 'more')
paging(content, pager)
except Exception:
print content
def thread(keyword, args):
f = FetchThread(keyword, args)
f.setDaemon(True)
f.start()
f.join()
def get_parser():
description = u'一个简单的在线查询单词小工具!'
epilog = u"""
当前版本:0.1.1
主要功能:
- 支持多个在线词典服务及星际译王词典
bing dict.bing.com.cn
stardict 星际译王
- 支持交互式模式下按<Tab>自动补全
bing,dictcn(dict.cn),spellcheck(拼写检查),external(外部命令)
- 发音支持
需要 gstreamer 的 python 绑定,可以使用 yum/apt-get 安装。
使用示例:
$ %s hello
直接查询 hello
$ %s
进入交互式模式,按 <CTRL-d> 退出!
$ %s -d bing -c dictcn
使用 dict.bing.com.cn 的在线翻译,dict.cn 来做查询时的自动补全!
代码链接:
https://github.com/grassofhust/dictc
License:
Beerware (If we meet some day, and you think
this stuff is worth it, you can buy me a beer.)
""" % (sys.argv[0], sys.argv[0], sys.argv[0])
formatter_class = argparse.RawDescriptionHelpFormatter
parser = argparse.ArgumentParser(description=description, epilog=epilog,
formatter_class=formatter_class)
parser.add_argument('-d', nargs='?',
help=u'词典(默认:bing):%s ' %
','.join(map(lambda d: d.metadata['id'],
CLIAction.services)),
metavar=u'dictionary',
dest='dict',
action=CLIAction,
default=CLIAction.services[0],
choices=map(lambda d: d.metadata['id'],
CLIAction.services),
)
parser.add_argument('-c', nargs='?',
help=u'自动补全(默认:bing):%s ' %
','.join(map(lambda d: d.metadata['id'],
CompletionAction.services)),
metavar=u'completion',
dest='sugg',
action=CompletionAction,
default=CompletionAction.services[0],
choices=map(lambda s: s.metadata['id'],
CompletionAction.services),
)
parser.add_argument('--nosound', help=u'禁用发音(默认启用)', dest='nosound',
action='store_true', default=False)
parser.add_argument('-v', '--version', action='version',
version='%(prog)s 0.1.1')
parser.add_argument('words', metavar='keyword or sentence', type=str,
nargs='*')
return parser
def command_line_runner():
parser = get_parser()
args = vars(parser.parse_args())
histfile = "%s/dictc_history_py" % tempfile.gettempdir()
if (os.path.exists(histfile)):
readline.read_history_file(histfile)
hasSound = not args['nosound']
if hasSound:
try:
from DictC.Sound import Sound # @hack
s = Sound()
t = SoundThread(s)
t.setDaemon(True)
t.start()
except ImportError:
hasSound = False
try:
if not args['words']:
print 'Press <Ctrl-D> or <Ctrl-C> to exit!'
completer = Completer(args)
readline.parse_and_bind("set show-all-if-ambiguous on")
readline.parse_and_bind("set completion-ignore-case on")
readline.parse_and_bind("set completion-map-case on")
readline.parse_and_bind("set skip-completed-text on")
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
readline.set_completer_delims('')
while True:
line = raw_input('>> ')
keyword = line.strip()
if len(keyword):
if hasSound:
t.uri = BaseDict.soundUri(keyword)
thread(keyword, args)
else:
keyword = ' '.join(args['words'])
readline.add_history(keyword)
if hasSound:
t.uri = BaseDict.soundUri(keyword)
thread(keyword, args)
except (EOFError, KeyboardInterrupt, SystemExit):
pass
readline.write_history_file(histfile)
if __name__ == "__main__":
class CLIAction(argparse.Action):
services = (BingDict, StarDict)
def __init__(self, *args, **kwargs):
super(CLIAction, self).__init__(*args, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
for service in self.services:
if service.metadata['id'] == values:
return setattr(namespace, self.dest, service)
class CompletionAction(CLIAction):
services = (BingDict, DictCnDict, SpellCheck, External)
dict_instance = None
command_line_runner()
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 textwidth=79