ó
½½`Tc           @  sW  d  Z  d d l m Z d d l Z d d l Z d d l m Z d d l m Z m	 Z	 d d l
 m Z m Z m Z m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z d d l m Z m Z d d	 l m Z m Z m Z m Z m  Z  d d
 l! m" Z" m# Z# d d l$ m% Z% m& Z& d Z' d „  Z( d „  Z) d „  Z* d „  Z+ e j, d „ Z- d S(   s¼   
    pygments.cmdline
    ~~~~~~~~~~~~~~~~

    Command line interface.

    :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iÿÿÿÿ(   t   print_functionN(   t   dedent(   t   __version__t	   highlight(   t   ClassNotFoundt   OptionErrort   docstring_headlinet   guess_decodet   guess_decode_from_terminalt   terminal_encoding(   t   get_all_lexerst   get_lexer_by_namet   guess_lexert   get_lexer_for_filenamet   find_lexer_classt	   TextLexer(   t   LatexEmbeddedLexert   LatexFormatter(   t   get_all_formatterst   get_formatter_by_namet   get_formatter_for_filenamet   find_formatter_classt   TerminalFormatter(   t   get_all_filterst   find_filter_class(   t   get_all_stylest   get_style_by_names©
  Usage: %s [-l <lexer> | -g] [-F <filter>[:<options>]] [-f <formatter>]
          [-O <options>] [-P <option=value>] [-s] [-o <outfile>] [<infile>]

       %s -S <style> -f <formatter> [-a <arg>] [-O <options>] [-P <option=value>]
       %s -L [<which> ...]
       %s -N <filename>
       %s -H <type> <name>
       %s -h | -V

Highlight the input file and write the result to <outfile>.

If no input file is given, use stdin, if -o is not given, use stdout.

If -s is passed, lexing will be done in "streaming" mode, reading and
highlighting one line at a time.  This will only work properly with
lexers that have no constructs spanning multiple lines!

<lexer> is a lexer name (query all lexer names with -L). If -l is not
given, the lexer is guessed from the extension of the input file name
(this obviously doesn't work if the input is stdin).  If -g is passed,
attempt to guess the lexer from the file contents, or pass through as
plain text if this fails (this can work for stdin).

Likewise, <formatter> is a formatter name, and will be guessed from
the extension of the output file name. If no output file is given,
the terminal formatter will be used by default.

With the -O option, you can give the lexer and formatter a comma-
separated list of options, e.g. ``-O bg=light,python=cool``.

The -P option adds lexer and formatter options like the -O option, but
you can only give one option per -P. That way, the option value may
contain commas and equals signs, which it can't with -O, e.g.
``-P "heading=Pygments, the Python highlighter".

With the -F option, you can add filters to the token stream, you can
give options in the same way as for -O after a colon (note: there must
not be spaces around the colon).

The -O, -P and -F options can be given multiple times.

With the -S option, print out style definitions for style <style>
for formatter <formatter>. The argument given by -a is formatter
dependent.

The -L option lists lexers, formatters, styles or filters -- set
`which` to the thing you want to list (e.g. "styles"), or omit it to
list everything.

The -N option guesses and prints out a lexer name based solely on
the given filename. It does not take input or highlight anything.
If no specific lexer can be determined "text" is returned.

The -H option prints detailed help for the object <name> of type <type>,
where <type> is one of "lexer", "formatter" or "filter".

The -s option processes lines one at a time until EOF, rather than
waiting to process the entire file.  This only works for stdin, and
is intended for streaming input such as you get from 'tail -f'.
Example usage: "tail -f sql.log | pygmentize -s -l sql"

The -h option prints this help.
The -V option prints the package version.
c         C  s¹   i  } |  s | Sx¢ |  D]š } | s) q n  | j  d ƒ } xv | D]n } | j ƒ  } y4 | j  d d ƒ \ } } | j ƒ  } | j ƒ  } Wn t k
 r¢ t | | <q? X| | | <q? Wq W| S(   Nt   ,t   =i   (   t   splitt   stript
   ValueErrort   True(   t   o_strst   optst   o_strt   o_argst   o_argt   o_keyt   o_val(    (    s4   /usr/lib/python2.7/dist-packages/pygments/cmdline.pyt   _parse_optionsb   s"    c         C  s{   g  } |  s | Sxd |  D]\ } d | k r` | j  d d ƒ \ } } | j | t | g ƒ f ƒ q | j | i  f ƒ q W| S(   Nt   :i   (   R   t   appendR(   (   t   f_strst   filterst   f_strt   fnamet   fopts(    (    s4   /usr/lib/python2.7/dist-packages/pygments/cmdline.pyt   _parse_filtersw   s    c         C  sí   y¾ |  d k rB t  | ƒ } t d | j ƒ t t | j ƒ ƒ n{ |  d k r t | ƒ } t d | j ƒ t t | j ƒ ƒ n< |  d k r½ t | ƒ } t d | ƒ t t | j ƒ ƒ n  Wn( t k
 rè t d |  d t j	 ƒn Xd  S(	   Nt   lexers   Help on the %s lexer:t	   formatters   Help on the %s formatter:t   filters   Help on the %s filter:s   %s not found!t   file(
   R   t   printt   nameR   t   __doc__R   R   t   AttributeErrort   syst   stderr(   t   whatR6   t   cls(    (    s4   /usr/lib/python2.7/dist-packages/pygments/cmdline.pyt   _print_help„   s    c         C  sk  |  d k r¿ t  ƒ  t  d ƒ t  d ƒ g  } xc t ƒ  D]X \ } } } } d j | ƒ d | | ry d d j | ƒ d p| d f } | j | ƒ q7 W| j ƒ  xÇ| D] } t  d	 | ƒ q¤ Wn¨|  d
 k r‡t  ƒ  t  d ƒ t  d ƒ g  } xl t ƒ  D]a } t | ƒ }	 d j | j ƒ d |	 | j rAd d j | j ƒ d pDd f } | j | ƒ qö W| j ƒ  xÿ | D] } t  d	 | ƒ qlWnà |  d k r÷t  ƒ  t  d ƒ t  d ƒ x¶ t	 ƒ  D]8 }
 t
 |
 ƒ } t  d |
 d ƒ t  d t | ƒ ƒ q¸Wnp |  d k rgt  ƒ  t  d ƒ t  d ƒ xF t ƒ  D]8 }
 t |
 ƒ } t  d |
 d ƒ t  d t | ƒ ƒ q(Wn  d  S(   NR1   s   Lexers:s   ~~~~~~~s   , R)   s   (filenames t   )t    s   * %s
    %s %sR2   s   Formatters:s   ~~~~~~~~~~~R3   s   Filters:s   ~~~~~~~~s   * s       %st   styles   Styles:(   R5   R
   t   joinR*   t   sortR   R   t   aliasest	   filenamesR   R   R   R   (   R;   t   infot   fullnamet   namest   extst   _t   tupt   iR<   t   docR6   (    (    s4   /usr/lib/python2.7/dist-packages/pygments/cmdline.pyt   _print_list–   sR    

&


#




c   (      C  sâ  t  |  d f d } y  t j |  d d ƒ \ } }  Wn( t j k
 r_ t | d t j ƒd SXi  } g  } g  } g  } xr | D]j \ } } | d k r§ | j | ƒ n8 | d k rÃ | j | ƒ n | d	 k rß | j | ƒ n  | | | <q W| j d
 d" ƒ d" k	 rt | ƒ d S| j d d" ƒ d" k	 r=t d t	 ƒ d S| j d d" ƒ }	 |	 d" k	 rË| rxt | d t j ƒd St
 d d g ƒ |  s£d d d d g }  n  x! |  D] } t | j d ƒ ƒ qªWd S| j d d" ƒ }
 |
 d" k	 rX| st |  ƒ d k rt | d t j ƒd S|  \ } } | d9 k rGt | d t j ƒd St | | ƒ d St | ƒ } | j d d" ƒ xR | D]J } y | j d d ƒ \ } } Wn t k
 rºt | | <q{X| | | <q{W| j d d" ƒ | j d | j d ƒ ƒ } | j d | j d ƒ ƒ } | j d d" ƒ } | d" k	 ry t | |  } WnE t k
 r^} t ƒ  } n* t k
 r‡} t d | d t j ƒd SXt | j d ƒ d S| j d d" ƒ } | j d d" ƒ } | d" k	 rº| j d d" ƒ } | süt | d t j ƒd S| s|  rt | d t j ƒd Sy | | d <t | |  } Wn' t k
 re} t | d t j ƒd SX| pod } y t | j | ƒ ƒ Wn* t k
 rµ} t d | d t j ƒd SXd S| d" k	 rÝt | d t j ƒd St | ƒ } | j d	 d" ƒ | j d d" ƒ } | rZy t | |  } WqZt t f k
 rV} t d | d t j ƒd SXn  d" } |  rét |  ƒ d k rt | d t j ƒd Sd | k r²t d  d t j ƒd S|  d } y( t | d! ƒ  } | j ƒ  } Wd" QXWn* t k
 r} t d# | d t j ƒd SX| s,t  | ƒ \ } } n  | sˆy t | | |  } Wqæt k
 r¹} d$ | k rœy t! | |  } Wq¶t k
 r˜t |   } q¶Xqãt d | d t j ƒd Sqæt k
 râ} t d | d t j ƒd SXqˆnŸ d | k rˆt j" d: k rt j# j$ j ƒ  } n t j# j ƒ  } | sIt% | t j# ƒ \ } } n  | sˆy t! | |  } Wq…t k
 rt |   } q…Xqˆn  | j d& d" ƒ } | j d d" ƒ } | rûy t | |  } Wqût t f k
 r÷} t d | d t j ƒd SXn  | r“| sPy t& | |  } WqPt t f k
 rL} t d | d t j ƒd SXn  y t | d' ƒ } WqÏt k
 r} t d( | d t j ƒd SXn< | s¨t' |   } n  t j" d; k rÆt j( j$ } n	 t j( } | sü| rç| | _) qüt* t j( ƒ | _) n  | r±	t j+ d< k r±	| j, d= k r±	t j" d> k r`	d- d" l- } | j. | d | j) ƒ} d" | _) n  y d- d" l/ } Wn t0 k
 rƒ	q±	X| j1 j2 | d. d" d/ d" d0 t3 d1 t ƒ} n  | j d2 d ƒ } t | ƒ d k r
