home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / common / spelling / pyaspell.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  8.9 KB  |  220 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4.  
  5. try:
  6.     import ctypes
  7.     import ctypes.util as ctypes
  8. except ImportError:
  9.     raise ImportError('ctypes library is needed')
  10.  
  11.  
  12. class AspellError(Exception):
  13.     pass
  14.  
  15.  
  16. class AspellConfigError(AspellError):
  17.     pass
  18.  
  19.  
  20. class AspellSpellerError(AspellError):
  21.     pass
  22.  
  23.  
  24. class Aspell(object):
  25.     VERSION = 5
  26.     if VERSION == 5:
  27.         LIBNAME = 'aspell\\bin\\aspell-15'
  28.     elif VERSION == 6:
  29.         LIBNAME = 'aspell\\bin\\aspell'
  30.     
  31.     
  32.     def __init__(self, configkeys = None, libname = None, **kwds):
  33.         if libname is None:
  34.             libname = ctypes.util.find_library(self.LIBNAME)
  35.         
  36.         self._Aspell__lib = ctypes.CDLL(libname)
  37.         self.libname = libname
  38.         config = self._Aspell__lib.new_aspell_config()
  39.         if config == None:
  40.             raise AspellError("Can't create aspell config object")
  41.         config == None
  42.         if configkeys:
  43.             if hasattr(configkeys, 'items'):
  44.                 configkeys = configkeys.items()
  45.             
  46.             if len(configkeys) == 2 and type(configkeys[0]) is str and type(configkeys[1]) is str:
  47.                 configkeys = [
  48.                     configkeys]
  49.             
  50.             configkeys = dict(configkeys)
  51.         else:
  52.             configkeys = { }
  53.         configkeys.update(kwds)
  54.         configkeys = configkeys.items()
  55.         for key, value in configkeys:
  56.             if not self._Aspell__lib.aspell_config_replace(config, key, value):
  57.                 raise self._aspell_config_error(config)
  58.             self._Aspell__lib.aspell_config_replace(config, key, value)
  59.         
  60.         possible_error = self._Aspell__lib.new_aspell_speller(config)
  61.         self._Aspell__lib.delete_aspell_config(config)
  62.         errno = self._Aspell__lib.aspell_error_number(possible_error)
  63.         if errno != 0:
  64.             errmsg = ctypes.string_at(self._Aspell__lib.aspell_error_message(possible_error))
  65.             self._Aspell__lib.delete_aspell_can_have_error(possible_error)
  66.             raise AspellError(errno, errmsg)
  67.         errno != 0
  68.         self._Aspell__speller = self._Aspell__lib.to_aspell_speller(possible_error)
  69.  
  70.     
  71.     def check(self, word):
  72.         if type(word) is str:
  73.             return bool(self._Aspell__lib.aspell_speller_check(self._Aspell__speller, word, len(word)))
  74.         raise TypeError('String expeced')
  75.  
  76.     
  77.     def suggest(self, word):
  78.         if type(word) is str:
  79.             return self._aspellwordlist(self._Aspell__lib.aspell_speller_suggest(self._Aspell__speller, word, len(word)))
  80.         raise TypeError('String expeced')
  81.  
  82.     
  83.     def personal_dict(self, word = None):
  84.         if word is not None:
  85.             self._Aspell__lib.aspell_speller_add_to_personal(self._Aspell__speller, word, len(word))
  86.             self._aspell_check_error()
  87.         else:
  88.             return self._aspellwordlist(self._Aspell__lib.aspell_speller_personal_word_list(self._Aspell__speller))
  89.         return word is not None
  90.  
  91.     
  92.     def session_dict(self, word = None, clear = False):
  93.         if clear:
  94.             self._Aspell__lib.aspell_speller_clear_session(self._Aspell__speller)
  95.             self._aspell_check_error()
  96.             return None
  97.         if word is not None:
  98.             self._Aspell__lib.aspell_speller_add_to_session(self._Aspell__speller, word, len(word))
  99.             self._aspell_check_error()
  100.         else:
  101.             return self._aspellwordlist(self._Aspell__lib.aspell_speller_session_word_list(self._Aspell__speller))
  102.         return clear
  103.  
  104.     
  105.     def add_replacement_pair(self, misspelled, correct):
  106.         self._Aspell__lib.aspell_speller_store_replacement(self._Aspell__speller, misspelled, len(misspelled), correct, len(correct))
  107.         self._aspell_check_error()
  108.  
  109.     
  110.     def save_all(self):
  111.         self._Aspell__lib.aspell_speller_save_all_word_lists(self._Aspell__speller)
  112.         self._aspell_check_error()
  113.  
  114.     
  115.     def configkeys(self):
  116.         config = self._Aspell__lib.aspell_speller_config(self._Aspell__speller)
  117.         if config is None:
  118.             raise AspellConfigError("Can't get speller's config")
  119.         config is None
  120.         keys_enum = self._Aspell__lib.aspell_config_possible_elements(config, 1)
  121.         if keys_enum is None:
  122.             raise AspellError("Can't get list of config keys")
  123.         keys_enum is None
  124.         
  125.         class KeyInfo(ctypes.Structure):
  126.             _fields_ = [
  127.                 ('name', ctypes.c_char_p),
  128.                 ('type', ctypes.c_int),
  129.                 ('default', ctypes.c_char_p),
  130.                 ('desc', ctypes.c_char_p),
  131.                 ('flags', ctypes.c_int),
  132.                 ('other_data', ctypes.c_int)]
  133.  
  134.         key_next = self._Aspell__lib.aspell_key_info_enumeration_next
  135.         key_next.restype = ctypes.POINTER(KeyInfo)
  136.         list = []
  137.         while True:
  138.             key_info = key_next(keys_enum)
  139.             if not key_info:
  140.                 break
  141.             else:
  142.                 key_info = key_info.contents
  143.             if key_info.type == 0:
  144.                 list.append((key_info.name, key_info.default, key_info.desc))
  145.                 continue
  146.             if key_info.type == 1:
  147.                 list.append((key_info.name, int(key_info.default), key_info.desc))
  148.                 continue
  149.             if key_info.type == 2:
  150.                 if key_info.default.lower() == 'true':
  151.                     list.append((key_info.name, True, key_info.desc))
  152.                 else:
  153.                     list.append((key_info.name, False, key_info.desc))
  154.             key_info.default.lower() == 'true'
  155.             if key_info.type == 3:
  156.                 list.append((key_info.name, key_info.default.split(), key_info.desc))
  157.                 continue
  158.         self._Aspell__lib.delete_aspell_key_info_enumeration(keys_enum)
  159.         return list
  160.  
  161.     
  162.     def reset_cache(self):
  163.         return self._reload_lib()
  164.  
  165.     
  166.     def close(self):
  167.         self._Aspell__lib.delete_aspell_speller(self._Aspell__speller)
  168.  
  169.     
  170.     def _reload_lib(self):
  171.         _LoadLibrary = LoadLibrary
  172.         _FreeLibrary = FreeLibrary
  173.         import _ctypes
  174.         if self._Aspell__lib._handle != 0:
  175.             _FreeLibrary(self._Aspell__lib._handle)
  176.             del self._Aspell__lib
  177.             self._Aspell__lib = ctypes.CDLL(self.libname)
  178.         
  179.  
  180.     
  181.     def _aspellwordlist(self, wordlist_id):
  182.         elements = self._Aspell__lib.aspell_word_list_elements(wordlist_id)
  183.         list = []
  184.         while True:
  185.             wordptr = self._Aspell__lib.aspell_string_enumeration_next(elements)
  186.             if not wordptr:
  187.                 break
  188.                 continue
  189.             word = ctypes.c_char_p(wordptr)
  190.             list.append(word.value)
  191.         self._Aspell__lib.delete_aspell_string_enumeration(elements)
  192.         return list
  193.  
  194.     
  195.     def _aspell_config_error(self, config):
  196.         exc = AspellConfigError(ctypes.c_char_p(self._Aspell__lib.aspell_config_error_message(config)).value)
  197.         self._Aspell__lib.delete_aspell_config(config)
  198.         raise exc
  199.  
  200.     
  201.     def _aspell_check_error(self):
  202.         if self._Aspell__lib.aspell_speller_error(self._Aspell__speller) != 0:
  203.             msg = self._Aspell__lib.aspell_speller_error_message(self._Aspell__speller)
  204.             raise AspellSpellerError(ctypes.string_at(msg))
  205.         self._Aspell__lib.aspell_speller_error(self._Aspell__speller) != 0
  206.  
  207.  
  208. _test_basic = '>>> a = Aspell((\'lang\', \'en\'))\n>>> a.check("when")\nTrue\n>>> set(a.suggest("wehn")) == set([\'when\', \'wen\', \'wean\', \'ween\', \'Wehr\', \'whens\', \'hen\'])\nTrue\n>>> a.add_replacement_pair("wehn", "ween")\n>>> set(a.suggest("wehn")) == set([\'ween\', \'when\', \'wen\', \'wean\', \'Wehr\', \'whens\', \'hen\'])\nTrue\n>>> a.session_dict() == []\nTrue\n>>> a.check("pyaspell")\nFalse\n>>> a.session_dict("pyaspell")\n>>> a.session_dict() == [\'pyaspell\']\nTrue\n>>> a.check("pyaspell")\nTrue\n>>> a.session_dict(clear=True)\n>>> a.session_dict()\n[]\n>>> a.close()'
  209. _test_locales = ">>> # Locales\n>>> a1 = Aspell(('lang', 'en-us'))\n>>> a2 = Aspell(('lang', 'en-gb'))\n>>> (a1.check('localize'), a2.check('localize')) == (True, False)\nTrue\n>>> (a1.check('localize'), a2.check('localize')) == (True, False)\nTrue\n>>> (a1.check('localise'), a2.check('localise')) == (False, True)\nTrue\n>>> (a1.suggest('localise')[0], a2.suggest('localize')[0]) == ('localize', 'localise')\nTrue\n>>> a1.close()\n>>> a2.close()"
  210. _test_case = ">>> # Case sensitivity\n>>> a1 = Aspell(('ignore-case',''))\n>>> a2 = Aspell(('dont-ignore-case', ''))\n>>> (a1.check('steve'), a2.check('steve')) == (True, False)\nTrue\n>>> (a1.check('Steve'), a2.check('Steve')) == (True, True)\nTrue\n>>> a1.close()\n>>> a2.close()"
  211. __test__ = dict(basic = _test_basic, locales = _test_locales, case = _test_case)
  212.  
  213. def _test():
  214.     import doctest
  215.     doctest.testmod(verbose = True)
  216.  
  217. if __name__ == '__main__':
  218.     Aspell(('lang', 'fake'))
  219.  
  220.