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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from pygments.formatter import Formatter
  5. from pygments.util import OptionError, get_choice_opt, b
  6. from pygments.token import Token
  7. from pygments.console import colorize
  8. __all__ = [
  9.     'NullFormatter',
  10.     'RawTokenFormatter']
  11.  
  12. class NullFormatter(Formatter):
  13.     name = 'Text only'
  14.     aliases = [
  15.         'text',
  16.         'null']
  17.     filenames = [
  18.         '*.txt']
  19.     
  20.     def format(self, tokensource, outfile):
  21.         enc = self.encoding
  22.         for ttype, value in tokensource:
  23.             if enc:
  24.                 outfile.write(value.encode(enc))
  25.                 continue
  26.             outfile.write(value)
  27.         
  28.  
  29.  
  30.  
  31. class RawTokenFormatter(Formatter):
  32.     name = 'Raw tokens'
  33.     aliases = [
  34.         'raw',
  35.         'tokens']
  36.     filenames = [
  37.         '*.raw']
  38.     unicodeoutput = False
  39.     
  40.     def __init__(self, **options):
  41.         Formatter.__init__(self, **options)
  42.         if self.encoding:
  43.             raise OptionError('the raw formatter does not support the encoding option')
  44.         self.encoding
  45.         self.encoding = 'ascii'
  46.         self.compress = get_choice_opt(options, 'compress', [
  47.             '',
  48.             'none',
  49.             'gz',
  50.             'bz2'], '')
  51.         self.error_color = options.get('error_color', None)
  52.         if self.error_color is True:
  53.             self.error_color = 'red'
  54.         
  55.         if self.error_color is not None:
  56.             
  57.             try:
  58.                 colorize(self.error_color, '')
  59.             except KeyError:
  60.                 raise ValueError('Invalid color %r specified' % self.error_color)
  61.             except:
  62.                 None<EXCEPTION MATCH>KeyError
  63.             
  64.  
  65.         None<EXCEPTION MATCH>KeyError
  66.  
  67.     
  68.     def format(self, tokensource, outfile):
  69.         
  70.         try:
  71.             outfile.write(b(''))
  72.         except TypeError:
  73.             raise TypeError('The raw tokens formatter needs a binary output file')
  74.  
  75.         if self.compress == 'gz':
  76.             import gzip
  77.             outfile = gzip.GzipFile('', 'wb', 9, outfile)
  78.             
  79.             def write(text):
  80.                 outfile.write(text.encode())
  81.  
  82.             flush = outfile.flush
  83.         elif self.compress == 'bz2':
  84.             import bz2
  85.             compressor = bz2.BZ2Compressor(9)
  86.             
  87.             def write(text):
  88.                 outfile.write(compressor.compress(text.encode()))
  89.  
  90.             
  91.             def flush():
  92.                 outfile.write(compressor.flush())
  93.                 outfile.flush()
  94.  
  95.         else:
  96.             
  97.             def write(text):
  98.                 outfile.write(text.encode())
  99.  
  100.             flush = outfile.flush
  101.         lasttype = None
  102.         lastval = u''
  103.         if self.error_color:
  104.             for ttype, value in tokensource:
  105.                 line = '%s\t%r\n' % (ttype, value)
  106.                 if ttype is Token.Error:
  107.                     write(colorize(self.error_color, line))
  108.                     continue
  109.                 (None,)
  110.                 write(line)
  111.             
  112.         else:
  113.             for ttype, value in tokensource:
  114.                 write('%s\t%r\n' % (ttype, value))
  115.             
  116.         flush()
  117.  
  118.  
  119.