o
    Õ­j¼—  ã                	   @  s¬  U d dl m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mZmZmZmZ ddlmZ dd	lmZmZ dd
lmZmZmZmZmZmZ e d¡Ze  ¡ Z!e! "e #d¡¡ g Z$de%d< g Z&de%d< eD ]!Z'zee'ƒr{e$ (e'¡ ne& (e'¡ W qn e)y�   e& (e'¡ Y qnw e$e& Z*de%d< 									d6d7d(d)„Z+									d6d8d,d-„Z,									d6d9d0d1„Z-									d:d;d4d5„Z.dS )<é    )ÚannotationsN)ÚPathLike)ÚBinaryIOé   )Úcoherence_ratioÚencoding_languagesÚmb_encoding_languagesÚmerge_coherence_ratios)ÚIANA_SUPPORTEDÚIANA_SUPPORTED_SIMILARÚTOO_BIG_SEQUENCEÚTOO_SMALL_SEQUENCEÚTRACE)Ú
mess_ratio)ÚCharsetMatchÚCharsetMatches)Úany_specified_encodingÚcut_sequence_chunksÚ	iana_nameÚidentify_sig_or_bomÚis_multi_byte_encodingÚshould_strip_sig_or_bomÚcharset_normalizerz)%(asctime)s | %(levelname)s | %(message)sz	list[str]Ú_mb_supportedÚ_sb_supportedÚIANA_SUPPORTED_MB_FIRSTé   é   çš™™™™™É?TFçš™™™™™¹?Ú	sequencesúbytes | bytearrayÚstepsÚintÚ
chunk_sizeÚ	thresholdÚfloatÚcp_isolationúlist[str] | NoneÚcp_exclusionÚpreemptive_behaviourÚboolÚexplainÚlanguage_thresholdÚenable_fallbackÚreturnr   c
           @      C  sÚ  t | ttfƒstd t| ƒ¡ƒ‚|rtj}
t t	¡ t 
t¡ t| ƒ}|dkrDt d¡ |r8t t	¡ t 
|
¡ tt| dddg dƒgƒS |durZt td	d
 |¡¡ dd„ |D ƒ}ng }|durrt tdd
 |¡¡ dd„ |D ƒ}ng }||| kr‡t td|||¡ d}|}|dkr—|| |k r—t|| ƒ}t| ƒtk }t| ƒtk}|r¯t td |¡¡ n|rºt td |¡¡ g }|rÂt| ƒnd}|durÔ| |¡ t td|¡ tƒ }g }g }tƒ }tƒ }i }d}tƒ }d}d}d}d}d}d}tƒ }tƒ }t| ƒ\} }!| du�r| | ¡ t tdt|!ƒ| ¡ | d¡ d|v�r#| d¡ |t D �]F}"|�r4|"|v�r4�q'|�r>|"|v �r>�q'|"|v �rE�q'| |"¡ d}#| |"k}$|$�oVt|"ƒ}%|"dv �rh|$�sht td|"¡ �q'|"dv �ry|$�syt td|"¡ �q'|"|v �r‡t td|"¡ �q'|"|v �r•t td|"¡ �q'zt|"ƒ}&W n ttf�y¯   t td|"¡ Y �q'w |�rÔ|&�s½tt |"ƒƒ}'ntt!|"ƒƒ}'|' "|¡�sÔt td|"|'|¡ �q'|�rê|&�sê||k�rêt td|"||¡ �q'|�rù|&�sùt td |"¡ �q'zX|�r|&du �rt#|%du �r| dtd!ƒ… n	| t|!ƒtd!ƒ… |"d"� n2|"d#k�r=|$�r=t#| |"d"�}#|#�r<|#d d$k�r<|#dd… }#nt#|%du �rE| n| t|!ƒd… |"d"�}#W n+ t$t%f�y| }( zt |(t%ƒ�slt td%|"t#|(ƒ¡ | |"¡ W Y d}(~(�q'd}(~(ww t&|$�sƒdnt|!ƒ|t|| ƒƒ})|&�o›|#du�o›t|#ƒ|k }*|*�r¦t td&|"¡ |#du�rš|&�sšt'|#ƒ}+| (|+¡},|,du�rš|,\}-}.}/|/�rbt| |"|-|$|.|du �s×|"|ddfv �rÙ|#nd|d'�}0| |0¡ | |"¡ t td(|"t)|-d) d*d+�¡ |"|ddfv �r(|-d,k �r(|-dk�r#t d-|0j*¡ |�rt t	¡ t 
