home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1325 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  2.8 KB  |  71 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. from PyQt4.Qt import QThread
  9. from calibre.utils.dictclient import Connection
  10.  
  11. class Lookup(QThread):
  12.     TEMPLATE = u'<html>\n    <body>\n    <div>\n    {0}\n    </div>\n    </body>\n    </html>\n    '
  13.     
  14.     def __init__(self, word, parent = None):
  15.         QThread.__init__(self, parent)
  16.         self.word = None if isinstance(word, unicode) else word
  17.         self.result = None
  18.         self.traceback = None
  19.         self.exception = None
  20.  
  21.     
  22.     def define(self):
  23.         conn = Connection('dict.org')
  24.         self.result = conn.define('!', self.word)
  25.         if self.result:
  26.             self.result = self.result[0].defstr
  27.         
  28.  
  29.     
  30.     def run(self):
  31.         
  32.         try:
  33.             self.define()
  34.         except Exception:
  35.             e = None
  36.             import traceback
  37.             self.exception = e
  38.             self.traceback = traceback.format_exc()
  39.  
  40.  
  41.     
  42.     def format_exception(self):
  43.         lines = [
  44.             '<b>Failed to connect to dict.org</b>',
  45.             '']
  46.         lines += self.traceback.splitlines()
  47.         ans = '<br>'.join(lines)
  48.         if not isinstance(ans, unicode):
  49.             ans = ans.decode('utf-8')
  50.         
  51.         return self.TEMPLATE.format(ans)
  52.  
  53.     
  54.     def no_results(self):
  55.         ans = _('No results found for:') + ' ' + self.word.decode('utf-8')
  56.         return self.TEMPLATE.format(ans)
  57.  
  58.     
  59.     def html_result(self):
  60.         if self.exception is not None:
  61.             return self.format_exception()
  62.         if not self.result:
  63.             return self.no_results()
  64.         lines = self.result.splitlines()
  65.         lines[0] = '<b>' + lines[0] + '</b>'
  66.         ans = '<br>'.join(lines)
  67.         return self.TEMPLATE.format(ans)
  68.  
  69.     html_result = property(html_result)
  70.  
  71.