Format: application/x-python-code
⬇ Download File (3.4 KB)
   1  o
   2  
   3  ��hY
   4  �@s�dZddlZddlmZmZddlmZddlZddlmZm	Z	e�
   5  dej�Ze�
   6  dej�Z
   7  dd
   8  d�Zdd
   9  �Zddd�Zddd�ZdS)z�
  10  Tools to open .py files as Unicode, using the encoding specified within the file,
  11  as per PEP 263.
  12  
  13  Much of the code is taken from the tokenize module in Python 3.2.
  14  �N)�
  15  TextIOWrapper�BytesIO)�Path)�open�detect_encodingzcoding[:=]\s*([-\w.]+)z^\s*#.*coding[:=]\s*([-\w.]+)�replaceTcCs�t|t�r|St|t�rt|�}n|}z	t|j�\}}Wnty'd}Ynw|�d�t|||dd��}d|_	|rId�
  16  t|��Wd�S|��Wd�S1sWwYdS)a#Converts a bytes string with python source code to unicode.
  17  
  18      Unicode strings are passed through unchanged. Byte strings are checked
  19      for the python source file encoding cookie to determine encoding.
  20      txt can be either a bytes buffer or a string containing the source
  21      code.
  22      �asciirT)�errors�line_buffering�r�N)
  23  �
  24  isinstance�str�bytesrr�readline�SyntaxError�seekr�mode�join�strip_encoding_cookie�read)�txtr	�skip_encoding_cookie�buffer�encoding�_�text�r�Jc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/utils/openpy.py�source_to_unicodes$
  25  
  26  
  27  �
  28  �$�rccsh�t|�}zt|�}t�|�s|Vt|�}t�|�s|VWn
  29  ty)YdSw|D]}|Vq,dS)z{Generator to pull lines from a text-mode file, skipping the encoding
  30      cookie if it is found in the first two lines.
  31      N)�iter�next�cookie_comment_re�match�
  32  StopIteration)Zfilelike�it�first�second�linerrrr+s �
  33  
  34  ���rcCs\t|�}t|��}|rd�t|��Wd�S|��Wd�S1s'wYdS)a�Read a Python file, using the encoding declared inside the file.
  35  
  36      Parameters
  37      ----------
  38      filename : str
  39          The path to the file to read.
  40      skip_encoding_cookie : bool
  41          If True (the default), and the encoding declaration is found in the first
  42          two lines, that line will be excluded from the output.
  43  
  44      Returns
  45      -------
  46      A unicode string containing the contents of the file.
  47      rN)rrrrr)�filenamer�filepath�frrr�read_py_file=s
  48  �$�r,cCs.ddlm}||�}t�|���}t|||�S)aJRead a Python file from a URL, using the encoding declared inside the file.
  49  
  50      Parameters
  51      ----------
  52      url : str
  53          The URL from which to fetch the file.
  54      errors : str
  55          How to handle decoding errors in the file. Options are the same as for
  56          bytes.decode(), but here 'replace' is the default.
  57      skip_encoding_cookie : bool
  58          If True (the default), and the encoding declaration is found in the first
  59          two lines, that line will be excluded from the output.
  60  
  61      Returns
  62      -------
  63      A unicode string containing the contents of the file.
  64      r)�urlopen)�urllib.requestr-�iorrr)�urlr	rr-�responserrrr�read_py_urlSsr2)rT)T)�__doc__r/rr�pathlibr�re�tokenizerr�compile�UNICODE�	cookie_rer"rrr,r2rrrr�<module>s
  65  
  66