o
    õ·i˜  ã                	   @  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_bomZ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 �](}"|�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'z9|�r|&du �rt#|%du �r| dtd!ƒ… n	| t|!ƒtd!ƒ… |"d"� nt#|%du �r&| n| t|!ƒd… |"d"�}#W n+ t$t%f�y] }( zt |(t%ƒ�sMt td#|"t#|(ƒ¡ | |"¡ W Y d}(~(�q'd}(~(ww t&|$�sddnt|!ƒ|t|| ƒƒ})|&�o||#du�o|t|#ƒ|k }*|*�r‡t td$|"¡ |#du�r{|&�s{t'|#ƒ}+| (|+¡},|,du�r{|,\}-}.}/|/�rCt| |"|-|$|.|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|ƒ�rA|du �s||v �rAd|v �rAd|v �rA| +¡ }1t d+|1j*¡ |�r:t t	¡ t 
|
¡ t|1gƒ  S �q'| |"¡ t td,|"¡ |	�ry|"dd|d-d.fv �ryt| |"||$g |#|d%�}2|"|k�ro|2}n
|"dk�rw|2}n|2}�q'tt|)ƒd/ ƒ}3t,|3d0ƒ}3d}4d}5g }6g }7zMt-| |"|)||$|%|!|&|#ƒ	D ]>}8|6 |8¡ |7 t.|8||d1u �o¼dt|ƒ  k�oºd0kn  ƒ¡ |7d2 |k�rÊ|4d7 }4|4|3k�s×|$�rÚ|%du �rÚ �qÜ�q�W n! t$�yþ }( zt td3|"t#|(ƒ¡ |3}4d1}5W Y d}(~(nd}(~(ww |5�s;|�r;|&�s;z| td4ƒd… j/|"d5d6� W n# t$�y: }( zt td7|"t#|(ƒ¡ | |"¡ W Y d}(~(�q'd}(~(ww |7�rFt0|7ƒt|7ƒ nd}9|9|k�sR|4|3k�r´| |"¡ |"t1v �rc| 2t1|" ¡ |#du�rv|&�sv| 3t'|#ƒ|9g df¡ t td8|"|4t)|9d' d(d)�¡ |	�r²|"dd|d-d.fv �r²|5�s²t| |"||$g |#|d%�}2|"|k�r¨|2}n
|"dk�r°|2}n|2}�q't td9|"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|;ƒ}=|=�rt td< |=|"¡¡ t| |"|9|$|=|du �s&|"|ddfv �r(|#nd|d%�}>| |>¡ |#du�rE|&�sE| 3t'|#ƒ|9|=d1f¡ |�rT|&�sT|9d=k �rT|d7 }|"|ddfv �r†|9d*k �r†|9dk�r�t d+|>j*¡ |�rzt 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>d?„ |=D ƒdd@�nd}?|?dAk�ród|v �ród|v �ród1}| 2|:¡ t tdB|"|9|?¡ |�s.|&�r.|*�r.|#du�r.t|#ƒ|dC k �r.|"dDv�r.d|v �r.d|v �r.d1}t tdE|"|9t|#ƒ|t|#ƒ| d' ¡ |"| k�rOt dF|"¡ |�rFt t	¡ t 
|
¡ t||" gƒ  S �q't|ƒdk�r©|�sa|�sa|�rgt tdG¡ |�rwt dH|j*¡ | |¡ n2|�r|du �s‘|�rŒ|�rŒ|j6|j6k�s‘|du�rœt dI¡ | |¡ n|�r©t dJ¡ | |¡ |�r»t dK| +¡ j*t|ƒd ¡ nt dL¡ |�rÍt t	¡ t 
|
¡ |S )Maf  
    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© r7   ú\C:\Users\psant\Downloads\PAF backend\school\venv\Lib\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  r1   r2   r3   r4   r7   r7   r8   r9   ~   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_32Úutf_16z\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)Úencodingz9Code 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.)Z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 )Nr7   )r5   Ú_Úvr7   r7   r8   Ú	<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\�Âõ(\ï?>	   Ú	utf_16_ber/   r=   Ú	utf_32_ler>   Ú	utf_16_leZ	utf_8_sigr?   Ú	utf_32_bezjMulti-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Úroundr@   ÚbestÚmaxr   r   ÚdecodeÚsumr   ÚupdateÚ
setdefaultr   r	   Úfingerprint)@r   r!   r#   r$   r&   r(   r)   r+   r,   r-   Zprevious_logger_levelÚlengthZis_too_small_sequenceZis_too_large_sequenceZprioritized_encodingsZspecified_encodingZtestedZtested_but_hard_failureZtested_but_soft_failureZsoft_failure_skipZsuccess_fast_trackedZpayload_result_cacheZdefinitive_match_foundZdefinitive_target_languagesZ post_definitive_sb_success_countZPOST_DEFINITIVE_SB_CAPZmb_definitive_match_foundZfallback_asciiZfallback_u8Zfallback_specifiedÚresultsZearly_stop_resultsZsig_encodingZsig_payloadZencoding_ianaZdecoded_payloadZbom_or_sig_availableZstrip_sig_or_bomZis_multi_byte_decoderZenc_languagesÚeZr_Zmulti_byte_bonusZpayload_hashÚcachedZcached_messZ	cached_cdZcached_passedZ
fast_matchZprobable_resultZfallback_entryZmax_chunk_gave_upZearly_stop_countZlazy_str_hard_failureZ	md_chunksZ	md_ratiosÚchunkZmean_mess_ratioZtarget_languagesZ	cd_ratiosZchunk_languagesZcd_ratios_mergedZcurrent_matchZbest_coherencer7   r7   r8   Ú
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.
    N)r{   Úread)
r|   r!   r#   r$   r&   r(   r)   r+   r,   r-   r7   r7   r8   Úfrom_fpR  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|   r7   r7   r8   Ú	from_pathp  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-   N)rR   rh   r   rƒ   rT   rS   r{   r~   )r„   r!   r#   r$   r&   r(   r)   r+   r,   r-   Zguessesr7   r7   r8   Ú	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   Zcdr   r   r   r	   Zconstantr
   r   r   r   r   Zmdr   Úmodelsr   r   Úutilsr   r   r   r   r   r   Ú	getLoggerrX   ÚStreamHandlerr[   ÚsetFormatterÚ	Formatterr   Ú__annotations__r   Z_supported_encrb   rf   r   r{   r~   rƒ   r†   r7   r7   r7   r8   Ú<module>   s�     
	ÿ	
€ÿö      !ö ö!ö