home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / common / spelling / dicts.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  9.7 KB  |  300 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement, division
  5. from util import soupify, httpjoin, autoassign, httpok
  6. from util.cacheable import urlcacheopen
  7. from path import path
  8. import subprocess
  9. import tarfile
  10. import shlex
  11. import logging
  12. log = logging.getLogger('spellchecker')
  13.  
  14. class AspellDictionary(object):
  15.     
  16.     def __init__(self, id, name_english, name_native):
  17.         autoassign(self, locals())
  18.  
  19.     
  20.     def __repr__(self):
  21.         return '<%s id=%s name=%s(%r)>' % (type(self).__name__, self.id, self.name_english, self.name_native)
  22.  
  23.     
  24.     def name_description(self):
  25.         return self.name_english
  26.  
  27.     name_description = property(name_description)
  28.  
  29.  
  30. class RemoteAspellDictionary(AspellDictionary):
  31.     ROOT = 'http://ftp.gnu.org/gnu/aspell/dict/'
  32.     
  33.     def __init__(self, id, n_e, n_n, dict_dir, dict_path):
  34.         AspellDictionary.__init__(self, id, n_e, n_n)
  35.         self.directory = dict_dir
  36.         self.package_path = dict_path
  37.         self.digest_location = dict_path + '.sig'
  38.         self.sig = None
  39.         self.needs_update = True
  40.  
  41.     
  42.     def fetch_sig(self, root = None):
  43.         if self.sig is None:
  44.             (response, content) = urlcacheopen(self.get_digest_location(root))
  45.             if not response.ok:
  46.                 return False
  47.             
  48.             self.sig = content
  49.             self.needs_update = not (response.fromcache)
  50.         
  51.         return True
  52.  
  53.     
  54.     def clear_sig(self, root = None):
  55.         pass
  56.  
  57.     
  58.     def get_package_path(self, root = None):
  59.         if not root:
  60.             pass
  61.         return httpjoin(self.ROOT, self.package_path)
  62.  
  63.     
  64.     def get_digest_location(self, root = None):
  65.         return self.get_package_path(root) + '.sig'
  66.  
  67.     
  68.     def get_directory(self, root = None):
  69.         if not root:
  70.             pass
  71.         return httpjoin(self.ROOT, self.directory)
  72.  
  73.     
  74.     def __repr__(self):
  75.         return AspellDictionary.__repr__(self)[:-1] + ' url=%s>' % self.package_path
  76.  
  77.  
  78.  
  79. class LocalAspellDictionary(AspellDictionary):
  80.     
  81.     def __init__(self, id, n_e, n_n, signame):
  82.         AspellDictionary(self, id, n_e, n_n)
  83.         self.signame = signame
  84.  
  85.  
  86.  
  87. def AspellIndexToYaml(root = None, outfile = None):
  88.     import syck
  89.     index = _GetAspellIndex(root)
  90.     mydict = { }
  91.     
  92.     def RAD_to_dict(rad):
  93.         res = { }
  94.         if rad.name_native is not None:
  95.             res.update(name_native = rad.name_native.encode('utf-8'))
  96.         
  97.         res.update(name_english = rad.name_english.encode('utf-8'), location = rad.package_path.encode('utf-8'))
  98.         return res
  99.  
  100.     for d in index:
  101.         mydict[d.id.encode('utf-8')] = RAD_to_dict(d)
  102.     
  103.     return syck.dump(mydict, outfile)
  104.  
  105.  
  106. def _GetAspellIndex(root = None):
  107.     RAD = RemoteAspellDictionary
  108.     if not root:
  109.         pass
  110.     (response, content) = urlcacheopen(httpjoin(RAD.ROOT, '0index.html'), decode = False)
  111.     if not response.ok:
  112.         print 'Unhandled HTTP response code: %r' % response
  113.         return ()
  114.     
  115.     soup = soupify(content)
  116.     results = { }
  117.     for row in soup.find('a', attrs = dict(name = '0.50')).findAllNext('tr'):
  118.         contents = row.findAll('td')
  119.         if len(contents) == 4:
  120.             (id, name_english, name_native, dictionary_path) = contents
  121.             id = id.find(href = True)
  122.             if id is None:
  123.                 continue
  124.             
  125.             id = id['href']
  126.             if id not in results:
  127.                 dictionary_path = dictionary_path.find(href = True)
  128.                 if dictionary_path is None:
  129.                     continue
  130.                 
  131.                 dictionary_path = dictionary_path['href']
  132.                 name_english = name_english.renderContents(None).decode('xml')
  133.                 if not name_native.renderContents(None).decode('xml').strip():
  134.                     pass
  135.                 name_native = None
  136.                 results[id] = RAD(id, name_english, name_native, id, dictionary_path)
  137.             
  138.         id not in results
  139.     
  140.     return results.values()
  141.  
  142.  
  143. def DownloadAllDictionaries(infodict, towhere, root = None):
  144.     if root is None:
  145.         root = RemoteAspellDictionary.ROOT
  146.     
  147.     for id in infodict:
  148.         name = infodict[id]['name_english']
  149.         bz2path = infodict[id]['location']
  150.         localpath = path(towhere) / bz2path
  151.         bz2path = httpjoin(root, bz2path)
  152.         localpath = localpath.expand()
  153.         print 'Downloading %s (%s) from %s to %s... ' % (name, id, bz2path, localpath),
  154.         (response, content) = urlcacheopen(bz2path)
  155.         print response.reason
  156.         if response.ok:
  157.             if not localpath.parent.exists():
  158.                 localpath.parent.makedirs()
  159.             
  160.             
  161.             try:
  162.                 f = _[2]
  163.                 f.write(content)
  164.             finally:
  165.                 pass
  166.  
  167.             continue
  168.         open(localpath, 'wb')
  169.     
  170.  
  171.  
  172. def ExtractInfoFiles(localroot):
  173.     localroot = path(localroot)
  174.     for bz2path in localroot.walkfiles('*.tar.bz2'):
  175.         tar = None
  176.         infofile = None
  177.         
  178.         try:
  179.             tar = tarfile.open(bz2path, 'r:bz2')
  180.             for member in tar.getmembers():
  181.                 mempth = path(member.name)
  182.                 if mempth.name == 'info':
  183.                     break
  184.                     continue
  185.             else:
  186.                 print 'Couldn\'t get "info" from %s' % bz2path
  187.                 continue
  188.             infofile = tar.extractfile(member)
  189.             
  190.             try:
  191.                 f = _[2]
  192.                 f.write(infofile.read())
  193.             finally:
  194.                 pass
  195.  
  196.         finally:
  197.             if tar is not None:
  198.                 tar.close()
  199.             
  200.             if infofile is not None:
  201.                 infofile.close()
  202.             
  203.  
  204.     
  205.  
  206.  
  207. class AspellInfoShlexer(shlex.shlex):
  208.     
  209.     def __init__(self, *a, **k):
  210.         if 'posix' not in k:
  211.             k['posix'] = True
  212.         
  213.         shlex.shlex.__init__(self, *a, **k)
  214.         self.whitespace_split = True
  215.  
  216.     
  217.     def get_token(self):
  218.         curlineno = self.lineno
  219.         token = shlex.shlex.get_token(self)
  220.         if self.lineno != curlineno:
  221.             self.pushback.append('\n')
  222.         
  223.         return token
  224.  
  225.  
  226.  
  227. def GetAliases(root, id):
  228.     aliases = []
  229.     root = path(root)
  230.     
  231.     try:
  232.         info = _[2]
  233.         for line in info:
  234.             line = line.rstrip()
  235.             if line.startswith('alias'):
  236.                 (_alias, rest) = line.split(None, 1)
  237.                 rest = rest.split()
  238.                 alias_id = rest[0]
  239.                 alias_names = rest[1:]
  240.                 if alias_id != id:
  241.                     aliases.append((alias_id, alias_names))
  242.                 
  243.             alias_id != id
  244.     finally:
  245.         pass
  246.  
  247.     return aliases
  248.  
  249.  
  250. def GetAllAliases(root):
  251.     root = path(root)
  252.     aliases = { }
  253.     for lang in root.walkdirs():
  254.         lang = lang.name
  255.         aliases = GetAliases(root, lang)
  256.         if aliases:
  257.             print lang, aliases
  258.             continue
  259.     
  260.     return aliases
  261.  
  262.  
  263. def _getshlexer(fpath):
  264.     f = open(fpath, 'rb')
  265.     return AspellInfoShlexer(f)
  266.  
  267.  
  268. def MakeDigsbyDict(lang = 'en', local_dir = None):
  269.     digsby_words = set(('asap', 'asl', 'bbs', 'bff', 'brb', 'btw', 'cya', 'digsby', 'fud', 'fwiw', 'gl', 'ic', 'ily', 'im', 'imho', 'irl', 'jk', 'lmao', 'lol', 'np', 'oic', 'omg', 'plz', 'rofl', 'roflmao', 'thx', 'ttyl', 'ttys', 'u', 'wtf'))
  270.     dict_file = local_dir / ('digsby-%s.rws' % lang)
  271.     cmd = ' '.join([
  272.         '.\\lib\\aspell\\bin\\aspell.exe',
  273.         '--local-data-dir=%s' % subprocess.list2cmdline([
  274.             local_dir]),
  275.         '--lang=%s' % lang,
  276.         'create',
  277.         'master',
  278.         '"%s"' % dict_file])
  279.     startupinfo = subprocess.STARTUPINFO()
  280.     startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  281.     startupinfo.wShowWindow = subprocess.SW_HIDE
  282.     Popen = Popen
  283.     PIPE = PIPE
  284.     import subprocess
  285.     proc = Popen(cmd.encode('filesys'), stdout = PIPE, stderr = PIPE, stdin = PIPE, startupinfo = startupinfo)
  286.     log.info('Creating Digsby dict in "%s"', lang)
  287.     log.info('Executing Command: %s', cmd)
  288.     result = proc.communicate('\n'.join(digsby_words))
  289.     log.info('Subprocess returned: %s', result)
  290.     return dict_file
  291.  
  292.  
  293. def _main(*a, **k):
  294.     MakeDigsbyDict(*a, **k)
  295.  
  296. if __name__ == '__main__':
  297.     import sys
  298.     _main(sys.argv[1:])
  299.  
  300.