home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1487 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  3.9 KB  |  115 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. from PyQt4.Qt import QTextCharFormat, QFont, QBrush, QColor
  8. from pygments.formatter import Formatter as PF
  9. from pygments.token import Token, Generic, string_to_tokentype
  10.  
  11. class Formatter(object):
  12.     
  13.     def __init__(self, prompt, continuation, style = 'default'):
  14.         if len(prompt) != len(continuation):
  15.             raise ValueError('%r does not have the same length as %r' % (prompt, continuation))
  16.         len(prompt) != len(continuation)
  17.         self.prompt = prompt
  18.         self.continuation = continuation
  19.         self.set_style(style)
  20.  
  21.     
  22.     def set_style(self, style):
  23.         pf = PF(style = style)
  24.         self.styles = { }
  25.         self.normal = self.base_fmt()
  26.         self.background_color = pf.style.background_color
  27.         self.color = 'black'
  28.         for ttype, ndef in pf.style:
  29.             fmt = self.base_fmt()
  30.             fmt.setProperty(fmt.UserProperty, str(ttype))
  31.             if ndef['color']:
  32.                 fmt.setForeground(QBrush(QColor('#%s' % ndef['color'])))
  33.                 fmt.setUnderlineColor(QColor('#%s' % ndef['color']))
  34.                 if ttype == Generic.Output:
  35.                     self.color = '#%s' % ndef['color']
  36.                 
  37.             
  38.             if ndef['bold']:
  39.                 fmt.setFontWeight(QFont.Bold)
  40.             
  41.             if ndef['italic']:
  42.                 fmt.setFontItalic(True)
  43.             
  44.             if ndef['underline']:
  45.                 fmt.setFontUnderline(True)
  46.             
  47.             if ndef['bgcolor']:
  48.                 fmt.setBackground(QBrush(QColor('#%s' % ndef['bgcolor'])))
  49.             
  50.             if ndef['border']:
  51.                 pass
  52.             
  53.             self.styles[ttype] = fmt
  54.         
  55.  
  56.     
  57.     def get_fmt(self, token):
  58.         if type(token) != type(Token.Generic):
  59.             token = string_to_tokentype(token)
  60.         
  61.         fmt = self.styles.get(token, None)
  62.         if fmt is None:
  63.             fmt = self.base_fmt()
  64.             fmt.setProperty(fmt.UserProperty, str(token))
  65.         
  66.         return fmt
  67.  
  68.     
  69.     def base_fmt(self):
  70.         fmt = QTextCharFormat()
  71.         fmt.setFontFamily('monospace')
  72.         return fmt
  73.  
  74.     
  75.     def render_raw(self, raw, cursor):
  76.         cursor.insertText(raw, self.normal)
  77.  
  78.     
  79.     def render_syntax_error(self, tb, cursor):
  80.         fmt = self.get_fmt(Token.Error)
  81.         cursor.insertText(tb, fmt)
  82.  
  83.     
  84.     def render(self, tokens, cursor):
  85.         lastval = ''
  86.         lasttype = None
  87.         for ttype, value in tokens:
  88.             while ttype not in self.styles:
  89.                 ttype = ttype.parent
  90.             if ttype == lasttype:
  91.                 lastval += value
  92.                 continue
  93.             if lastval:
  94.                 fmt = self.styles[lasttype]
  95.                 cursor.insertText(lastval, fmt)
  96.             
  97.             lastval = value
  98.             lasttype = ttype
  99.         
  100.         if lastval:
  101.             fmt = self.styles[lasttype]
  102.             cursor.insertText(lastval, fmt)
  103.         
  104.  
  105.     
  106.     def render_prompt(self, is_continuation, cursor):
  107.         pr = None if is_continuation else self.prompt
  108.         fmt = self.get_fmt(Generic.Prompt)
  109.         if fmt is None:
  110.             fmt = self.base_fmt()
  111.         
  112.         cursor.insertText(pr, fmt)
  113.  
  114.  
  115.