author | Mikael Berthe <mikael@lilotux.net> |
Wed, 02 May 2007 22:38:53 +0200 | |
changeset 28 | f7d12d2d39bc |
parent 27 | c66d39c6c27b |
child 30 | 33af762962f1 |
permissions | -rw-r--r-- |
14 | 1 |
#$ neutron_plugin 01 |
23
f34555473aaf
Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
2 |
# 2007-05 Mikael Berthe <mikael@lilotux.net> |
14 | 3 |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
4 |
import subprocess |
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
5 |
import locale |
14 | 6 |
import re |
7 |
||
8 |
def handler_aspell(type, source, parameters): |
|
9 |
parameters = parameters.lstrip().rstrip() |
|
10 |
splitdata = parameters.split() |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
11 |
cmd = ['/usr/bin/aspell', '-a'] |
14 | 12 |
reply = "" |
13 |
||
27
c66d39c6c27b
aspell: Be silent if no word is given
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
14 |
if not parameters: |
c66d39c6c27b
aspell: Be silent if no word is given
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
15 |
return |
c66d39c6c27b
aspell: Be silent if no word is given
Mikael Berthe <mikael@lilotux.net>
parents:
26
diff
changeset
|
16 |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
17 |
LC, encoding = locale.getdefaultlocale() |
28
f7d12d2d39bc
aspell: Default encoding value (ascii)
Mikael Berthe <mikael@lilotux.net>
parents:
27
diff
changeset
|
18 |
if not encoding: |
f7d12d2d39bc
aspell: Default encoding value (ascii)
Mikael Berthe <mikael@lilotux.net>
parents:
27
diff
changeset
|
19 |
encoding = 'ascii' |
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
20 |
|
24 | 21 |
# Check if the language is specified (two-letter code) |
14 | 22 |
if len(splitdata) > 1: |
23 |
lang = parameters.lower() |
|
24 |
if re.match(r"\w\w ", lang): |
|
25 |
parameters = parameters[3:].lstrip() |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
26 |
cmd.append("--lang=" + lang) |
14 | 27 |
|
24 | 28 |
# Spawn an aspell process |
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
29 |
aspell = subprocess.Popen(cmd, stdin=subprocess.PIPE, |
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
30 |
stdout=subprocess.PIPE) |
14 | 31 |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
32 |
aspell.stdin.write(parameters.encode(encoding)) |
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
33 |
aspell.stdin.close() |
14 | 34 |
|
24 | 35 |
# Skip the first line (banner) |
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
36 |
aspell.stdout.readline() |
24 | 37 |
|
38 |
# Process all result lines |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
39 |
for r in aspell.stdout.readlines(): |
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
40 |
r = r.decode(encoding) |
14 | 41 |
if r == "*\n": |
42 |
reply += "* Word correctly spelled.\n" |
|
43 |
elif r.startswith("& "): |
|
44 |
if r.count(": "): |
|
45 |
reply += "* Let me suggest " + r[r.find(": ")+2:].strip() |
|
46 |
reply += ".\n" |
|
47 |
else: |
|
24 | 48 |
# I don't know if it can happen |
14 | 49 |
reply += r |
50 |
||
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
51 |
aspell.stdout.close() |
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
52 |
|
14 | 53 |
if not reply: |
24 | 54 |
reply = "Unknown error (no reply)" |
14 | 55 |
|
56 |
smsg(type, source, reply.rstrip()) |
|
57 |
||
58 |
register_command_handler(handler_aspell, 'aspell', 0, 'Check the spelling', |
|
23
f34555473aaf
Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
59 |
'aspell [lang]', ['aspell fr bonjour']) |
f34555473aaf
Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
60 |
|
f34555473aaf
Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
61 |
# vim:set et sts=4: |