
    	l`              
       `   d Z ddlZddlZ ej        e          Zd ZddZdZe	e
u rVed                     ee edd	                              z   Zd                     ee ed	d
                              Zn-e e	 edd	                    z   Z e	 ed	d
                    Zd ZdS )zP
binaryornot.helpers
-------------------

Helper utilities used by BinaryOrNot.
    Nc                 ^    t          d                    d | D                                  dS )z&
    Print a string as hex bytes.
    :c              3   Z   K   | ]&}d                      t          |                    V  'dS )z{0:x}N)formatord).0cs     3lib/python3.11/site-packages/binaryornot/helpers.py	<genexpr>zprint_as_hex.<locals>.<genexpr>   s4      55a7>>#a&&))555555    N)printjoin)ss    r
   print_as_hexr      s3     
#((551555
5
566666r      c                     	 t          | d          5 }|                    |          }|cddd           S # 1 swxY w Y   dS # t          $ r}t          |           Y d}~dS d}~ww xY w)z
    :param filename: File to open and get the first little chunk of.
    :param length: Number of bytes to read, default 1024.
    :returns: Starting chunk of bytes.
    rbN)openreadIOErrorr   )filenamelengthfchunkes        r
   get_starting_chunkr      s    (D!! 	QFF6NNE	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	    as2   A 6A :A :A 
A'A""A's   
	r             c                    | sdS |                      dt                    }t          t          |                    t          t          |                     z  }t                              dt                                 |                      dt                    }t          t          |                    t          t          |                     z  }t                              dt                                 |dk    r|dk     p|dk    o|dk    }t                              dt                                 t          j	        |           }t                              d	t                                 d}|d
         dk    r|d         dk    r	 	 | 
                    |d                    n'# t          $ r t          | |d                    Y nw xY wd}t                              dt                                 n]# t          $ r t                              d|           Y n6t          $ r* t                              dt                                 Y nw xY wt                              dt                                 |r|rdS dS |rdS d| v sd| v r.t                              dt          d| v           z              dS dS )a  
    Uses a simplified version of the Perl detection algorithm,
    based roughly on Eli Bendersky's translation to Python:
    http://eli.thegreenplace.net/2011/10/19/perls-guess-if-file-is-text-or-binary-implemented-in-python/

    This is biased slightly more in favour of deeming files as text
    files than the Perl algorithm, since all ASCII compatible character
    sets are accepted as text, not just utf-8.

    :param bytes: A chunk of bytes to check.
    :returns: True if appears to be a binary, otherwise False.
    FNz"nontext_ratio1: %(nontext_ratio1)rz"nontext_ratio2: %(nontext_ratio2)rg333333?g?g?z&is_likely_binary: %(is_likely_binary)rz(detected_encoding: %(detected_encoding)r
confidenceg?encodingascii)r"   Tz7success: decodable_as_unicode: %(decodable_as_unicode)rz0failure: could not look up encoding %(encoding)sz7failure: decodable_as_unicode: %(decodable_as_unicode)r       z
has nulls:)	translate_printable_asciifloatlenloggerdebuglocals_printable_high_asciichardetdetectdecode	TypeErrorunicodeLookupErrorUnicodeDecodeErrorrepr)bytes_to_check	low_charsnontext_ratio1
high_charsnontext_ratio2is_likely_binarydetected_encodingdecodable_as_unicodes           r
   is_binary_stringr>   3   s     u ((/?@@I3y>>**U3~3F3F-G-GGN
LL5vxx@@@  ))$0EFFJ3z??++eC4G4G.H.HHN
LL5vxx@@@ 
#		7.4"7 	8	#		6.3"6  LL9688DDD  ~66
LL;VXXFFF !,'#--j)W44	?P%%/@/L%MMMM P P P1B:1NOOOOOOP $( LL 45;XX? ? ? ? 	, 	, 	,LLK*, , , , ,! 	? 	? 	?LL 45;XX? ? ? ? ?	? LL ,-3XX7 7 7  	54 	5.((G~,E,E\DN1J,K,KKLLLtus6   F. -G? .!GG? G,G? ?%I&0II)r   )__doc__r.   logging	getLogger__name__r*   r   r   _control_charsbytesstrr   mapchrranger'   r-   r>    r   r
   <module>rJ      s      
	8	$	$7 7 7    C<<%S%%C..1I1I(J(JJHHSSeeCoo%>%>?? &eeBnn(=(==!E%%S//22Q Q Q Q Qr   