t4 | t5 ƒ r
| d }  | d }! t6 |  |! | ƒ } n  yx$ | D] \ }" }# | j7 |" |#  q
Wd | k rY
t8 | | | | ƒ nÇ | sv
t d3 d t j ƒd Sy• xŽ t j" d? k r 
t j# j$ j9 ƒ  }$ n t j# j9 ƒ  }$ |$ s¹
Pn  | sØ
t% |$ t j# ƒ d }$ n  t8 |$ | | | ƒ t: | d4 ƒ r|
| j; ƒ  q|
q|
Wn t< k
 rd SXWnº t k
 rÝ‚  d- d" l= }% |% j> t j? ƒ  Œ  }& |& d- j@ ƒ  }' t |& ƒ d% k r£|' d5 |& d6 j d7 ƒ d j@ ƒ  d 7}' n  t d t j ƒ t d8 d t j ƒt |' d t j ƒd SXd S(@   s(   
    Main command line entry point.
    i    i   i   s   l:f:F:o:O:P:LS:a:N:hVHgsR4   i   s   -Os   -Ps   -Fs   -hs   -Vs3   Pygments version %s, (c) 2006-2014 by Georg Brandl.s   -LR?   R1   R2   R3   R@   t   ss   -HR   t
   inencodingt   encodingt   outencodings   -Ns   Error:s   -Ss   -as   -fs   -ls   -ss5   Error: -s option not usable when input file specifiedt   rbNs   Error: cannot read infile:s   -gi   s   -ot   wbs   Error: cannot open outfile:t   win32t   cygwint   Terminalt   Terminal256iÿÿÿÿt   convertR   t	   autoresett   wrapt   escapeinsides7   Error: when using -s a lexer has to be selected with -lt   flushs	   
   (f%s)iþÿÿÿs   
