B
    nôb\“!  ã               @   s(  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mZmZmZmZ ddlmZ e ¡ Zeefdd„Zd	d
„ Zeƒ Zdd„ Zdd„ Ze d¡Zddddddddddddddddd d!d"d#gZd8d%d&„Zd'd(„ Zd)d*„ Zd+d,„ Zd9d.d/„Z d:d0d1„Z!d;d2d3„Z"d<d4d5„Z#d6d7„ 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)Ú	is_stringÚto_text_stringÚis_binary_stringÚ
is_unicode)Ú	is_binaryc             C   sP   y|   d¡ d¡S  tk
rJ   y|   d¡ |¡S  tk
rD   | S X Y nX dS )zTranscode a text stringÚcp437Úcp1252N)ÚdecodeÚencodeÚUnicodeError)ÚtextÚinputÚoutput© r   ú4lib/python3.7/site-packages/spyder/utils/encoding.pyÚ	transcode    s    r   c              C   s   t  ¡ } | dk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   1   s    r   c          	   C   sP   t | ƒst|  ¡ dƒ} n4t| ƒrLy|  t¡}W n ttfk
rF   Y nX |S | S )zT
    Return a unicode version of string decoded using the file system encoding.
    zutf-8)r   r   ZtoUtf8r   r   ÚFS_ENCODINGr   Ú	TypeError)ÚstringÚunicr   r   r   Úto_unicode_from_fs>   s    r   c          	   C   s8   t | ƒr4y|  t¡}W n ttfk
r.   Y nX |S | S )z[
    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_unicodeN   s    r   zcoding[:=]\s*([-\w_.]+)úutf-8z	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-14zlatin-1zutf-16Fc          	   C   s°   |s`xZ|   ¡ dd… D ]F}yt t|ƒ¡}W n tk
r@   Y qX |r| d¡}|tkr|S qW t| ƒr¬tƒ }x*|   ¡ dd… D ]}| 	|¡ |j
r€P q€W | ¡  |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Úliner,   ÚcodecZdetectorr   r   r   Ú
get_codingj   s&    

 
r/   c          	   C   sÚ   yŠ|   t¡r&t| ttƒd… dƒdfS |   t¡rJt| ttƒd… dƒdfS |   t¡rnt| ttƒd… dƒdfS t| ƒ}|rˆt| |ƒ|fS W n ttfk
r¢   Y nX yt| dƒdfS  ttfk
rÊ   Y nX t| dƒdfS )	zr
    Function to decode a text.
    @param text text to decode (string)
    @return decoded text and encoding
    Nzutf-8z	utf-8-bomzutf-16zutf-32zutf-8-guessedzlatin-1zlatin-1-guessed)	Ú
startswithr   r   Úlenr   r   r/   r   ÚLookupError)r   Úcodingr   r   r   r      s"    


r   c          	   C   s  |dkrt |  d¡ dfS |rFy|  |¡|fS  ttfk
rD   Y nX t| ƒ}|r†y|  |¡|fS  ttfk
r„   td| ƒ‚Y nX |r”| d¡sž| d¡rÞ| dd¡}| dd¡}y|  |¡|fS  ttfk
rÜ   Y nX y|  d¡dfS  tk
r   Y nX |  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
    z	utf-8-bomzutf-8zIncorrect encoding (%s)z-defaultz-guessedÚ r!   )r   r   r   r2   r/   ÚRuntimeErrorÚendswithÚreplace)r   Zorig_codingr3   r   r   r   r   ª   s4    
r   c          	   C   sV   t | ƒsRxHtD ]@}yt| |ƒ}W n( tk
r4   Y q tk
rH   P Y qX |S qW | S )zConvert a string to unicode)r	   r*   r   r   r   )r   r.   r   r   r   r   Ú
to_unicodeÔ   s    
r8   Úwbc          	   C   s2   t | |ƒ\} }t||ƒ}| | ¡ W dQ R X |S )zd
    Write 'text' to file ('filename') assuming 'encoding'
    Return (eventually new) encoding
    N)r   ÚopenÚwrite)r   Úfilenamer   ÚmodeZtextfiler   r   r   r;   ã   s    r;   c             C   s   t tj | ¡|||ƒS )ze
    Write 'lines' to file ('filename') assuming 'encoding'
    Return (eventually new) encoding
    )r;   ÚosÚlinesepÚjoin)Úlinesr<   r   r=   r   r   r   Ú
writelinesí   s    rB   c             C   s   t t| dƒ ¡ ƒ\}}||fS )zG
    Read text from file ('filename')
    Return text and encoding
    Úrb)r   r:   Úread)r<   r   r   r   r   r   rD   ô   s    rD   c             C   s   t | |ƒ\}}| tj¡|fS )zI
    Read lines from file ('filename')
    Return lines and encoding
    )rD   Úsplitr>   r?   )r<   r   r   r   r   r   Ú	readlinesü   s    rF   c          	   C   s(   y
t | ƒ S  ttfk
r"   dS X dS )z5
    Test if the given path is a text-like file.
    FN)r
   ÚOSErrorÚIOError)r<   r   r   r   Úis_text_file  s    
rI   )F)r    r9   )r    r9   )r    )r    )%Ú__doc__Úcodecsr   r   r   ZlocaleÚrer>   r   Zchardet.universaldetectorr   Zspyder.py3compatr   r   r   r	   Z'spyder.utils.external.binaryornot.checkr
   Úgetpreferredencodingr   r   r   r   r   r   Úcompiler&   r*   r/   r   r   r8   r;   rB   rD   rF   rI   r   r   r   r   Ú<module>   s:   


#*




	