
    ce                        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 ddlmZmZmZmZ  ej                    Zd Z e            Zd Zd	 Z ej        d
          Zg dZd Zd Zd ZddZddZddZ ddZ!d Z"d Z#dS )u   
Text encoding utilities, text file I/O.

Functions 'get_coding', 'decode' and 'encode' come from Eric4 source code (Utilities/__init___.py)

Copyright © 2003-2009 Detlev Offenbach
    )BOM_UTF8	BOM_UTF16	BOM_UTF32N)UniversalDetector)	is_binary)is_binary_string	is_string
is_unicodeto_text_stringc                  >    t          j                    } | t          } | S )zU
    Query the filesystem for the encoding used to encode filenames and envvars.
    )sysgetfilesystemencodingPREFERRED_ENCODING)encodings    Alib/python3.11/site-packages/anaconda_navigator/utils/encoding.pyr   r   &   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   decodeFS_ENCODINGUnicodeError	TypeError)stringunics     r   to_unicode_from_fsr   4   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 )zU
    Return a byte string version of unc encoded using the file system encoding.
    )r
   encoder   r   r   )r   r   s     r   to_fs_from_unicoder   E   s_     $ 	[[--F M i( 	 	 	D	 Ks   - A Azcoding[:=]\s*([-\w_.]+))r   z	iso8859-1z
iso8859-15asciizkoi8-rz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                    |                                  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)textliner/   codecdetectors        r   
get_codingr4   `   s    !!"1"% ! !	!%%nT&:&:;;F  !QF?? LLL " 	 	 	 D		  +$&&OO%%bqb) 	 	DMM$}  	z**4s   'A,,
A98A9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 )	zs
    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   r4   r   LookupError)r0   codings     r   r   r      s_   ??8$$ 	N!$s8}}~~"6@@+MM??9%% 	M!$s9~~"7BBHLL??9%% 	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	          |           }|rE	 |                     |          |fS # t          t          f$ r}t          d| d          |d}~ww xY w|r|                    d          s|                    d          rY|                    dd                              dd          }	 |                     |          |fS # t          t          f$ r Y nw xY w	 |                     d	          d	fS # t          $ 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.
    r6   r   zIncorrect encoding ()Nz-defaultz-guessed r    )r   r   r   r9   r4   RuntimeErrorendswithreplace)r0   orig_codingr:   	exceptions       r   r   r      s    k!!$++g...;;  	;;{++[88k* 	 	 	D	 F P	P;;v&&..k* 	P 	P 	P?f???@@iO	P ,,Z88 K<P<PQ[<\<\ $$Z44<<ZLL	;;v&&..k* 	 	 	D	{{7##W,,    ;;w((sM   A   AA)B   B*B%%B*D D/.D/3E
 

EEr   wbc                     t          | |          \  } }t          ||          5 }|                    |            ddd           n# 1 swxY w Y   |S )zg
    Write 'text' to file ('filename') assuming 'encoding'.

    Return (eventually new) encoding.
    N)r   openwrite)r0   filenamer   modetextfiles        r   rF   rF      s     D(++ND(	h		 t              Os   AA
A
c                 `    t          t          j                            |           |||          S )zh
    Write 'lines' to file ('filename') assuming 'encoding'.

    Return (eventually new) encoding.
    )rF   oslinesepjoin)linesrG   r   rH   s       r   
writelinesrO      s&     ''8TBBBr   c                     t          | d          5 }t          |                                          \  }}ddd           n# 1 swxY w Y   ||fS )zG
    Read text from file ('filename')
    Return text and encoding
    rbN)rE   r   read)rG   r   streamr0   s       r   rR   rR      s    
 
h		 /..h/ / / / / / / / / / / / / / />s   %AA	Ac                 j    t          | |          \  }}|                    t          j                  |fS )zL
    Read lines from file ('filename').

    Return lines and encoding.
    )rR   splitrK   rL   )rG   r   r0   s      r   	readlinesrV      s0     (H--ND(::bj!!8++r   c                 T    	 t          |            S # t          t          f$ r Y dS w xY w)z+Test if the given path is a text-like file.F)r   OSErrorIOError)rG   s    r   is_text_filerZ      s?    X&&&&W   uus    ''c                 R    	 |                      d          S # t          $ r | cY S w xY w)z2Ensure a file is binary format. From conda/common.r   )r   AttributeError)values    r   ensure_binaryr^      s?    ||G$$$    s    &&)r   rC   )r   )$__doc__codecsr   r   r   localerK   rer   chardet.universaldetectorr   -anaconda_navigator.external.binaryornot.checkr   "anaconda_navigator.utils.py3compatr   r	   r
   r   getpreferredencodingr   r   r   r   r   compiler'   r*   r4   r   r   rF   rO   rR   rV   rZ   r^    r   r   <module>ri      s    2 1 1 1 1 1 1 1 1 1  				 				 



 7 7 7 7 7 7 C C C C C C f f f f f f f f f f f f 1V022    $#%%  "  ( BJ122	
 
 
     F> > >D') ') ')T	 	 	 	C C C C   , , , ,      r   