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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import getopt
  6. from textwrap import dedent
  7. from pygments import __version__, highlight
  8. from pygments.util import ClassNotFound, OptionError, docstring_headline
  9. from pygments.lexers import get_all_lexers, get_lexer_by_name, get_lexer_for_filename, find_lexer_class, guess_lexer, TextLexer
  10. from pygments.formatters import get_all_formatters, get_formatter_by_name, get_formatter_for_filename, find_formatter_class, TerminalFormatter
  11. from pygments.filters import get_all_filters, find_filter_class
  12. from pygments.styles import get_all_styles, get_style_by_name
  13. USAGE = 'Usage: %s [-l <lexer> | -g] [-F <filter>[:<options>]] [-f <formatter>]\n          [-O <options>] [-P <option=value>] [-o <outfile>] [<infile>]\n\n       %s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>]\n       %s -L [<which> ...]\n       %s -N <filename>\n       %s -H <type> <name>\n       %s -h | -V\n\nHighlight the input file and write the result to <outfile>.\n\nIf no input file is given, use stdin, if -o is not given, use stdout.\n\n<lexer> is a lexer name (query all lexer names with -L). If -l is not\ngiven, the lexer is guessed from the extension of the input file name\n(this obviously doesn\'t work if the input is stdin).  If -g is passed,\nattempt to guess the lexer from the file contents, or pass through as\nplain text if this fails (this can work for stdin).\n\nLikewise, <formatter> is a formatter name, and will be guessed from\nthe extension of the output file name. If no output file is given,\nthe terminal formatter will be used by default.\n\nWith the -O option, you can give the lexer and formatter a comma-\nseparated list of options, e.g. ``-O bg=light,python=cool``.\n\nThe -P option adds lexer and formatter options like the -O option, but\nyou can only give one option per -P. That way, the option value may\ncontain commas and equals signs, which it can\'t with -O, e.g.\n``-P "heading=Pygments, the Python highlighter".\n\nWith the -F option, you can add filters to the token stream, you can\ngive options in the same way as for -O after a colon (note: there must\nnot be spaces around the colon).\n\nThe -O, -P and -F options can be given multiple times.\n\nWith the -S option, print out style definitions for style <style>\nfor formatter <formatter>. The argument given by -a is formatter\ndependent.\n\nThe -L option lists lexers, formatters, styles or filters -- set\n`which` to the thing you want to list (e.g. "styles"), or omit it to\nlist everything.\n\nThe -N option guesses and prints out a lexer name based solely on\nthe given filename. It does not take input or highlight anything.\nIf no specific lexer can be determined "text" is returned.\n\nThe -H option prints detailed help for the object <name> of type <type>,\nwhere <type> is one of "lexer", "formatter" or "filter".\n\nThe -h option prints this help.\nThe -V option prints the package version.\n'
  14.  
  15. def _parse_options(o_strs):
  16.     opts = { }
  17.     if not o_strs:
  18.         return opts
  19.     for o_str in o_strs:
  20.         if not o_str:
  21.             continue
  22.         
  23.         o_args = o_str.split(',')
  24.         for o_arg in o_args:
  25.             o_arg = o_arg.strip()
  26.             
  27.             try:
  28.                 (o_key, o_val) = o_arg.split('=')
  29.                 o_key = o_key.strip()
  30.                 o_val = o_val.strip()
  31.             except ValueError:
  32.                 opts[o_arg] = True
  33.                 continue
  34.  
  35.             opts[o_key] = o_val
  36.         
  37.     
  38.     return opts
  39.  
  40.  
  41. def _parse_filters(f_strs):
  42.     filters = []
  43.     if not f_strs:
  44.         return filters
  45.     for f_str in f_strs:
  46.         if ':' in f_str:
  47.             (fname, fopts) = f_str.split(':', 1)
  48.             filters.append((fname, _parse_options([
  49.                 fopts])))
  50.             continue
  51.         f_strs
  52.         filters.append((f_str, { }))
  53.     
  54.     return filters
  55.  
  56.  
  57. def _print_help(what, name):
  58.     
  59.     try:
  60.         if what == 'lexer':
  61.             cls = find_lexer_class(name)
  62.             print 'Help on the %s lexer:' % cls.name
  63.             print dedent(cls.__doc__)
  64.         elif what == 'formatter':
  65.             cls = find_formatter_class(name)
  66.             print 'Help on the %s formatter:' % cls.name
  67.             print dedent(cls.__doc__)
  68.         elif what == 'filter':
  69.             cls = find_filter_class(name)
  70.             print 'Help on the %s filter:' % name
  71.             print dedent(cls.__doc__)
  72.     except AttributeError:
  73.         print >>sys.stderr, '%s not found!' % what
  74.  
  75.  
  76.  
  77. def _print_list(what):
  78.     if what == 'lexer':
  79.         print 
  80.         print 'Lexers:'
  81.         print '~~~~~~~'
  82.         info = []
  83.         for fullname, names, exts, _ in get_all_lexers():
  84.             if not exts or '(filenames ' + ', '.join(exts) + ')':
  85.                 pass
  86.             tup = (', '.join(names) + ':', fullname, '')
  87.             info.append(tup)
  88.         
  89.         info.sort()
  90.         for i in info:
  91.             print '* %s\n    %s %s' % i
  92.         
  93.     elif what == 'formatter':
  94.         print 
  95.         print 'Formatters:'
  96.         print '~~~~~~~~~~~'
  97.         info = []
  98.         for cls in get_all_formatters():
  99.             doc = docstring_headline(cls)
  100.             if not cls.filenames or '(filenames ' + ', '.join(cls.filenames) + ')':
  101.                 pass
  102.             tup = (', '.join(cls.aliases) + ':', doc, '')
  103.             info.append(tup)
  104.         
  105.         info.sort()
  106.         for i in info:
  107.             print '* %s\n    %s %s' % i
  108.         
  109.     elif what == 'filter':
  110.         print 
  111.         print 'Filters:'
  112.         print '~~~~~~~~'
  113.         for name in get_all_filters():
  114.             cls = find_filter_class(name)
  115.             print '* ' + name + ':'
  116.             print '    %s' % docstring_headline(cls)
  117.         
  118.     elif what == 'style':
  119.         print 
  120.         print 'Styles:'
  121.         print '~~~~~~~'
  122.         for name in get_all_styles():
  123.             cls = get_style_by_name(name)
  124.             print '* ' + name + ':'
  125.             print '    %s' % docstring_headline(cls)
  126.         
  127.     
  128.  
  129.  
  130. def main(args = sys.argv):
  131.     usage = USAGE % (args[0],) * 6
  132.     
  133.     try:
  134.         (popts, args) = getopt.getopt(args[1:], 'l:f:F:o:O:P:LS:a:N:hVHg')
  135.     except getopt.GetoptError:
  136.         err = None
  137.         print >>sys.stderr, usage
  138.         return 2
  139.  
  140.     opts = { }
  141.     O_opts = []
  142.     P_opts = []
  143.     F_opts = []
  144.     for opt, arg in popts:
  145.         if opt == '-O':
  146.             O_opts.append(arg)
  147.         elif opt == '-P':
  148.             P_opts.append(arg)
  149.         elif opt == '-F':
  150.             F_opts.append(arg)
  151.         
  152.         opts[opt] = arg
  153.     
  154.     if not opts and not args:
  155.         print usage
  156.         return 0
  157.     if opts.pop('-h', None) is not None:
  158.         print usage
  159.         return 0
  160.     if opts.pop('-V', None) is not None:
  161.         print 'Pygments version %s, (c) 2006-2008 by Georg Brandl.' % __version__
  162.         return 0
  163.     L_opt = opts.pop('-L', None)
  164.     if L_opt is not None:
  165.         if opts:
  166.             print >>sys.stderr, usage
  167.             return 2
  168.         main([
  169.             '',
  170.             '-V'])
  171.         for arg in args:
  172.             _print_list(arg.rstrip('s'))
  173.         
  174.         return 0
  175.     H_opt = opts.pop('-H', None)
  176.     if H_opt is not None:
  177.         if opts or len(args) != 2:
  178.             print >>sys.stderr, usage
  179.             return 2
  180.         (what, name) = args
  181.         if what not in ('lexer', 'formatter', 'filter'):
  182.             print >>sys.stderr, usage
  183.             return 2
  184.         _print_help(what, name)
  185.         return 0
  186.     parsed_opts = _parse_options(O_opts)
  187.     opts.pop('-O', None)
  188.     for p_opt in P_opts:
  189.         
  190.         try:
  191.             (name, value) = p_opt.split('=', 1)
  192.         except ValueError:
  193.             H_opt is not None
  194.             H_opt is not None
  195.             L_opt is not None
  196.             parsed_opts[p_opt] = True
  197.             continue
  198.             opts.pop('-V', None) is not None
  199.  
  200.         parsed_opts[name] = value
  201.     
  202.     opts.pop('-P', None)
  203.     infn = opts.pop('-N', None)
  204.     if infn is not None:
  205.         
  206.         try:
  207.             lexer = get_lexer_for_filename(infn, **parsed_opts)
  208.         except ClassNotFound:
  209.             H_opt is not None
  210.             err = H_opt is not None
  211.             L_opt is not None
  212.             lexer = TextLexer()
  213.         except OptionError:
  214.             opts.pop('-h', None) is not None
  215.             err = opts.pop('-h', None) is not None
  216.             not args
  217.             print >>sys.stderr, 'Error:', err
  218.             return 1
  219.  
  220.         print lexer.aliases[0]
  221.         return 0
  222.     S_opt = opts.pop('-S', None)
  223.     a_opt = opts.pop('-a', None)
  224.     if S_opt is not None:
  225.         f_opt = opts.pop('-f', None)
  226.         if not f_opt:
  227.             print >>sys.stderr, usage
  228.             return 2
  229.         if opts or args:
  230.             print >>sys.stderr, usage
  231.             return 2
  232.         
  233.         try:
  234.             parsed_opts['style'] = S_opt
  235.             fmter = get_formatter_by_name(f_opt, **parsed_opts)
  236.         except ClassNotFound:
  237.             args
  238.             err = args
  239.             f_opt
  240.             print >>sys.stderr, err
  241.             return 1
  242.             H_opt is not None
  243.  
  244.         if not a_opt:
  245.             pass
  246.         arg = ''
  247.         
  248.         try:
  249.             print fmter.get_style_defs(arg)
  250.         except Exception:
  251.             args
  252.             err = args
  253.             f_opt
  254.             print >>sys.stderr, 'Error:', err
  255.             return 1
  256.             H_opt is not None
  257.  
  258.         return 0
  259.     if a_opt is not None:
  260.         print >>sys.stderr, usage
  261.         return 2
  262.     F_opts = _parse_filters(F_opts)
  263.     opts.pop('-F', None)
  264.     outfn = opts.pop('-o', None)
  265.     fmter = opts.pop('-f', None)
  266.     if fmter:
  267.         
  268.         try:
  269.             fmter = get_formatter_by_name(fmter, **parsed_opts)
  270.         except (OptionError, ClassNotFound):
  271.             a_opt is not None
  272.             err = a_opt is not None
  273.             S_opt is not None
  274.             print >>sys.stderr, 'Error:', err
  275.             return 1
  276.             H_opt is not None
  277.         
  278.  
  279.     L_opt is not None
  280.     if outfn:
  281.         if not fmter:
  282.             
  283.             try:
  284.                 fmter = get_formatter_for_filename(outfn, **parsed_opts)
  285.             except (OptionError, ClassNotFound):
  286.                 opts.pop('-V', None) is not None
  287.                 err = opts.pop('-V', None) is not None
  288.                 opts.pop('-h', None) is not None
  289.                 print >>sys.stderr, 'Error:', err
  290.                 return 1
  291.             
  292.  
  293.         opts.pop('-V', None) is not None<EXCEPTION MATCH>(OptionError, ClassNotFound)
  294.         
  295.         try:
  296.             outfile = open(outfn, 'wb')
  297.         except Exception:
  298.             opts.pop('-V', None) is not None
  299.             err = opts.pop('-V', None) is not None
  300.             opts.pop('-V', None) is not None
  301.             print >>sys.stderr, 'Error: cannot open outfile:', err
  302.             return 1
  303.             not args
  304.         
  305.  
  306.     opts.pop('-V', None) is not None<EXCEPTION MATCH>Exception
  307.     outfile = sys.stdout
  308.     lexer = opts.pop('-l', None)
  309.     if lexer:
  310.         
  311.         try:
  312.             lexer = get_lexer_by_name(lexer, **parsed_opts)
  313.         except (OptionError, ClassNotFound):
  314.             opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  315.             err = opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  316.             opts.pop('-h', None) is not None
  317.             print >>sys.stderr, 'Error:', err
  318.             return 1
  319.         
  320.  
  321.     opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None<EXCEPTION MATCH>(OptionError, ClassNotFound)
  322.     if args:
  323.         if len(args) > 1:
  324.             print >>sys.stderr, usage
  325.             return 2
  326.         infn = args[0]
  327.         
  328.         try:
  329.             code = open(infn, 'rb').read()
  330.         except Exception:
  331.             len(args) > 1
  332.             err = len(args) > 1
  333.             opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  334.             print >>sys.stderr, 'Error: cannot read infile:', err
  335.             return 1
  336.             opts.pop('-h', None) is not None
  337.  
  338.         if not lexer:
  339.             
  340.             try:
  341.                 lexer = get_lexer_for_filename(infn, code, **parsed_opts)
  342.             except ClassNotFound:
  343.                 len(args) > 1
  344.                 err = len(args) > 1
  345.                 opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  346.                 if '-g' in opts:
  347.                     
  348.                     try:
  349.                         lexer = guess_lexer(code)
  350.                     except ClassNotFound:
  351.                         opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  352.                         opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  353.                         opts.pop('-h', None) is not None
  354.                         lexer = TextLexer()
  355.                     except:
  356.                         opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None<EXCEPTION MATCH>ClassNotFound
  357.                     
  358.  
  359.                 opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None<EXCEPTION MATCH>ClassNotFound
  360.                 print >>sys.stderr, 'Error:', err
  361.                 return 1
  362.             except OptionError:
  363.                 len(args) > 1
  364.                 err = len(args) > 1
  365.                 len(args) > 1
  366.                 print >>sys.stderr, 'Error:', err
  367.                 return 1
  368.                 opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  369.             
  370.  
  371.         len(args) > 1<EXCEPTION MATCH>ClassNotFound
  372.     elif '-g' in opts:
  373.         code = sys.stdin.read()
  374.         
  375.         try:
  376.             lexer = guess_lexer(code)
  377.         except ClassNotFound:
  378.             len(args) > 1
  379.             len(args) > 1
  380.             opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  381.             lexer = TextLexer()
  382.         except:
  383.             len(args) > 1<EXCEPTION MATCH>ClassNotFound
  384.         
  385.  
  386.     len(args) > 1
  387.     if not lexer:
  388.         print >>sys.stderr, 'Error: no lexer name given and reading ' + 'from stdin (try using -g or -l <lexer>)'
  389.         return 2
  390.     code = sys.stdin.read()
  391.     
  392.     try:
  393.         for fname, fopts in F_opts:
  394.             lexer.add_filter(fname, **fopts)
  395.         
  396.         highlight(code, lexer, fmter, outfile)
  397.     except Exception:
  398.         opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None if 'encoding' not in parsed_opts and 'outencoding' not in parsed_opts else len(args) > 1
  399.         err = opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None if 'encoding' not in parsed_opts and 'outencoding' not in parsed_opts else len(args) > 1
  400.         opts.pop('-V', None) is not None if not fmter else opts.pop('-V', None) is not None
  401.         import traceback
  402.         info = traceback.format_exception(*sys.exc_info())
  403.         msg = info[-1].strip()
  404.         print >>sys.stderr
  405.         print >>sys.stderr, '*** Error while highlighting:'
  406.         print >>sys.stderr, msg
  407.         return 1
  408.  
  409.     return 0
  410.  
  411.