author | Mikael Berthe <mikael@lilotux.net> |
Wed, 02 May 2007 20:39:20 +0200 | |
changeset 26 | 8a1893b6b113 |
parent 24 | 7d7edae239c9 |
child 27 | c66d39c6c27b |
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 |
||
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
14 |
LC, encoding = locale.getdefaultlocale() |
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
15 |
|
24 | 16 |
# Check if the language is specified (two-letter code) |
14 | 17 |
if len(splitdata) > 1: |
18 |
lang = parameters.lower() |
|
19 |
if re.match(r"\w\w ", lang): |
|
20 |
parameters = parameters[3:].lstrip() |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
21 |
cmd.append("--lang=" + lang) |
14 | 22 |
|
24 | 23 |
# Spawn an aspell process |
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
24 |
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
|
25 |
stdout=subprocess.PIPE) |
14 | 26 |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
27 |
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
|
28 |
aspell.stdin.close() |
14 | 29 |
|
24 | 30 |
# 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
|
31 |
aspell.stdout.readline() |
24 | 32 |
|
33 |
# Process all result lines |
|
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
34 |
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
|
35 |
r = r.decode(encoding) |
14 | 36 |
if r == "*\n": |
37 |
reply += "* Word correctly spelled.\n" |
|
38 |
elif r.startswith("& "): |
|
39 |
if r.count(": "): |
|
40 |
reply += "* Let me suggest " + r[r.find(": ")+2:].strip() |
|
41 |
reply += ".\n" |
|
42 |
else: |
|
24 | 43 |
# I don't know if it can happen |
14 | 44 |
reply += r |
45 |
||
26
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
46 |
aspell.stdout.close() |
8a1893b6b113
Switch to using module "subprocess" (Suggested by Loic Berthe)
Mikael Berthe <mikael@lilotux.net>
parents:
24
diff
changeset
|
47 |
|
14 | 48 |
if not reply: |
24 | 49 |
reply = "Unknown error (no reply)" |
14 | 50 |
|
51 |
smsg(type, source, reply.rstrip()) |
|
52 |
||
53 |
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
|
54 |
'aspell [lang]', ['aspell fr bonjour']) |
f34555473aaf
Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
55 |
|
f34555473aaf
Update coding style for my modules
Mikael Berthe <mikael@lilotux.net>
parents:
16
diff
changeset
|
56 |
# vim:set et sts=4: |