a
    W_#                     @   sD  d Z ddlZddlZddlmZ edZedejejB ej	B Z
edejejB ejB ejB ZedejZG dd	 d	eZG d
d deZd2ddZd3ddZd4ddZd5ddZdd Zdd Zdd Zdd Zdd Zi Zdd  Zd!d" Zd6d#d$Z d7d&d'Z!G d(d) d)Z"d*d+ Z#d,d- Z$d.d/ Z%G d0d1 d1eZ&dS )8z
    pygments.util
    ~~~~~~~~~~~~~

    Utility functions.

    :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
    N)TextIOWrapperz[/\\ ]z
    (<\?.*?\?>)?\s*
    <!DOCTYPE\s+(
     [a-zA-Z_][a-zA-Z0-9]*
     (?: \s+      # optional in HTML5
     [a-zA-Z_][a-zA-Z0-9]*\s+
     "[^"]*")?
     )
     [^>]*>
z<(.+?)(\s.*?)?>.*?</.+?>z\s*<\?xml[^>]*\?>c                   @   s   e Zd ZdZdS )ClassNotFoundzCRaised if one of the lookup functions didn't find a matching class.N)__name__
__module____qualname____doc__ r   r   //usr/lib/python3/dist-packages/pygments/util.pyr   !   s   r   c                   @   s   e Zd ZdS )OptionErrorN)r   r   r   r   r   r   r	   r
   %   s   r
   Fc                 C   s@   |  ||}|r| }||vr<td|dtt|f |S )Nz%Value for option %s must be one of %sz, )getlowerr
   joinmapstr)optionsoptnameZalloweddefaultnormcasestringr   r   r	   get_choice_opt)   s    r   c                 C   s|   |  ||}t|tr|S t|tr,t|S t|tsHtd||f n0| dv rXdS | dv rhdS td||f d S )NzBInvalid type %r for option %s; use 1/0, yes/no, true/false, on/off)1ZyestrueZonT)0ZnoZfalseZoffFzCInvalid value %r for option %s; use 1/0, yes/no, true/false, on/off)r   
isinstanceboolintr   r
   r   r   r   r   r   r   r   r	   get_bool_opt3   s$    


r   c                 C   s^   |  ||}z
t|W S  ty8   td||f Y n" tyX   td||f Y n0 d S )Nz=Invalid type %r for option %s; you must give an integer valuez>Invalid value %r for option %s; you must give an integer value)r   r   	TypeErrorr
   
ValueErrorr   r   r   r	   get_int_optG   s    

r    c                 C   sH   |  ||}t|tr| S t|ttfr4t|S td||f d S )Nz9Invalid type %r for option %s; you must give a list value)r   r   r   splitlisttupler
   )r   r   r   valr   r   r	   get_list_optU   s    
r%   c                 C   sP   | j s
dS g }| j   D ]$}| r<|d|   q qBqd| S )N  )r   strip
splitlinesappendr   lstrip)objresliner   r   r	   docstring_headlinea   s    r/   c                    s    fdd} j |_ t|S )zAReturn a static text analyser function that returns float values.c              	      s^   z | }W n t y    Y dS 0 |s*dS ztdtdt|W S  ttfyX   Y dS 0 d S )Ng        g      ?)	Exceptionminmaxfloatr   r   )textrvfr   r	   text_analyseo   s    z%make_analysator.<locals>.text_analyse)r   staticmethod)r7   r8   r   r6   r	   make_analysatorm   s    r:   c                 C   s   |  d}|dkr$| d|  }n|  }|drz(dd t|dd  D d }W n tyr   Y d	S 0 td
| tj	}|
|durdS d	S )a  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 ``'^$'``)
    
