
    G@d)                     ^   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j                    ZeefdZd	 Z e            Zd
 Z d Z! ej"        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-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_text_string)	is_binaryc                     	 |                      d                              d          S # t          $ r@ 	 |                      d                              |          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      5lib/python3.11/site-packages/spyder/utils/encoding.py	transcoder   &   s    {{7##**8444   	;;w''..v66666 	 	 	KKKKK	s,   '* 
A4'AA4A0+A4/A00A4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          |                                 d          } nCt          |           r4	 |                     t
                    }|S # t          t          f$ r Y nw xY w| S )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   s     V 	99F## 	}}[11  !),    Ms   A A32A3c                     t          |           r4	 |                     t                    }|S # t          t          f$ r Y nw xY w| S )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   s_    
 d 	[[--F M i( 	 	 	D	 Ks   - A A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                    |s{|                                  dd         D ]^}	 t                              t          |                    }|r"|                    d          }|t
          v r|c S O# t          $ r Y [w xY wt          |           rlt                      }|                                  dd         D ] }|	                    |           |j
        r n!|                                 |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	   groupCODECSUnicodeDecodeErrorr
   r   feeddonecloseresult)r   force_chardetliner8   codecdetectors         r   
get_codingr=   p   s"     %OO%%bqb) 	% 	%D%")).*>*>??  %"LLOOE $ &    	  +$&&OO%%bqb) 	$ 	$DMM$}#ee#z**4s   'A..
A;:A;c                    	 |                      t                    r,t          | t          t                    d         d          dfS |                      t                    r,t          | t          t                    d         d          dfS |                      t
                    r,t          | t          t
                    d         d          dfS t          |           }|rt          | |          |fS n# t          t          f$ r Y nw xY w	 t          | d          dfS # 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$$ 	M!$s8}}~~"6@@+MM__Y'' 	M!$s9~~"7BBHLL__Y'' 	M!$s9~~"7BBHLLD!! 	8!$//77	8+&   dG,,o==+&    $	**,===s8   AC8 AC8 AC8 "C8 8DDD" "D65D6c                    |dk    rt           |                     d          z   dfS |r/	 |                     |          |fS # t          t          f$ r Y nw xY wt	          |           }|r?	 |                     |          |fS # t          t          f$ r t          d|z            w xY w|r|                    d          s|                    d          r[|                    dd          }|                    dd          }	 |                     |          |fS # t          t          f$ r Y nw xY w|                     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
    r?   r   zIncorrect encoding (%s)z-defaultz-guessed )r   r   r   rB   r=   RuntimeErrorendswithreplace)r   orig_codingrC   s      r   r   r      s    k!!$++g...;;  	;;{++[88k* 	 	 	D	 F C	C;;v&&..k* 	C 	C 	C86ABBB	C {++J77 :&&$$Z44$$Z44	;;v&&..k* 	 	 	D	 ;;w((s/   A   AA)B   $B$ D D+*D+c                     t          |           s:t          D ]2}	 t          | |          }|c S # t          $ r Y #t          $ r Y  nw xY w| S )zConvert a string to unicode)r   r3   r	   r   r"   )r#   r;   r$   s      r   
to_unicoderK      s    &!! 	 	 	E%fe44          Ms   .
A	AAr   wbc                 T   t          | |          \  } }t          j        dk    r	 t          j        |                                          }|                                rt          |          }nt          j	        |          }n?# t          t          f$ r t          j	        |          }Y nw xY wt          j	        |          }d|v r?t          ||          5 }|                    |            ddd           n# 1 swxY w Y   n	 t          j        |          }|j        }|j        }	nQ# t"          $ rD t          j        d          }
t          j        |
           d|
 z  }t'          j                    }	Y nw xY w	 d}d|                                v rt+          j                    }t/          |d||          5 }|                    |            ddd           n# 1 swxY w Y   ni# t          $ r\}|j        t0          j        k    r=t          ||          5 }|                    |            ddd           n# 1 swxY w Y   Y d}~nd}~ww xY w	 t          j        ||           t          j        |          }t          j        ||	|j        f           n# t          $ r Y nw xY w|S )	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   rR   absolute_path_filenameabsolute_filenametextfile	file_statoriginal_modecreationrc   
tempfoldererrors                r   r^   r^      s   
 D(++ND(	w$	7%,\(%;%;%C%C%E%E"%,,.. ;$23I$J$J!!$'L$:$:!& 	7 	7 	7 #X 6 6	7  L22
d{{#T** 	!hNN4   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!	# 122I%-M )HH  	# 	# 	#HQKKEHUOOO "UFNMy{{HHH	#	) J-335555%022
/4#'Z9 9 9 %<Dt$$$% % % % % % % % % % % % % % %  	) 	) 	) {el**+T22 )hNN4((() ) ) ) ) ) ) ) ) ) ) ) ) ) )		)	H&666 122IH&93E(FGGGG 	 	 	 D	 Os   AB %B,+B,C99C= C="D* *AE87E8<>G( :GG( G  G( #G $G( (
I2%I	H9-I	9H=	=I	 H=	I		IAJ 
J%$J%c                 `    t          t          j                            |           |||          S )ze
    Write 'lines' to file ('filename') assuming 'encoding'
    Return (eventually new) encoding
    )r^   rT   linesepjoin)linesrm   r   rR   s       r   
writelinesrz   $  s&    
 ''8TBBBr   c                 n    t          t          | d                                                    \  }}||fS )zG
    Read text from file ('filename')
    Return text and encoding
    rb)r   r]   readrm   r   r   s      r   r}   r}   +  s5    
 T(D116688::ND(>r   c                 j    t          | |          \  }}|                    t          j                  |fS )zI
    Read lines from file ('filename')
    Return lines and encoding
    )r}   splitrT   rw   r~   s      r   	readlinesr   3  s0    
 (H--ND(::bj!!8++r   c                 T    	 t          |            S # t          t          f$ r Y dS w xY w)z5
    Test if the given path is a text-like file.
    F)r   r\   IOError)rm   s    r   is_text_filer   <  s?    X&&&&W   uus    '')F)r   rL   )r   ).__doc__codecsr   r   r   rf   localererT   os.pathpathrZ   rV   r   rd   rh   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^   rz   r}   r   r    r   r   <module>r      s=    2 1 1 1 1 1 1 1 1 1   				 				        



   8 7 7 7 7 7 % % % % % %. . . . . . . . . . . . = = = = = = 1V022 ,5G    "	 	 	 $#%%     ( BJ122	
 
 
! ! ! !F> > >:") ") ")H  > > > >BC C C C   , , , ,    r   