#!/usr/bin/env python

"""$URL: svn://svn/repos/trunk/sitereaper/lib/spell.py $
$Id: spell.py 20952 2003-03-05 16:51:11Z dbinger $

A class that wraps a call to ispell and collects
the results in a dictionary that records the frequency
for each suspicious word.

"""


__revision__ = "$Id: spell.py 20952 2003-03-05 16:51:11Z dbinger $"

import sys, os
from sitereaper.utils import _exec, warn


class Speller:
    """
    A Speller wraps a call to ispell.

    Instance attributes:
      results : { string : int }
      ispell_stdout : file
      ispell_pid : int
      cmd : [string]
    """

    def __init__ (self, personal_dict=None):
        self.results = {}
        self.ispell_stdout = None
        self.ispell_pid = None
        self.cmd = ['ispell', '-l', '-W', '2']
        if personal_dict:
            self.cmd.extend(['-p', personal_dict])

    def start (self, filename):
        file = os.open(filename, os.O_RDONLY)
        (stdout_r, stdout_w) = os.pipe()
        pid = os.fork()
        if pid == 0:                    # in the child
            os.dup2(file, 0)            # connect stdin to file to spellcheck
            os.dup2(stdout_w, 1)        # connect stdout to parent
            os.close(stdout_r)
            _exec(self.cmd)
        else:                           # in the parent
            os.close(file)
            os.close(stdout_w)
            self.ispell_pid = pid
            self.ispell_stdout = os.fdopen(stdout_r, "r")

    def print_results (self,f=sys.stdout):
        results = self.get_results()
        words = results.keys()
        words.sort()
        for w in words:
            f.write("%s: %d\n" % (w, results[w]))

    def add_word (self, word):
        if not word:
            return
        if self.results.has_key(word):
            self.results[word] += 1
        else:
            self.results[word] = 1

    def clear_results (self):
        self.results = {}

    def get_results (self):
        output = self.ispell_stdout.read()
        self.ispell_stdout.close()
        (pid, status) = os.waitpid(self.ispell_pid, 0)
        if status != 0:
            warn("ispell exited with status %d" % status)

        for word in output.split('\n'):
            self.add_word(word)
        return self.results

if __name__=='__main__':
    import sys, os
    from urllib import urlretrieve
    from cStringIO import StringIO
    from sitereaper.page_parser import PageParser

    parser = PageParser()
    textfile = StringIO()

    for name in sys.argv[1:]:
        (filename, headers) = urlretrieve(name)
        file = open(filename)
        #txt_filename = filename + ".txt"
        txt_filename = "tmp.txt"
        txt_file = open(txt_filename, "w")

        parser.reset()
        parser.set_text_file(txt_file)
        parser.feed(file.read())
        parser.close()
        file.close()
        txt_file.close()

        speller = Speller("dictionaries/default")
        speller.start(txt_filename)
        print "url: %s" % name
        print "tmp file: %s" % filename
        print "txt file: %s" % txt_filename
        speller.print_results()
        #os.unlink(txt_filename)