r   Nz#!c                 S   s   g | ]}|r| d s|qS )-)
startswith).0xr   r   r	   
<listcomp>   s   z#shebang_matches.<locals>.<listcomp>   Fz^%s(\.(exe|cmd|bat|bin))?$T)findr   r=   split_path_rer!   r(   
IndexErrorrecompile
IGNORECASEsearch)r4   regexindexZ
first_linefoundr   r   r	   shebang_matches~   s    

rM   c                 C   s<   t | }|du rdS |d}t|tj| duS )zCheck 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"'
    NFrA   )doctype_lookup_rerI   grouprF   rG   Imatchr(   )r4   rJ   mZdoctyper   r   r	   doctype_matches   s
    

rS   c                 C   s
   t | dS )z3Check if the file looks like it has a html doctype.Zhtml)rS   )r4   r   r   r	   html_doctype_matches   s    rT   c                 C   sr   t | rdS t| }z
t| W S  tyl   t| }|durFY dS t| dd du}|t|< | Y S 0 dS )z2Check if a doctype exists or if we have some tags.TNi  )xml_decl_rerQ   hash_looks_like_xml_cacheKeyErrorrN   rI   tag_re)r4   keyrR   r5   r   r   r	   looks_like_xml   s    


r[   c                 C   s   d| d?  d| d@  fS )zoGiven a unicode character code with length greater than 16 bits,
    return the two 16 bit surrogate pair.
    i  
   i   i  r   )cr   r   r	   surrogatepair   s    r^   c           	      C   s   g }d| d }d|d  d }| ||  d  |rT|D ]}| || d  q:n8|D ]2}t|d }| ||dd  |d	  d  qX| |d
  d|S )z)Formats a sequence of strings for output.r'         z = (,"NrB   )r;   )r*   reprr   )	Zvar_nameseqrawZindent_levellinesZbase_indentZinner_indentirr   r   r	   format_lines   s    $rk   r   c                 C   s>   g }t  }| D ]*}||v s||v r$q|| || q|S )za
    Returns a list with duplicates removed from the iterable `it`.

    Order is preserved.
    )setr*   add)itZalready_seenZlstseenri   r   r   r	   duplicates_removed   s    
rp   c                   @   s   e Zd ZdZdd ZdS )FuturezGeneric class to defer some work.

    Handled specially in RegexLexerMeta, to support regex string construction at
    first use.
    c                 C   s   t d S N)NotImplementedErrorselfr   r   r	   r     s    z
Future.getN)r   r   r   r   r   r   r   r   r	   rq      s   rq   c                 C   s   z|  d} | dfW S  tyz   z&ddl}| }|   } | |fW  Y S  ttfyt   |  d} | df Y  Y S 0 Y n0 dS )zDecode *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.
    zutf-8r   Nlatin1)decodeUnicodeDecodeErrorlocalegetpreferredencodingLookupError)r4   ry   Zprefencodingr   r   r	   guess_decode  s    


r|   c                 C   sB   t |ddr:z| |j} W n ty.   Y n0 | |jfS t| S )zDecode *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.
    encodingN)getattrrw   r}   rx   r|   )r4   termr   r   r	   guess_decode_from_terminal  s    
r   c                 C   s"   t | ddr| jS ddl}| S )z7Return our best guess of encoding for the given *term*.r}   Nr   )r~   r}   ry   rz   )r   ry   r   r   r	   terminal_encoding,  s    r   c                   @   s   e Zd Zdd ZdS )UnclosingTextIOWrapperc                 C   s   |    d S rr   )flushrt   r   r   r	   close6  s    zUnclosingTextIOWrapper.closeN)r   r   r   r   r   r   r   r	   r   4  s   r   )NF)N)N)N)Fr   )r   )'r   rF   sysior   rG   rD   DOTALL	MULTILINEVERBOSErN   UNICODErH   rY   rP   rU   r   r   r0   r
   r   r   r    r%   r/   r:   rM   rS   rT   rW   r[   r^   rk   rp   rq   r|   r   r   r   r   r   r   r	   <module>   s@   

	





-	


