ó
ś%^Tc           @   s!  d  Z  d d l Z d d l Z e j d  Z e j d  Z e j d  Z e j d e j  Z d e	 f d     YZ
 d	 e f d
     YZ d e d  Z d d  Z d d  Z d d  Z d   Z d   Z d   Z d   Z d   Z i  Z d   Z d   Z d   Z e d d  Z d$ d  Z d e f d     YZ d   Z  d   Z! d   Z" e j# d% k  rËe$ Z$ e% Z% e& e' f Z( e' Z) d  Z* e+ j, Z, e+ j- Z- d d l. Z. d d l/ Z/ e. j. Z. e/ j. Z0 nI e1 Z$ e2 Z% e& f Z( e& Z) d! Z* e+ j3 Z, e+ j4 Z- d d" l5 m. Z. m0 Z0 d#   Z6 d S(&   są   
    pygments.util
    ~~~~~~~~~~~~~

    Utility functions.

    :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
i˙˙˙˙Ns   [/\\ ]sŽ   (?smx)
    (<\?.*?\?>)?\s*
    <!DOCTYPE\s+(
     [a-zA-Z_][a-zA-Z0-9]*
     (?: \s+      # optional in HTML5
     [a-zA-Z_][a-zA-Z0-9]*\s+
     "[^"]*")?
     )
     [^>]*>
