a
    a +                     @   sN  d Z ddlmZmZmZ ddlZddlZddlZddlZddl	m
Z ddlZddlZddlZddlmZ ddlmZ ddlmZmZmZmZmZ ddlmZ erddlZnddlZe Zeefdd	Zd
d Z e  Z!dd Z"dd Z#e$dZ%g dZ&d'ddZ'dd Z(dd Z)dd Z*d(ddZ+d)dd Z,d*d!d"Z-d+d#d$Z.d%d& Z/dS ),u   
Text encoding utilities, text file I/O

Functions 'get_coding', 'decode', 'encode' and 'to_unicode' come from Eric4
source code (Utilities/__init___.py) Copyright © 2003-2009 Detlev Offenbach
    )BOM_UTF8	BOM_UTF16	BOM_UTF32N)UniversalDetector)atomic_write)	is_stringto_text_stringis_binary_string
is_unicodePY2)	is_binaryc                 C   s\   z|  ddW S  tyV   z|  d|W  Y S  tyP   |  Y  Y S 0 Y n0 dS )zTranscode a text stringcp437cp1252N)decodeencodeUnicodeError)textinputoutput r   4lib/python3.9/site-packages/spyder/utils/encoding.py	transcode*   s    r   c                  C   s   t  } | du rt} | S )zg
    Query the filesystem for the encoding used to encode filenames
    and environment variables.
    N)sysgetfilesystemencodingPREFERRED_ENCODING)encodingr   r   r   r   ;   s    r   c              	   C   sN   t | st|  d} n2t| rJz| t}W n ttfyD   Y n0 |S | S )zT
    Return a unicode version of string decoded using the file system encoding.
    utf-8)r   r   ZtoUtf8r	   r   FS_ENCODINGr   	TypeError)stringunicr   r   r   to_unicode_from_fsH   s    r!   c              	   C   s6   t | r2z| t}W n ttfy,   Y n0 |S | S )zZ
    Return a byte string version of unic encoded using the file
    system encoding.
    )r
   r   r   r   r   )r    r   r   r   r   to_fs_from_unicodeX   s    r"   zcoding[:=]\s*([-\w_.]+))r   z	iso8859-1z
iso8859-15asciizkoi8-rcp1251zkoi8-uz	iso8859-2z	iso8859-3z	iso8859-4z	iso8859-5z	iso8859-6z	iso8859-7z	iso8859-8z	iso8859-9z
iso8859-10z
iso8859-13z
iso8859-14latin-1utf-16Fc              	   C   s   |s^|   dd D ]H}ztt|}W n ty<   Y q0 |r|d}|tv r|  S qt| rt }|   dd D ]}|	| |j
r| qq||  |jd S dS )zr
    Function to get the coding of a text.
    @param text text to inspect (string)
    @return coding string
    N      r   )
splitlines	CODING_REsearchr   UnicodeDecodeErrorgroupCODECSr	   r   ZfeedZdonecloseresult)r   Zforce_chardetliner0   codecZdetectorr   r   r   
get_codingt   s$    



r3   c              	   C   s   z|  tr(t| ttd ddfW S |  trNt| ttd ddfW S |  trtt| ttd ddfW S t| }|rt| ||fW S W n ttfy   Y n0 zt| ddfW S  ttfy   Y n0 t| ddfS )	zr
    Function to decode a text.
    @param text text to decode (string)
    @return decoded text and encoding
    Nr   	utf-8-bomr&   zutf-32zutf-8-guessedr%   zlatin-1-guessed)	
startswithr   r   lenr   r   r3   r   LookupError)r   codingr   r   r   r      s"    


r   c              	   C   s   |dkrt | d dfS |rFz| ||fW S  ttfyD   Y n0 t| }|rz| ||fW S  ttfy   td| Y n0 |r|ds|dr|dd}|dd}z| ||fW S  ttfy   Y n0 | ddfS )z
    Function to encode a text.
    @param text text to encode (string)
    @param orig_coding type of the original coding (string)
    @return encoded text and encoding
    r4   r   zIncorrect encoding (%s)z-defaultz-guessed )r   r   r   r7   r3   RuntimeErrorendswithreplace)r   Zorig_codingr8   r   r   r   r      s.    r   c              	   C   sV   t | sRtD ]D}zt| |}W n( ty0   Y q tyF   Y  qRY q0 |  S q| S )zConvert a string to unicode)r
   r.   r   r   r   )r   r2   r    r   r   r   
to_unicode   s    
r=   r   wbc                 C   s  t | |\} }tjdkrjz.t| }| r:t|}n
t	|}W qt t
tfyf   t	|}Y qt0 n
t	|}d|v rt||}||  W d   n1 s0    Y  n^zt|}|j}|j}	W n: t
y   td}
t|
 d|
 @ }t }	Y n0 zZd}d| v r&t }t|d||d}||  W d   n1 sX0    Y  W nj t
y } zP|jtjkrt||}||  W d   n1 s0    Y  W Y d}~n
d}~0 0 z,t|| t|}t||	|jf W n t
y   Y n0 |S )	zu
    Write 'text' to file ('filename') assuming 'encoding' in an atomic way
    Return (eventually new) encoding
    ntaNr   i  ZdropboxT)Z	overwritemodedir)r   osnamepathlibPathZresolveexistsr   osprealpathOSErrorr:   openwritestatst_modest_atimeumasktimelowertempfileZ
gettempdirr   errnoZEINVALchmodutimest_mtime)r   filenamer   rA   Zabsolute_path_filenameZabsolute_filenameZtextfileZ	file_statZoriginal_modeZcreationrP   Z
tempfoldererrorr   r   r   rL      sT    


,




.@
rL   c                 C   s   t tj| |||S )ze
    Write 'lines' to file ('filename') assuming 'encoding'
    Return (eventually new) encoding
    )rL   rC   linesepjoin)linesrX   r   rA   r   r   r   
writelines(  s    r]   c                 C   s   t t| d \}}||fS )zG
    Read text from file ('filename')
    Return text and encoding
    rb)r   rK   readrX   r   r   r   r   r   r_   /  s    r_   c                 C   s   t | |\}}|tj|fS )zI
    Read lines from file ('filename')
    Return lines and encoding
    )r_   splitrC   rZ   r`   r   r   r   	readlines7  s    rb   c              	   C   s*   zt |  W S  ttfy$   Y dS 0 dS )z5
    Test if the given path is a text-like file.
    FN)r   rJ   IOError)rX   r   r   r   is_text_file@  s    rd   )F)r   r>   )r   r>   )r   )r   )0__doc__codecsr   r   r   rS   ZlocalererC   os.pathpathrH   r   rQ   rT   Zchardet.universaldetectorr   Zatomicwritesr   Zspyder.py3compatr   r   r	   r
   r   Z'spyder.utils.external.binaryornot.checkr   Zpathlib2rE   getpreferredencodingr   r   r   r   r!   r"   compiler*   r.   r3   r   r   r=   rL   r]   r_   rb   rd   r   r   r   r   <module>   sB   


#$
A


	