
    Je)                     ^   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ZddlmZ ddlmZ ddlmZmZmZmZ ddlmZ  ej6                         ZeefdZd	 Z e       Zd
 Z d Z! ejD                  d      Z#g dZ$ddZ%d Z&d Z'd Z(ddZ)ddZ*ddZ+ddZ,d Z-y)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_text_string)	is_binaryc                     	 | j                  d      j                  d      S # t        $ r7 	 | j                  d      j                  |      cY S # t        $ r | cY cY S w xY ww xY w)zTranscode a text stringcp437cp1252)decodeencodeUnicodeError)textinputoutputs      7/usr/lib/python3/dist-packages/spyder/utils/encoding.py	transcoder   &   sc    {{7#**844 	;;w'..v66 	K	s,   " 	A"AA"AA"AA"c                  >    t        j                         } | t        } | S )zg
    Query the filesystem for the encoding used to encode filenames
    and environment variables.
    )sysgetfilesystemencodingPREFERRED_ENCODING)encodings    r   r   r   7   s"    
 ((*H%O    c                     t        |       st        | j                         d      } | S t        |       r	 | j	                  t
              }|S | S # t        t        f$ r Y | S w xY w)zT
    Return a unicode version of string decoded using the file system encoding.
    utf-8)r   r	   toUtf8r
   r   FS_ENCODINGr   	TypeError)stringunics     r   to_unicode_from_fsr%   D   sn     V9 M F#}}[1 M	 !),  M	s   A A A c                 x    t        |       r	 | j                  t              }|S | S # t        t        f$ r Y | S w xY w)zZ
    Return a byte string version of unic encoded using the file
    system encoding.
    )r   r   r!   r   r"   )r$   r#   s     r   to_fs_from_unicoder'   T   sK    
 d	[[-F MK	 i( 	 K		s   & 99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-16c                    |sW| j                         dd D ]A  }	 t        j                  t        |            }|s%|j	                  d      }|t
        v s?|c S  t        |       r`t               }| j                         dd D ]!  }|j                  |       |j                  s! n |j                          |j                  d   S y# t        $ r Y w xY w)zr
    Function to get the coding of a text.
    @param text text to inspect (string)
    @return coding string
    N      r   )
splitlines	CODING_REsearchr	   groupCODECSUnicodeDecodeErrorr
   r   feeddonecloseresult)r   force_chardetliner8   codecdetectors         r   
get_codingr=   p   s     OO%bq) 	%D%")).*>? "LLOE $	%" $&OO%bq) 	$DMM$}}e	$ 	z**/ &  	s   C	CCc                    	 | j                  t              rt        | t        t              d d      dfS | j                  t              rt        | t        t              d d      dfS | j                  t
              rt        | t        t
              d d      dfS t        |       }|rt        | |      |fS 	 	 t        | d      dfS # t        t        f$ r Y  w xY w# t        t        f$ r Y nw xY w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   r=   r   LookupError)r   codings     r   r   r      s   ??8$!$s8}~"6@+MM__Y'!$s9~"7BHLL__Y'!$s9~"7BHLLD!!$/77 
dG,o==	 +& 
 +&  $	*,===s4   2C 2C (2C C 8C CCC-,C-c                 "   |dk(  rt         | j                  d      z   dfS |r	 | j                  |      |fS t	        |       }|r	 | j                  |      |fS |r|j                  d      s|j                  d      r8|j                  dd      }|j                  dd      }	 | j                  |      |fS | j                  d      dfS # t        t        f$ r Y w xY w# t        t        f$ r t        d|z        w xY w# t        t        f$ r Y Zw xY w)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
    r?   r   zIncorrect encoding (%s)z-defaultz-guessed )r   r   r   rB   r=   RuntimeErrorendswithreplace)r   orig_codingrC   s      r   r   r      s8    k!$++g..;; 	;;{+[88
 F	C;;v&.. {++J7:&$$Z4$$Z4	;;v&..
 ;;w(() k* 		 k* 	C86ABB	C k* 		s/   C C  C< CCC9<DDc                     t        |       st        D ]  }	 t        | |      }|c S  | S # t        $ r Y "t        $ r Y  | S w xY w)zConvert a string to unicode)r   r3   r	   r   r"   )r#   r;   r$   s      r   
to_unicoderK      s^    &! 	E%fe4 	 M     M	s   )	AA Ac                 2   t        | |      \  } }t        j                  dk(  rV	 t        j                  |      j                         }|j                         rt        |      }nt        j                  |      }nt        j                  |      }d|v r(t        ||      5 }|j                  |        ddd       |S 	 t        j                  |      }|j                  }|j                   }		 d}d|j)                         v rt+        j,                         }t/        |d||      5 }|j                  |        ddd       	 t        j4                  ||       t        j                  |      }t        j6                  ||	|j8                  f       |S # t        t        f$ r t        j                  |      }Y #w xY w# 1 sw Y   |S xY w# t"        $ rH t        j$                  d      }
t        j$                  |
       d|
 z  }t'        j&                         }	Y *w xY w# 1 sw Y   xY w# t        $ r[}|j0                  t0        j2                  k7  r3t        ||      5 }|j                  |        ddd       n# 1 sw Y   nxY wY d}~Gd}~ww xY w# t        $ r Y |S w xY w)	zu
    Write 'text' to file ('filename') assuming 'encoding' in an atomic way
    Return (eventually new) encoding
    ntaNr   i  dropboxT)	overwritemodedir)r   osnamepathlibPathresolveexistsr	   osprealpathOSErrorrF   openwritestatst_modest_atimeFileNotFoundErrorumasktimelowertempfile
gettempdirr   errnoEINVALchmodutimest_mtime)r   filenamer   rQ   absolute_path_filenameabsolute_filenametextfile	file_statoriginal_modecreationrb   
tempfoldererrors                r   r]   r]      sX   
 D(+ND(	ww$	7%,\\(%;%C%C%E"%,,.$23I$J!$'LL$:!  LL2
d{#T* 	!hNN4 	!T OI	# 12I%--M ))H	) J-3355%002
/4#'Z9 %<Dt$%	HH&6 12IHH&93E3E(FG
 Oa & 	7 #X 6	7	!T OA ! 	#HHQKEHHUO "UFNMyy{H	#% %  	) {{ell*+T2 )hNN4() ) )		)  	 O		s   AF
 F5;-G )7H"  H2H" ;AJ	 
$F21F25F?AHHHH" "	J+)JI/&	J/I8	4JJ		JJc                 X    t        t        j                  j                  |       |||      S )ze
    Write 'lines' to file ('filename') assuming 'encoding'
    Return (eventually new) encoding
    )r]   rS   linesepjoin)linesrl   r   rQ   s       r   
writelinesry   $  s"    
 '8TBBr   c                 V    t        t        | d      j                               \  }}||fS )zG
    Read text from file ('filename')
    Return text and encoding
    rb)r   r\   readrl   r   r   s      r   r|   r|   +  s,    
 T(D1668:ND(>r   c                 b    t        | |      \  }}|j                  t        j                        |fS )zI
    Read lines from file ('filename')
    Return lines and encoding
    )r|   splitrS   rv   r}   s      r   	readlinesr   3  s,    
 (H-ND(::bjj!8++r   c                 F    	 t        |        S # t        t        f$ r Y yw xY w)z5
    Test if the given path is a text-like file.
    F)r   r[   IOError)rl   s    r   is_text_filer   <  s,    X&&&W s      )F)r   wb)r   ).__doc__codecsr   r   r   re   localererS   os.pathpathrY   rU   r   rc   rg   chardet.universaldetectorr   atomicwritesr   spyder.py3compatr   r	   r
   r   'spyder.utils.external.binaryornot.checkr   getpreferredencodingr   r   r   r!   r%   r'   compiler0   r3   r=   r   r   rK   r]   ry   r|   r   r    r   r   <module>r      s    2 1   	 	   
   8 %. . = 1V002 ,5G "	 $% ( BJJ12	
!F>:")H>BC,r   