|
¡ t|0gƒ  S | |0¡ t|ƒ�r`|du �s7||v �r`d|v �r`d|v �r`| +¡ }1t d-|1j*¡ |�rYt t	¡ t 
|
¡ t|1gƒ  S �q'| |"¡ t td.|"¡ |	�r˜|"dd|d/d0fv �r˜t| |"||$g |#|d'�}2|"|k�rŽ|2}n
|"dk�r–|2}n|2}�q'tt|)ƒd1 ƒ}3t,|3d2ƒ}3d}4d}5g }6g }7zLt-| |"|)||$|%|!|&|#ƒ	D ]=}8|6 |8¡ |7 t.|8||d3u �oÛdt|ƒ  k�oÙd2kn  ƒ¡ |7d4 |k�ré|4d7 }4|4|3k�sö|$�rø|%du �rø n�q¼W n! t$�y }( zt td5|"t#|(ƒ¡ |3}4d3}5W Y d}(~(nd}(~(ww |5�sY|�rY|&�sYz| td6ƒd… j/|"d7d8� W n# t$�yX }( zt td9|"t#|(ƒ¡ | |"¡ W Y d}(~(�q'd}(~(ww |7�rdt0|7ƒt|7ƒ nd}9|9|k�sp|4|3k�rÒ| |"¡ |"t1v �r�| 2t1|" ¡ |#du�r”|&�s”| 3t'|#ƒ|9g df¡ t td:|"|4t)|9d) d*d+�¡ |	�rÐ|"dd|d/d0fv �rÐ|5�sÐt| |"||$g |#|d'�}2|"|k�rÆ|2}n
|"dk�rÎ|2}n|2}�q't td;|"t)|9d) d*d+�¡ |&�sèt |"ƒ}:nt!|"ƒ}:|:�rût td< |"t#|:ƒ¡¡ g };|"dk�r |6D ]}8t4|8||:�rd= |:¡ndƒ}<|; |<¡ �qt5|;ƒ}=nt5|;ƒ}=|=�r1t td> |=|"¡¡ t| |"|9|$|=|du �sD|"|ddfv �rF|#nd|d'�}>| |>¡ |#du�rc|&�sc| 3t'|#ƒ|9|=d3f¡ |�rr|&�sr|9d?k �rr|d7 }|"|ddfv �r¤|9d,k �r¤|9dk�rŸt d-|>j*¡ |�r˜t t	¡ t 
|
¡ t|>gƒ  S | |>¡ t|ƒ�rÜ|du �s³||v �rÜd|v �rÜd|v �rÜ| +¡ }1t d-|1j*¡ |�rÕt t	¡ t 
|
¡ t|1gƒ  S |�s|&�s|=�rðt,d@dA„ |=D ƒddB�nd}?|?dCk�rd|v �rd|v �rd3}| 2|:¡ t tdD|"|9|?¡ |�sL|&�rL|*�rL|#du�rLt|#ƒ|dE k �rL|"dFv�rLd|v �rLd|v �rLd3}t tdG|"|9t|#ƒ|t|#ƒ| d) ¡ |"| k�rmt dH|"¡ |�rdt t	¡ t 
|
¡ t||" gƒ  S �q't|ƒdk�rÇ|�s|�s|�r…t tdI¡ |�r•t dJ|j*¡ | |¡ n2|�r�|du �s¯|�rª|�rª|j6|j6k�s¯|du�rºt dK¡ | |¡ n|�rÇt dL¡ | |¡ |�rÙt dM| +¡ j*t|ƒd ¡ nt dN¡ |�rët t	¡ t 
|
¡ |S )Oaf  
    Given a raw bytes sequence, return the best possibles charset usable to render str objects.
    If there is no results, it is a strong indicator that the source is binary/not text.
    By default, the process will extract 5 blocks of 512o each to assess the mess and coherence of a given sequence.
    And will give up a particular code page after 20% of measured mess. Those criteria are customizable at will.

    The preemptive behavior DOES NOT replace the traditional detection workflow, it prioritize a particular code page
    but never take it for granted. Can improve the performance.

    You may want to focus your attention to some code page or/and not others, use cp_isolation and cp_exclusion for that
    purpose.

    This function will strip the SIG in the payload/sequence every time except on UTF-16, UTF-32.
    By default the library does not setup any handler other than the NullHandler, if you choose to set the 'explain'
    toggle to True it will alter the logger configuration to add a StreamHandler that is suitable for debugging.
    Custom logging format and handler can be set manually.
    z3Expected object of type bytes or bytearray, got: {}r   z<Encoding detection on empty bytes, assuming utf_8 intention.Úutf_8g        FÚ Nz`cp_isolation is set. use this flag for debugging purpose. limited list of encoding allowed : %s.z, c                 S  ó   g | ]}t |d ƒ‘qS ©F©r   ©Ú.0Úcp© r8   úS/var/www/html/CropPilot/venv/lib/python3.10/site-packages/charset_normalizer/api.pyÚ
<listcomp>s   ó    zfrom_bytes.<locals>.<listcomp>zacp_exclusion is set. use this flag for debugging purpose. limited list of encoding excluded : %s.c                 S  r2   r3   r4   r5   r8   r8   r9   r:   ~   r;   z^override steps (%i) and chunk_size (%i) as content does not fit (%i byte(s) given) parameters.r   z>Trying to detect encoding from a tiny portion of ({}) byte(s).zIUsing lazy str decoding because the payload is quite large, ({}) byte(s).z@Detected declarative mark in sequence. Priority +1 given for %s.é   zIDetected a SIG or BOM mark on first %i byte(s). Priority +1 given for %s.Úascii>   Úutf_16Úutf_32z\Encoding %s won't be tested as-is because it require a BOM. Will try some sub-encoder LE/BE.>   Úutf_7zREncoding %s won't be tested as-is because detection is unreliable without BOM/SIG.zY%s is deemed too similar to a code page that was already considered unsuited. Continuing!zESkipping %s: already fast-tracked from a similar successful encoding.z2Encoding %s does not provide an IncrementalDecoderzbSkipping %s: definitive match already found, this encoding targets different languages (%s vs %s).zXSkipping %s: already accumulated %d same-family results after definitive match (cap=%d).zCSkipping single-byte %s: multi-byte definitive match already found.g    €„A)Úencodingr@   u   ï»¿z9Code page %s does not fit given bytes sequence at ALL. %szpCode page %s is a multi byte encoding table and it appear that at least one character was encoded using n-bytes.)Úpreemptive_declarationzM%s fast-tracked (identical decoded payload to a prior encoding, chaos=%f %%).éd   é   )Úndigitsr   z.Encoding detection: %s is most likely the one.zZ%s fast-skipped (identical decoded payload to a prior encoding that failed chaos probing).r>   r?   é   é   TéÿÿÿÿzaLazyStr Loading: After MD chunk decode, code page %s does not fit given bytes sequence at ALL. %sg     jè@Ústrict)Úerrorsz^LazyStr Loading: After final lookup, code page %s does not fit given bytes sequence at ALL. %szc%s was excluded because of initial chaos probing. Gave up %i time(s). Computed mean chaos is %f %%.z=%s passed initial chaos probing. Mean measured chaos is %f %%z&{} should target any language(s) of {}ú,z We detected language {} using {}g{®Gáz”?c                 s  s   � | ]\}}|V  qd S )Nr8   )r6   Ú_Úvr8   r8   r9   Ú	<genexpr>ö  s   € zfrom_bytes.<locals>.<genexpr>)Údefaultg      à?zyDefinitive match found: %s (chaos=%.3f, coherence=%.2f). Encodings targeting different language families will be skipped.g\�Âõ(\ï?>	   r@   r0   r>   r?   Ú	utf_16_beÚ	utf_16_leÚ	utf_32_beÚ	utf_32_leÚ	utf_8_sigzjMulti-byte definitive match: %s (chaos=%.3f, decoded=%d/%d=%.1f%%). Single-byte encodings will be skipped.zoEncoding detection: %s is most likely the one as we detected a BOM or SIG within the beginning of the sequence.zONothing got out of the detection process. Using ASCII/UTF-8/Specified fallback.z7Encoding detection: %s will be used as a fallback matchz:Encoding detection: utf_8 will be used as a fallback matchz:Encoding detection: ascii will be used as a fallback matchz]Encoding detection: Found %s as plausible (best-candidate) for content. With %i alternatives.z=Encoding detection: Unable to determine any suitable charset.)7Ú
isinstanceÚ	bytearrayÚbytesÚ	TypeErrorÚformatÚtypeÚloggerÚlevelÚ
addHandlerÚexplain_handlerÚsetLevelr   ÚlenÚdebugÚremoveHandlerr   r   ÚlogÚjoinr#   r   r   r   ÚappendÚsetr   r   Úaddr   r   ÚModuleNotFoundErrorÚImportErrorr   r   ÚintersectionÚstrÚUnicodeDecodeErrorÚLookupErrorÚrangeÚhashÚgetÚroundrA   ÚbestÚmaxr   r   ÚdecodeÚsumr   ÚupdateÚ
setdefaultr   r	   Úfingerprint)@r    r"   r$   r%   r'   r)   r*   r,   r-   r.   Úprevious_logger_levelÚlengthÚis_too_small_sequenceÚis_too_large_sequenceÚprioritized_encodingsÚspecified_encodingÚtestedÚtested_but_hard_failureÚtested_but_soft_failureÚsoft_failure_skipÚsuccess_fast_trackedÚpayload_result_cacheÚdefinitive_match_foundÚdefinitive_target_languagesÚ post_definitive_sb_success_countÚPOST_DEFINITIVE_SB_CAPÚmb_definitive_match_foundÚfallback_asciiÚfallback_u8Úfallback_specifiedÚresultsÚearly_stop_resultsÚsig_encodingÚsig_payloadÚencoding_ianaÚdecoded_payloadÚbom_or_sig_availableÚstrip_sig_or_bomÚis_multi_byte_decoderÚenc_languagesÚeÚr_Úmulti_byte_bonusÚpayload_hashÚcachedÚcached_messÚ	cached_cdÚcached_passedÚ
fast_matchÚprobable_resultÚfallback_entryÚmax_chunk_gave_upÚearly_stop_countÚlazy_str_hard_failureÚ	md_chunksÚ	md_ratiosÚchunkÚmean_mess_ratioÚtarget_languagesÚ	cd_ratiosÚchunk_languagesÚcd_ratios_mergedÚcurrent_matchÚbest_coherencer8   r8   r9   Ú
from_bytes9   sœ  ÿÿ




üüûÿþÿþÿ
ý	


ü




ÿýý
ý
ýýúûÿþ
ûý
ÿúþ€
ÿú€ü
€÷
ýÿ
ýü



ÿýñ

ü

þ


ÿ

þ


ýûù
	

÷
&ýÿ
ÿ
€ÿü€öÿþýü
€ø


ÿú
ÿÿþù
	
ü
ÿþ
ý
ÿþ
ýò
þÿþ


þ


ÿ

þ

	ÿý
ûÿþý
ÿ

ù

ý

÷þþÿ
þý




ý


r¯   Úfpr   c
           
      C  s   t |  ¡ |||||||||	ƒ
S )z†
    Same thing than the function from_bytes but using a file pointer that is already ready.
    Will not close the file pointer.
    )r¯   Úread)
r°   r"   r$   r%   r'   r)   r*   r,   r-   r.   r8   r8   r9   Úfrom_fp`  s   ör²   Úpathústr | bytes | PathLikec
                 C  sH   t | dƒ�}
