plugins/aspell_plugin.py
author Mikael Berthe <mikael@lilotux.net>
Tue, 01 May 2007 23:16:29 +0200
changeset 14 5b5eaf194467
child 16 7a3843e91aa2
permissions -rw-r--r--
New Aspell plugin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
#$ neutron_plugin 01
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import popen2
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
import re
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
ASPELL = "/usr/bin/aspell -a"
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
def handler_aspell(type, source, parameters):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
    parameters = parameters.lstrip().rstrip()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
    splitdata = parameters.split()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
    aspell = ASPELL
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
    reply = ""
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
    if len(splitdata) > 1:
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
        lang = parameters.lower()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
        if re.match(r"\w\w ", lang):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
            parameters = parameters[3:].lstrip()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
            aspell += " --lang=" + lang
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
    stdout, stdin = popen2.popen2(aspell)
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
    stdin.write(parameters)
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
    stdin.close()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
    r = stdout.readline()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
    for r in stdout.readlines():
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
        if r == "*\n":
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
            reply += "* Word correctly spelled.\n"
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
        elif r.startswith("& "):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
            if r.count(": "):
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
                reply += "* Let me suggest " + r[r.find(": ")+2:].strip()
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
                reply += ".\n"
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
            else:
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
                reply += r
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
    if not reply:
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
        reply = "Unknown error"
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
    smsg(type, source, reply.rstrip())
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
register_command_handler(handler_aspell, 'aspell', 0, 'Check the spelling',
5b5eaf194467 New Aspell plugin
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
			 'aspell [lang]', ['aspell fr bonjour'])