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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. from pygments.token import String, Comment, Keyword, Name, Error, Whitespace, string_to_tokentype
  6. from pygments.filter import Filter
  7. from pygments.util import get_list_opt, get_int_opt, get_bool_opt, get_choice_opt, ClassNotFound, OptionError
  8. from pygments.plugin import find_plugin_filters
  9.  
  10. def find_filter_class(filtername):
  11.     if filtername in FILTERS:
  12.         return FILTERS[filtername]
  13.     for name, cls in find_plugin_filters():
  14.         if name == filtername:
  15.             return cls
  16.     
  17.  
  18.  
  19. def get_filter_by_name(filtername, **options):
  20.     cls = find_filter_class(filtername)
  21.     if cls:
  22.         return cls(**options)
  23.     raise ClassNotFound('filter %r not found' % filtername)
  24.  
  25.  
  26. def get_all_filters():
  27.     for name in FILTERS:
  28.         yield name
  29.     
  30.     for name, _ in find_plugin_filters():
  31.         yield name
  32.     
  33.  
  34.  
  35. def _replace_special(ttype, value, regex, specialttype, replacefunc = (lambda x: x)):
  36.     last = 0
  37.     for match in regex.finditer(value):
  38.         start = match.start()
  39.         end = match.end()
  40.         if start != last:
  41.             yield (ttype, value[last:start])
  42.         
  43.         yield (specialttype, replacefunc(value[start:end]))
  44.         last = end
  45.     
  46.     if last != len(value):
  47.         yield (ttype, value[last:])
  48.     
  49.  
  50.  
  51. class CodeTagFilter(Filter):
  52.     
  53.     def __init__(self, **options):
  54.         Filter.__init__(self, **options)
  55.         tags = get_list_opt(options, 'codetags', [
  56.             'XXX',
  57.             'TODO',
  58.             'BUG',
  59.             'NOTE'])
  60.         self.tag_re = '|'.join([] % [](_[1]))
  61.  
  62.     
  63.     def filter(self, lexer, stream):
  64.         regex = self.tag_re
  65.         for ttype, value in stream:
  66.             if (ttype in String.Doc or ttype in Comment) and ttype not in Comment.Preproc:
  67.                 for sttype, svalue in _replace_special(ttype, value, regex, Comment.Special):
  68.                     yield (sttype, svalue)
  69.                 
  70.             yield (ttype, value)
  71.         
  72.  
  73.  
  74.  
  75. class KeywordCaseFilter(Filter):
  76.     
  77.     def __init__(self, **options):
  78.         Filter.__init__(self, **options)
  79.         case = get_choice_opt(options, 'case', [
  80.             'lower',
  81.             'upper',
  82.             'capitalize'], 'lower')
  83.         self.convert = getattr(unicode, case)
  84.  
  85.     
  86.     def filter(self, lexer, stream):
  87.         for ttype, value in stream:
  88.             if ttype in Keyword:
  89.                 yield (ttype, self.convert(value))
  90.                 continue
  91.             yield (ttype, value)
  92.         
  93.  
  94.  
  95.  
  96. class NameHighlightFilter(Filter):
  97.     
  98.     def __init__(self, **options):
  99.         Filter.__init__(self, **options)
  100.         self.names = set(get_list_opt(options, 'names', []))
  101.         tokentype = options.get('tokentype')
  102.         if tokentype:
  103.             self.tokentype = string_to_tokentype(tokentype)
  104.         else:
  105.             self.tokentype = Name.Function
  106.  
  107.     
  108.     def filter(self, lexer, stream):
  109.         for ttype, value in stream:
  110.             if ttype is Name and value in self.names:
  111.                 yield (self.tokentype, value)
  112.                 continue
  113.             yield (ttype, value)
  114.         
  115.  
  116.  
  117.  
  118. class ErrorToken(Exception):
  119.     pass
  120.  
  121.  
  122. class RaiseOnErrorTokenFilter(Filter):
  123.     
  124.     def __init__(self, **options):
  125.         Filter.__init__(self, **options)
  126.         self.exception = options.get('excclass', ErrorToken)
  127.         
  128.         try:
  129.             if not issubclass(self.exception, Exception):
  130.                 raise TypeError
  131.             issubclass(self.exception, Exception)
  132.         except TypeError:
  133.             raise OptionError('excclass option is not an exception class')
  134.  
  135.  
  136.     
  137.     def filter(self, lexer, stream):
  138.         for ttype, value in stream:
  139.             if ttype is Error:
  140.                 raise self.exception(value)
  141.             ttype is Error
  142.             yield (ttype, value)
  143.         
  144.  
  145.  
  146.  
  147. class VisibleWhitespaceFilter(Filter):
  148.     
  149.     def __init__(self, **options):
  150.         Filter.__init__(self, **options)
  151.         for name, default in {
  152.             'spaces': u'┬╖',
  153.             'tabs': u'┬╗',
  154.             'newlines': u'┬╢' }.items():
  155.             opt = options.get(name, False)
  156.             if isinstance(opt, basestring) and len(opt) == 1:
  157.                 setattr(self, name, opt)
  158.                 continue
  159.             if not opt or default:
  160.                 pass
  161.             setattr(self, name, '')
  162.         
  163.         tabsize = get_int_opt(options, 'tabsize', 8)
  164.         if self.tabs:
  165.             self.tabs += ' ' * (tabsize - 1)
  166.         
  167.         if self.newlines:
  168.             self.newlines += '\n'
  169.         
  170.         self.wstt = get_bool_opt(options, 'wstokentype', True)
  171.  
  172.     
  173.     def filter(self, lexer, stream):
  174.         if self.wstt:
  175.             if not self.spaces:
  176.                 pass
  177.             spaces = ' '
  178.             if not self.tabs:
  179.                 pass
  180.             tabs = '\t'
  181.             if not self.newlines:
  182.                 pass
  183.             newlines = '\n'
  184.             regex = re.compile('\\s')
  185.             
  186.             def replacefunc(wschar):
  187.                 if wschar == ' ':
  188.                     return spaces
  189.                 if wschar == '\t':
  190.                     return tabs
  191.                 if wschar == '\n':
  192.                     return newlines
  193.                 return wschar
  194.  
  195.             for ttype, value in stream:
  196.                 for sttype, svalue in _replace_special(ttype, value, regex, Whitespace, replacefunc):
  197.                     yield (sttype, svalue)
  198.                     (None, None, None)
  199.                 
  200.             
  201.         else:
  202.             spaces = self.spaces
  203.             tabs = self.tabs
  204.             newlines = self.newlines
  205.             for ttype, value in stream:
  206.                 if spaces:
  207.                     value = value.replace(' ', spaces)
  208.                 
  209.                 if tabs:
  210.                     value = value.replace('\t', tabs)
  211.                 
  212.                 if newlines:
  213.                     value = value.replace('\n', newlines)
  214.                 
  215.                 yield (ttype, value)
  216.             
  217.  
  218.  
  219.  
  220. class GobbleFilter(Filter):
  221.     
  222.     def __init__(self, **options):
  223.         Filter.__init__(self, **options)
  224.         self.n = get_int_opt(options, 'n', 0)
  225.  
  226.     
  227.     def gobble(self, value, left):
  228.         if left < len(value):
  229.             return (value[left:], 0)
  230.         return ('', left - len(value))
  231.  
  232.     
  233.     def filter(self, lexer, stream):
  234.         n = self.n
  235.         left = n
  236.         for ttype, value in stream:
  237.             parts = value.split('\n')
  238.             (parts[0], left) = self.gobble(parts[0], left)
  239.             for i in range(1, len(parts)):
  240.                 (parts[i], left) = self.gobble(parts[i], n)
  241.             
  242.             value = '\n'.join(parts)
  243.             if value != '':
  244.                 yield (ttype, value)
  245.                 continue
  246.         
  247.  
  248.  
  249.  
  250. class TokenMergeFilter(Filter):
  251.     
  252.     def __init__(self, **options):
  253.         Filter.__init__(self, **options)
  254.  
  255.     
  256.     def filter(self, lexer, stream):
  257.         output = []
  258.         current_type = None
  259.         current_value = None
  260.         for ttype, value in stream:
  261.             if ttype is current_type:
  262.                 current_value += value
  263.                 continue
  264.             if current_type is not None:
  265.                 yield (current_type, current_value)
  266.             
  267.             current_type = ttype
  268.             current_value = value
  269.         
  270.         if current_type is not None:
  271.             yield (current_type, current_value)
  272.         
  273.  
  274.  
  275. FILTERS = {
  276.     'codetagify': CodeTagFilter,
  277.     'keywordcase': KeywordCaseFilter,
  278.     'highlight': NameHighlightFilter,
  279.     'raiseonerror': RaiseOnErrorTokenFilter,
  280.     'whitespace': VisibleWhitespaceFilter,
  281.     'gobble': GobbleFilter,
  282.     'tokenmerge': TokenMergeFilter }
  283.