t|
|||||||||	ƒ
W  d  ƒ S 1 sw   Y  dS )z•
    Same thing than the function from_bytes but with one extra step. Opening and reading given file path in binary mode.
    Can raise IOError.
    ÚrbN)Úopenr²   )r³   r"   r$   r%   r'   r)   r*   r,   r-   r.   r°   r8   r8   r9   Ú	from_path~  s   ö$ÿr·   Úfp_or_path_or_payloadú!PathLike | str | BinaryIO | bytesc
                 C  s‚   t | ttfƒrt| |||||||||	d�
}
|
 S t | ttfƒr0t| |||||||||	d�
}
|
 S t| |||||||||	d�
}
|
 S )a)  
    Detect if the given input (file, bytes, or path) points to a binary file. aka. not a string.
    Based on the same main heuristic algorithms and default kwargs at the sole exception that fallbacks match
    are disabled to be stricter around ASCII-compatible but unlikely to be a string.
    )	r"   r$   r%   r'   r)   r*   r,   r-   r.   )rU   rk   r   r·   rW   rV   r¯   r²   )r¸   r"   r$   r%   r'   r)   r*   r,   r-   r.   Úguessesr8   r8   r9   Ú	is_binary�  s\   ö-ßþþöóör»   )	r   r   r   NNTFr   T)r    r!   r"   r#   r$   r#   r%   r&   r'   r(   r)   r(   r*   r+   r,   r+   r-   r&   r.   r+   r/   r   )r°   r   r"   r#   r$   r#   r%   r&   r'   r(   r)   r(   r*   r+   r,   r+   r-   r&   r.   r+   r/   r   )r³   r´   r"   r#   r$   r#   r%   r&   r'   r(   r)   r(   r*   r+   r,   r+   r-   r&   r.   r+   r/   r   )	r   r   r   NNTFr   F)r¸   r¹   r"   r#   r$   r#   r%   r&   r'   r(   r)   r(   r*   r+   r,   r+   r-   r&   r.   r+   r/   r+   )/Ú
__future__r   ÚloggingÚosr   Útypingr   Úcdr   r   r   r	   Úconstantr
   r   r   r   r   Úmdr   Úmodelsr   r   Úutilsr   r   r   r   r   r   Ú	getLoggerr[   ÚStreamHandlerr^   ÚsetFormatterÚ	Formatterr   Ú__annotations__r   Ú_supported_encre   ri   r   r¯   r²   r·   r»   r8   r8   r8   r9   Ú<module>   s�     
	ÿ	
€ÿö      /ö ö!ö