B
    \                 @   sz   d Z ddlZddlmZ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)opendetect_encodingzcoding[:=]\s*([-\w.]+)z^\s*#.*coding[:=]\s*([-\w.]+)replaceTc             C   s   t | tr| S t | tr"t| }n| }yt|j\}}W n tk
rP   d}Y nX |d t|||dd}d|_	|rd
t|S | S 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)Ztxtr   skip_encoding_cookiebufferencoding_text r   3lib/python3.7/site-packages/IPython/utils/openpy.pysource_to_unicode   s    




r   c             c   sj   t | }y4t|}t|s"|V  t|}t|s:|V  W n tk
rP   dS X x|D ]
}|V  qXW d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)Zfilelikeitfirstsecondliner   r   r   r   *   s    



r   c          	   C   s2   t |  }|rdt|S | S W dQ R X 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 - compiling a
      unicode string with an encoding declaration is a SyntaxError in Python 2.
    
    Returns
    -------
    A unicode string containing the contents of the file.
    r   N)r   r   r   r   )filenamer   fr   r   r   read_py_file<   s    
r)   c             C   s.   ddl m} || }t| }t|||S )a  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 - compiling a
      unicode string with an encoding declaration is a SyntaxError in Python 2.
    
    Returns
    -------
    A unicode string containing the contents of the file.
    r   )urlopen)Zurllib.requestr*   ior   r   r   )Zurlr   r   r*   Zresponser   r   r   r   read_py_urlR   s    r,   )r   T)T)r   T)__doc__r+   r   r   retokenizer   r   compileUNICODE	cookie_rer    r   r   r)   r,   r   r   r   r   <module>   s   

