/usr/share/pyshared/d_rats/spell.py is in d-rats 0.3.3-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | import os
import subprocess
class Spelling:
def __open_aspell(self):
kwargs = {}
if subprocess.mswindows:
su = subprocess.STARTUPINFO()
su.dwFlags |= subprocess.STARTF_USESHOWWINDOW
su.wShowWindow = subprocess.SW_HIDE
kwargs["startupinfo"] = su
p = subprocess.Popen([self.__aspell, "pipe"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
#close_fds=True,
**kwargs)
return p
def __close_aspell(self):
if self.__pipe:
self.__pipe.terminate()
self.__pipe = None
def __init__(self, aspell="aspell", persist=True):
self.__aspell = aspell
self.__persist = persist
self.__pipe = None
def lookup_word(self, wiq):
for c in wiq:
c = ord(c)
if c < ord('A') or c > ord('z') or \
(c > ord('Z') and c < ord('a')):
return []
try:
self.__pipe.stdout.readline()
except Exception, e:
print "Demand-opening aspell..."
self.__pipe = self.__open_aspell()
self.__pipe.stdout.readline()
self.__pipe.stdin.write("%s%s" % (wiq, os.linesep))
suggest_str = self.__pipe.stdout.readline()
if not self.__persist:
self.__close_aspell()
if suggest_str.startswith("*"):
return []
elif not suggest_str.startswith("&"):
raise Exception("Unknown response from aspell: %s" % suggest_str)
suggestions = suggest_str.split()
return suggestions[4:]
def test(self):
try:
s = self.lookup_word("speling")
if s[0] != "spelling,":
print "Unable to validate first suggestion of `spelling'"
print s[0]
return False
except Exception, e:
print "Spelling test failed: %s" % e
return False
print "Tested spelling okay: %s" % s
return True
def test_word(spell, word):
spell.stdin.write(word + "\n")
result = spell.stdout.readline()
spell.stdout.readline()
if result.startswith("*"):
return []
elif result.startswith("&"):
items = result.split()
return items[4:]
else:
print "Unknown response: `%s'" % result
SPELL = None
def get_spell():
global SPELL
if not SPELL:
SPELL = Spelling()
return SPELL
def __do_fly_spell(buffer):
cursor_mark = buffer.get_mark("insert")
start_iter = buffer.get_iter_at_mark(cursor_mark)
end_iter = buffer.get_iter_at_mark(cursor_mark)
if not start_iter.starts_word():
start_iter.backward_word_start()
if end_iter.inside_word():
end_iter.forward_word_end()
text = buffer.get_text(start_iter, end_iter)
word = text.strip()
#print "Got: '%s' (%s)" % (text, word)
if not word:
return
end_iter.backward_chars(len(text) - len(word))
if " " in word:
mispelled = False
else:
speller = get_spell()
mispelled = bool(speller.lookup_word(word))
if text.endswith(" ") and mispelled:
buffer.apply_tag_by_name("misspelled", start_iter, end_iter)
else:
buffer.remove_tag_by_name("misspelled", start_iter, end_iter)
def prepare_TextBuffer(buf):
import gtk
import pango
tags = buf.get_tag_table()
tag = gtk.TextTag("misspelled")
tag.set_property("underline", pango.UNDERLINE_SINGLE)
tag.set_property("underline-set", True)
tag.set_property("foreground", "red")
tags.add(tag)
buf.connect("changed", __do_fly_spell)
if __name__ == "__main__":
s = Spelling()
print s.lookup_word("speling")
print s.lookup_word("teh")
print s.lookup_word("foo")
|