home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / common / spelling / dicts.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  9.6 KB  |  298 lines

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