a
    lcY                     @   s   d Z ddlZddlmZmZ ddlmZ ddlZddlmZm	Z	 e
dejZe
dejZdd
dZdd ZdddZdddZdS )z
Tools to open .py files as Unicode, using the encoding specified within the file,
as per PEP 263.

Much of the code is taken from the tokenize module in Python 3.2.
    N)TextIOWrapperBytesIO)Path)opendetect_encodingzcoding[:=]\s*([-\w.]+)z^\s*#.*coding[:=]\s*([-\w.]+)replaceTc                 C   s   t | tr| S t | tr"t| }n| }zt|j\}}W n tyN   d}Y n0 |d t|||ddL}d|_	|rd
t|W  d   S | W  d   S W d   n1 s0    Y  dS )a#  Converts a bytes string with python source code to unicode.

    Unicode strings are passed through unchanged. Byte strings are checked
    for the python source file encoding cookie to determine encoding.
    txt can be either a bytes buffer or a string containing the source
    code.
    asciir   T)errorsline_bufferingr N)
isinstancestrbytesr   r   readlineSyntaxErrorseekr   modejoinstrip_encoding_cookieread)txtr	   skip_encoding_cookiebufferencoding_text r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/IPython/utils/openpy.pysource_to_unicode   s    




r   c                 c   sf   t | }z4t|}t|s"|V  t|}t|s:|V  W n tyP   Y dS 0 |D ]
}|V  qVdS )z{Generator to pull lines from a text-mode file, skipping the encoding
    cookie if it is found in the first two lines.
    N)iternextcookie_comment_rematchStopIteration)filelikeitfirstsecondliner   r   r   r   +   s    


r   c                 C   sj   t | }t|F}|r2dt|W  d   S | W  d   S W d   n1 s\0    Y  dS )a  Read a Python file, using the encoding declared inside the file.

    Parameters
    ----------
    filename : str
        The path to the file to read.
    skip_encoding_cookie : bool
        If True (the default), and the encoding declaration is found in the first
        two lines, that line will be excluded from the output.

    Returns
    -------
    A unicode string containing the contents of the file.
    r   N)r   r   r   r   r   )filenamer   filepathfr   r   r   read_py_file=   s
    
r-   c                 C   s.   ddl m} || }t| }t|||S )aJ  Read a Python file from a URL, using the encoding declared inside the file.

    Parameters
    ----------
    url : str
        The URL from which to fetch the file.
    errors : str
        How to handle decoding errors in the file. Options are the same as for
        bytes.decode(), but here 'replace' is the default.
    skip_encoding_cookie : bool
        If True (the default), and the encoding declaration is found in the first
        two lines, that line will be excluded from the output.

    Returns
    -------
    A unicode string containing the contents of the file.
    r   )urlopen)urllib.requestr.   ior   r   r   )urlr	   r   r.   responser   r   r   r   read_py_urlS   s    r3   )r   T)T)r   T)__doc__r0   r   r   pathlibr   retokenizer   r   compileUNICODE	cookie_rer"   r   r   r-   r3   r   r   r   r   <module>   s   

