Format: application/x-python-code
1 o
2
3 ��hY
4 � @ s� d Z ddlZddlmZmZ ddlmZ ddlZddlmZm Z e�
5 dej�Ze�
6 dej�Z
7 dd
8 d�Zdd
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.]+)�replaceTc C s� t | t�r| S t | t�rt| �}n| }z t|j�\}}W n ty' d}Y nw |�d� t|||dd��}d|_ |rId�
16 t|��W d � S |�� W d � S 1 sWw Y dS )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 �asciir T)�errors�line_buffering�r� N)
23 �
24 isinstance�str�bytesr r �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_unicode s$
25
26
27 �
28 �$�r c c sh � t | �}zt|�}t�|�s|V t|�}t�|�s|V W n
29 ty) Y dS w |D ]}|V q,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�liner r r r + s �
33
34 ���r c C s\ t | �}t|��}|rd�t|��W d � S |�� W d � S 1 s'w Y dS )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)r r r r r )�filenamer �filepath�fr r r �read_py_file= s
48 �$�r, c C s. ddl m} || �}t�|�� �}t|||�S )aJ Read 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- �ior r r )�urlr r r- �responser r r r �read_py_urlS s r2 )r T)T)�__doc__r/ r r �pathlibr �re�tokenizer r �compile�UNICODE� cookie_rer"