s   *** Error while highlighting:(   s   lexers	   formatters   filter(   i   (   i   (   s   win32s   cygwin(   RV   RW   (   i   (   i   (A   t   USAGEt   getoptt   GetoptErrorR5   R9   R:   R*   t   popt   NoneR   t   mainRM   t   rstript   lenR=   R(   R   R   R    t   getR   R   R   R   RC   R   t   get_style_defst	   ExceptionR0   R   t   opent   readR   R   t   version_infot   stdint   bufferR   R   R   t   stdoutRP   R	   t   platformR6   t   iot   TextIOWrappert   colorama.initialiset   ImportErrort
   initialiset   wrap_streamt   Falset
   isinstanceR   R   t
   add_filterR   t   readlinet   hasattrR\   t   KeyboardInterruptt	   tracebackt   format_exceptiont   exc_infoR   ((   t   argst   usaget   poptsR"   t   O_optst   P_optst   F_optst   optt   argt   L_optt   H_optR;   R6   t   parsed_optst   p_optt   valueRO   RQ   t   infnR1   t   errt   S_optt   a_optt   f_optt   fmtert   codet   infpt   outfnt   outfileRo   t   coloramaR[   t   leftt   rightR.   R/   t   lineR{   RE   t   msg(    (    s4   /usr/lib/python2.7/dist-packages/pygments/cmdline.pyRb   É   sÆ    

	

		$!

	
	,(.   R7   t
   __future__R    R9   R^   t   textwrapR   t   pygmentsR   R   t   pygments.utilR   R   R   R   R   R	   t   pygments.lexersR
   R   R   R   R   R   t   pygments.formatters.latexR   R   t   pygments.formattersR   R   R   R   R   t   pygments.filtersR   R   t   pygments.stylesR   R   R]   R(   R0   R=   RM   t   argvRb   (    (    (    s4   /usr/lib/python2.7/dist-packages/pygments/cmdline.pyt   <module>
   s"   ..(C				3