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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from pygments.formatter import Formatter
  5. __all__ = [
  6.     'Terminal256Formatter']
  7.  
  8. class EscapeSequence:
  9.     
  10.     def __init__(self, fg = None, bg = None, bold = False, underline = False):
  11.         self.fg = fg
  12.         self.bg = bg
  13.         self.bold = bold
  14.         self.underline = underline
  15.  
  16.     
  17.     def escape(self, attrs):
  18.         if len(attrs):
  19.             return '\x1b[' + ';'.join(attrs) + 'm'
  20.         return ''
  21.  
  22.     
  23.     def color_string(self):
  24.         attrs = []
  25.         if self.fg is not None:
  26.             attrs.extend(('38', '5', '%i' % self.fg))
  27.         
  28.         if self.bg is not None:
  29.             attrs.extend(('48', '5', '%i' % self.bg))
  30.         
  31.         if self.bold:
  32.             attrs.append('01')
  33.         
  34.         if self.underline:
  35.             attrs.append('04')
  36.         
  37.         return self.escape(attrs)
  38.  
  39.     
  40.     def reset_string(self):
  41.         attrs = []
  42.         if self.fg is not None:
  43.             attrs.append('39')
  44.         
  45.         if self.bg is not None:
  46.             attrs.append('49')
  47.         
  48.         if self.bold or self.underline:
  49.             attrs.append('00')
  50.         
  51.         return self.escape(attrs)
  52.  
  53.  
  54.  
  55. class Terminal256Formatter(Formatter):
  56.     name = 'Terminal256'
  57.     aliases = [
  58.         'terminal256',
  59.         'console256',
  60.         '256']
  61.     filenames = []
  62.     
  63.     def __init__(self, **options):
  64.         Formatter.__init__(self, **options)
  65.         self.xterm_colors = []
  66.         self.best_match = { }
  67.         self.style_string = { }
  68.         self.usebold = 'nobold' not in options
  69.         self.useunderline = 'nounderline' not in options
  70.         self._build_color_table()
  71.         self._setup_styles()
  72.  
  73.     
  74.     def _build_color_table(self):
  75.         self.xterm_colors.append((0, 0, 0))
  76.         self.xterm_colors.append((205, 0, 0))
  77.         self.xterm_colors.append((0, 205, 0))
  78.         self.xterm_colors.append((205, 205, 0))
  79.         self.xterm_colors.append((0, 0, 238))
  80.         self.xterm_colors.append((205, 0, 205))
  81.         self.xterm_colors.append((0, 205, 205))
  82.         self.xterm_colors.append((229, 229, 229))
  83.         self.xterm_colors.append((127, 127, 127))
  84.         self.xterm_colors.append((255, 0, 0))
  85.         self.xterm_colors.append((0, 255, 0))
  86.         self.xterm_colors.append((255, 255, 0))
  87.         self.xterm_colors.append((92, 92, 255))
  88.         self.xterm_colors.append((255, 0, 255))
  89.         self.xterm_colors.append((0, 255, 255))
  90.         self.xterm_colors.append((255, 255, 255))
  91.         valuerange = (0, 95, 135, 175, 215, 255)
  92.         for i in range(217):
  93.             r = valuerange[i // 36 % 6]
  94.             g = valuerange[i // 6 % 6]
  95.             b = valuerange[i % 6]
  96.             self.xterm_colors.append((r, g, b))
  97.         
  98.         for i in range(1, 22):
  99.             v = 8 + i * 10
  100.             self.xterm_colors.append((v, v, v))
  101.         
  102.  
  103.     
  104.     def _closest_color(self, r, g, b):
  105.         distance = 198147
  106.         match = 0
  107.         for i in range(0, 254):
  108.             values = self.xterm_colors[i]
  109.             rd = r - values[0]
  110.             gd = g - values[1]
  111.             bd = b - values[2]
  112.             d = rd * rd + gd * gd + bd * bd
  113.             if d < distance:
  114.                 match = i
  115.                 distance = d
  116.                 continue
  117.         
  118.         return match
  119.  
  120.     
  121.     def _color_index(self, color):
  122.         index = self.best_match.get(color, None)
  123.         if index is None:
  124.             
  125.             try:
  126.                 rgb = int(str(color), 16)
  127.             except ValueError:
  128.                 rgb = 0
  129.  
  130.             r = rgb >> 16 & 255
  131.             g = rgb >> 8 & 255
  132.             b = rgb & 255
  133.             index = self._closest_color(r, g, b)
  134.             self.best_match[color] = index
  135.         
  136.         return index
  137.  
  138.     
  139.     def _setup_styles(self):
  140.         for ttype, ndef in self.style:
  141.             escape = EscapeSequence()
  142.             if ndef['color']:
  143.                 escape.fg = self._color_index(ndef['color'])
  144.             
  145.             if ndef['bgcolor']:
  146.                 escape.bg = self._color_index(ndef['bgcolor'])
  147.             
  148.             if self.usebold and ndef['bold']:
  149.                 escape.bold = True
  150.             
  151.             if self.useunderline and ndef['underline']:
  152.                 escape.underline = True
  153.             
  154.             self.style_string[str(ttype)] = (escape.color_string(), escape.reset_string())
  155.         
  156.  
  157.     
  158.     def format(self, tokensource, outfile):
  159.         if not (self.encoding) and hasattr(outfile, 'encoding') and hasattr(outfile, 'isatty') and outfile.isatty():
  160.             self.encoding = outfile.encoding
  161.         
  162.         return Formatter.format(self, tokensource, outfile)
  163.  
  164.     
  165.     def format_unencoded(self, tokensource, outfile):
  166.         for ttype, value in tokensource:
  167.             not_found = True
  168.             while ttype and not_found:
  169.                 
  170.                 try:
  171.                     (on, off) = self.style_string[str(ttype)]
  172.                     spl = value.split('\n')
  173.                     for line in spl[:-1]:
  174.                         if line:
  175.                             outfile.write(on + line + off)
  176.                         
  177.                         outfile.write('\n')
  178.                     
  179.                     if spl[-1]:
  180.                         outfile.write(on + spl[-1] + off)
  181.                     
  182.                     not_found = False
  183.                 continue
  184.                 except KeyError:
  185.                     ttype = ttype[:-1]
  186.                     continue
  187.                 
  188.  
  189.                 None<EXCEPTION MATCH>KeyError
  190.             if not_found:
  191.                 outfile.write(value)
  192.                 continue
  193.         
  194.  
  195.  
  196.