s   <(.+?)(\s.*?)?>.*?</.+?>(?uism)s   \s*<\?xml[^>]*\?>t   ClassNotFoundc           B   s   e  Z d  Z RS(   sC   Raised if one of the lookup functions didn't find a matching class.(   t   __name__t
   __module__t   __doc__(    (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyR       s   t   OptionErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyR   #   s   c         C   sb   |  j  | |  } | r' | j   } n  | | k r^ t d | d j t t |   f   n  | S(   Ns%   Value for option %s must be one of %ss   , (   t   gett   lowerR   t   joint   mapt   str(   t   optionst   optnamet   allowedt   defaultt   normcaset   string(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   get_choice_opt'   s    %c         C   sŹ   |  j  | |  } t | t  r% | St | t  r> t |  St | t  sf t d | | f   nB | j   d k r| t S| j   d k r t St d
 | | f   d  S(   NsB   Invalid type %r for option %s; use 1/0, yes/no, true/false, on/offt   1t   yest   truet   ont   0t   not   falset   offsC   Invalid value %r for option %s; use 1/0, yes/no, true/false, on/off(   R   s   yesR   R   (   R   s   noR   R   (	   R   t
   isinstancet   boolt   intt   string_typesR   R   t   Truet   False(   R
   R   R   R   (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   get_bool_opt1   s    
c         C   st   |  j  | |  } y t |  SWnM t k
 rI t d | | f   n' t k
 ro t d | | f   n Xd  S(   Ns=   Invalid type %r for option %s; you must give an integer values>   Invalid value %r for option %s; you must give an integer value(   R   R   t	   TypeErrorR   t
   ValueError(   R
   R   R   R   (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   get_int_optE   s    c         C   sd   |  j  | |  } t | t  r+ | j   St | t t f  rJ t |  St d | | f   d  S(   Ns9   Invalid type %r for option %s; you must give a list value(   R   R   R   t   splitt   listt   tupleR   (   R
   R   R   t   val(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   get_list_optS   s    

c         C   sm   |  j  s d Sg  } xD |  j  j   j   D]- } | j   rU | j d | j    q) Pq) Wd j |  j   S(   Nt    t    (   R   t   stript
   splitlinest   appendR   t   lstrip(   t   objt   rest   line(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   docstring_headline_   s    	c            s%     f d   }   j  | _  t |  S(   sA   Return a static text analyser function that returns float values.c            sn   y   |   } Wn t  k
 r$ d SX| s/ d Sy  t d t d t |    SWn t t f k
 ri d SXd  S(   Ng        g      đ?(   t	   Exceptiont   mint   maxt   floatR!   R    (   t   textt   rv(   t   f(    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   text_analysem   s     (   R   t   staticmethod(   R8   R9   (    (   R8   s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   make_analysatork   s    c         C   sá   |  j  d  } | d k r. |  |  j   } n |  j   } | j d  rÝ yJ g  t j | d j    D]" } | rf | j d  rf | ^ qf d } Wn t k
 r§ t SXt j	 d | t j
  } | j |  d k	 rÝ t Sn  t S(	   sđ  Check if the given regular expression matches the last part of the
    shebang if one exists.

        >>> from pygments.util import shebang_matches
        >>> shebang_matches('#!/usr/bin/env python', r'python(2\.\d)?')
        True
        >>> shebang_matches('#!/usr/bin/python2.4', r'python(2\.\d)?')
        True
        >>> shebang_matches('#!/usr/bin/python-ruby', r'python(2\.\d)?')
        False
        >>> shebang_matches('#!/usr/bin/python/ruby', r'python(2\.\d)?')
        False
        >>> shebang_matches('#!/usr/bin/startsomethingwith python',
        ...                 r'python(2\.\d)?')
        True

    It also checks for common windows executable file extensions::

        >>> shebang_matches('#!C:\Python2.4\Python.exe', r'python(2\.\d)?')
        True

    Parameters (``'-f'`` or ``'--foo'`` are ignored so ``'perl'`` does
    the same as ``'perl -e'``)

    Note that this method automatically searches the whole string (eg:
    the regular expression is wrapped in ``'^$'``)
    s   
i    s   #!i   t   -i˙˙˙˙s   ^%s(\.(exe|cmd|bat|bin))?$N(   t   findR   t
   startswitht   split_path_reR#   R*   t
   IndexErrorR   t   ret   compilet
   IGNORECASEt   searcht   NoneR   (   R6   t   regext   indext
   first_linet   xt   found(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   shebang_matches|   s     *c         C   sV   t  j |   } | d k r t S| j d  } t j | t j  j | j    d k	 S(   sÁ   Check if the doctype matches a regular expression (if present).

    Note that this method only checks the first part of a DOCTYPE.
    eg: 'html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
    i   N(	   t   doctype_lookup_ret   matchRE   R   t   groupRA   RB   t   IR*   (   R6   RF   t   mt   doctype(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   doctype_matchesŠ   s
    c         C   s   t  |  d  S(   s3   Check if the file looks like it has a html doctype.t   html(   RR   (   R6   (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   html_doctype_matchesś   s    c         C   s   t  j |   r t St |   } y t | SWnT t k
 r t j |   } | d k	 rZ t St j	 |  d   d k	 } | t | <| SXd S(   s2   Check if a doctype exists or if we have some tags.ič  N(
   t   xml_decl_reRM   R   t   hasht   _looks_like_xml_cachet   KeyErrorRL   RE   t   tag_reRD   (   R6   t   keyRP   R7   (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   looks_like_xmlź   s    
c         C   s   d |  d ?d |  d @f S(   NiŔ×  i
   i Ü  i˙  (    (   t   c(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   _surrogatepairÍ   s    c         C   sŠ  | |  k  r t  d   n  |  d k  s3 | d k  rB t  d   n  t j d k rk d t |   t |  f St |   \ } } t |  \ } } | | k rž d t |  t |  t |  f Sg  } | j d t |  t |  | | k r÷ t |  p t d  f  | | d	 k rU| j d
 t | d	  t | d	  t d  t d   n  | | k r| j d t |  t d  t |  f  n  d d j |  d Sd S(   sE   Returns a regular expression string to match the given non-BMP range.s   Bad character rangei   s+   unirange is only defined for non-BMP rangesi˙˙  u   [%s-%s]u   (?:%s[%s-%s])u	   %s[%s-%s]i˙ß  i   u   [%s-%s][%s-%s]i Ü  u   (?:u   |u   )N(   R!   t   syst
   maxunicodet   unichrR]   R,   R   (   t   at   bt   aht   alt   bht   blt   buf(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   unirangeŐ   s*    #	)	4	&i    c   	      C   sĘ   g  } d | d } d | d d } | j  | |  d  | rj xh | D] } | j  | | d  qH WnB x? | D]7 } t | d  } | j  | | d  | d d  qq W| j  | d	  d
 j |  S(   s)   Formats a sequence of strings for output.R)   i   i   s    = (t   ,t   "iţ˙˙˙i˙˙˙˙t   )s   
(   R,   t   reprR   (	   t   var_namet   seqt   rawt   indent_levelt   linest   base_indentt   inner_indentt   it   r(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   format_linesü   s    %c         C   s\   g  } t    } xF |  D]> } | | k s | | k r: q n  | j |  | j |  q W| S(   sa   
    Returns a list with duplicates removed from the iterable `it`.

    Order is preserved.
    (   t   setR,   t   add(   t   itt   already_seent   lstt   seenRt   (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   duplicates_removed  s    	t   Futurec           B   s   e  Z d  Z d   Z RS(   s   Generic class to defer some work.

    Handled specially in RegexLexerMeta, to support regex string construction at
    first use.
    c         C   s
   t   d  S(   N(   t   NotImplementedError(   t   self(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyR   %  s    (   R   R   R   R   (    (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyR~     s   c         C   s   y |  j  d  }  |  d f SWns t k
 r y2 d d l } | j   } |  j    }  |  | f SWq t t f k
 r |  j  d  }  |  d f SXn Xd S(   sĂ   Decode *text* with guessed encoding.

    First try UTF-8; this should fail for non-UTF-8 encodings.
    Then try the preferred locale encoding.
    Fall back to latin-1, which always works.
    s   utf-8i˙˙˙˙Nt   latin1(   t   decodet   UnicodeDecodeErrort   localet   getpreferredencodingt   LookupError(   R6   R   t   prefencoding(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   guess_decode)  s    c         C   sV   t  | d d  rL y |  j | j  }  Wn t k
 r; qL X|  | j f Sn  t |   S(   sĘ   Decode *text* coming from terminal *term*.

    First try the terminal encoding, if given.
    Then try UTF-8.  Then try the preferred locale encoding.
    Fall back to latin-1, which always works.
    t   encodingN(   t   getattrRE   R   R   R   R   (   R6   t   term(    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   guess_decode_from_terminal>  s    c         C   s/   t  |  d d  r |  j Sd d l } | j   S(   s7   Return our best guess of encoding for the given *term*.R   i˙˙˙˙N(   R   RE   R   R   R   (   R   R   (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   terminal_encodingO  s    i   t   uR(   (   t   StringIOt   BytesIOc            s     f d   } | S(   s6   Class decorator for creating a class with a metaclass.c            so   |  j  j   } | j d d   | j d d   x' | j d d  D] } | j |  qB W  |  j |  j |  S(   Nt   __dict__t   __weakref__t	   __slots__(    (   R   t   copyt   popRE   R   R   t	   __bases__(   t   clst	   orig_varst	   slots_var(   t	   metaclass(    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   wrapperr  s    (    (   R   R   (    (   R   s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   add_metaclassp  s    (    (   i   i    (7   R   RA   R^   RB   R?   RL   RY   RO   RU   R!   R    R2   R   RE   R   R   R   R"   R'   R1   R;   RK   RR   RT   RW   R[   R]   Rh   Rv   R}   t   objectR~   R   R   R   t   version_infoR`   t   xrangeR	   t   unicodeR   t	   text_typet   u_prefixt   dictt	   iteritemst
   itervaluesR   t	   cStringIOR   t   chrt   ranget   itemst   valuest   ioR   (    (    (    s1   /usr/lib/python2.7/dist-packages/pygments/util.pyt   <module>
   s`   		
			-					'
			
						