plugins/aspell_plugin.py
author Mikael Berthe <mikael@lilotux.net>
Wed, 02 May 2007 20:10:16 +0200
changeset 24 7d7edae239c9
parent 23 f34555473aaf
child 26 8a1893b6b113
permissions -rw-r--r--
Update comments

#$ neutron_plugin 01
# 2007-05 Mikael Berthe <mikael@lilotux.net>

import popen2
import re

ASPELL = "/usr/bin/aspell -a"

def handler_aspell(type, source, parameters):
    parameters = parameters.lstrip().rstrip()
    splitdata = parameters.split()
    aspell = ASPELL
    reply = ""

    # Check if the language is specified (two-letter code)
    if len(splitdata) > 1:
        lang = parameters.lower()
        if re.match(r"\w\w ", lang):
            parameters = parameters[3:].lstrip()
            aspell += " --lang=" + lang

    # Spawn an aspell process
    stdout, stdin = popen2.popen2(aspell)

    stdin.write(parameters.encode('iso-8859-15'))
    stdin.close()

    # Skip the first line (banner)
    r = stdout.readline()

    # Process all result lines
    for r in stdout.readlines():
        r = r.decode('iso-8859-15')
        if r == "*\n":
            reply += "* Word correctly spelled.\n"
        elif r.startswith("& "):
            if r.count(": "):
                reply += "* Let me suggest " + r[r.find(": ")+2:].strip()
                reply += ".\n"
            else:
                # I don't know if it can happen
                reply += r

    if not reply:
        reply = "Unknown error (no reply)"

    smsg(type, source, reply.rstrip())

register_command_handler(handler_aspell, 'aspell', 0, 'Check the spelling',
                         'aspell [lang]', ['aspell fr bonjour'])

# vim:set et sts=4: