plugins/aspell_plugin.py
author Mikael Berthe <mikael@lilotux.net>
Tue, 01 May 2007 23:58:21 +0200
changeset 16 7a3843e91aa2
parent 14 5b5eaf194467
child 23 f34555473aaf
permissions -rw-r--r--
Fix encoding issues

#$ neutron_plugin 01

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 = ""

    if len(splitdata) > 1:
        lang = parameters.lower()
        if re.match(r"\w\w ", lang):
            parameters = parameters[3:].lstrip()
            aspell += " --lang=" + lang

    stdout, stdin = popen2.popen2(aspell)

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

    r = stdout.readline()
    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:
                reply += r

    if not reply:
        reply = "Unknown error"

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

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