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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import cStringIO
  6. from pygments.lexer import Lexer
  7. from pygments.token import Token, Error, Text
  8. from pygments.util import get_choice_opt, b
  9. __all__ = [
  10.     'TextLexer',
  11.     'RawTokenLexer']
  12.  
  13. class TextLexer(Lexer):
  14.     name = 'Text only'
  15.     aliases = [
  16.         'text']
  17.     filenames = [
  18.         '*.txt']
  19.     mimetypes = [
  20.         'text/plain']
  21.     
  22.     def get_tokens_unprocessed(self, text):
  23.         yield (0, Text, text)
  24.  
  25.  
  26. _ttype_cache = { }
  27. line_re = re.compile(b('.*?\n'))
  28.  
  29. class RawTokenLexer(Lexer):
  30.     name = 'Raw token data'
  31.     aliases = [
  32.         'raw']
  33.     filenames = []
  34.     mimetypes = [
  35.         'application/x-pygments-tokens']
  36.     
  37.     def __init__(self, **options):
  38.         self.compress = get_choice_opt(options, 'compress', [
  39.             '',
  40.             'none',
  41.             'gz',
  42.             'bz2'], '')
  43.         Lexer.__init__(self, **options)
  44.  
  45.     
  46.     def get_tokens(self, text):
  47.         if isinstance(text, unicode):
  48.             text = text.encode('ascii')
  49.         
  50.         if self.compress == 'gz':
  51.             import gzip
  52.             gzipfile = gzip.GzipFile('', 'rb', 9, cStringIO.StringIO(text))
  53.             text = gzipfile.read()
  54.         elif self.compress == 'bz2':
  55.             import bz2
  56.             text = bz2.decompress(text)
  57.         
  58.         text = text.strip(b('\n')) + b('\n')
  59.         for i, t, v in self.get_tokens_unprocessed(text):
  60.             yield (t, v)
  61.         
  62.  
  63.     
  64.     def get_tokens_unprocessed(self, text):
  65.         length = 0
  66.         for match in line_re.finditer(text):
  67.             
  68.             try:
  69.                 (ttypestr, val) = match.group().split(b('\t'), 1)
  70.             except ValueError:
  71.                 val = match.group().decode(self.encoding)
  72.                 ttype = Error
  73.  
  74.             ttype = _ttype_cache.get(ttypestr)
  75.             if not ttype:
  76.                 ttype = Token
  77.                 ttypes = ttypestr.split('.')[1:]
  78.                 for ttype_ in ttypes:
  79.                     if not ttype_ or not ttype_[0].isupper():
  80.                         raise ValueError('malformed token name')
  81.                     not ttype_[0].isupper()
  82.                     ttype = getattr(ttype, ttype_)
  83.                 
  84.                 _ttype_cache[ttypestr] = ttype
  85.             
  86.             val = val[2:-2].decode('unicode-escape')
  87.             yield (length, ttype, val)
  88.             length += len(val)
  89.         
  90.  
  91.  
  92.