o
    ì6WjU¥  ã                   @  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 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¡¡ e&edd„ d�Z'de(d< 									d8d9d*d+„Z)									d8d:d.d/„Z*									d8d;d2d3„Z+									d<d=d6d7„Z,dS )>é    )ÚannotationsN)Ú	lru_cache)Ú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)sc                 C  s
   t | ƒ S ©N)r   ©Úencoding© r   úe/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/charset_normalizer/api.pyÚ<lambda>.   s   
 r   )Úkeyz	list[str]Ú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
           D      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 }tdd�tƒ}tdd�tƒ}d}tƒ }d}d}d}d}d}d}tƒ } tƒ }!t| ƒ\}"}#|"du�r"| |"¡ t tdt|#ƒ|"¡ | d¡ d|v�r1| d¡ |t D �]É}$|�rB|$|v�rB�q5|�rL|$|v �rL�q5|$|v �rS�q5| |$¡ d}%|"|$k}&|&�odt|$ƒ}'|$dv �rv|&�svt td|$¡ �q5|$dv �r‡|&�s‡t td|$¡ �q5|$|v �r•t td|$¡ �q5|$|v �r£t td|$¡ �q5zt |$ƒ}(W n t!t"f�y½   t td|$¡ Y �q5w |�râ|(�sËtt#|$ƒƒ})ntt$|$ƒƒ})|) %|¡�sât td|$|)|¡ �q5|�rø|(�sø||k�røt td |$||¡ �q5|�r|(�st td!|$¡ �q5|( �o| }*zU|�r/|(�s/t&|'�s!| dtd"ƒ… n	| t|#ƒtd"ƒ… |$d#� n3|*�sb|$d$k�rQ|&�rQt&| |$d#�}%|%�rP|%d d%k�rP|%dd… }%nt&|'�sW| n| t|#ƒd… |$d#�}%W n+ t't(f�yŽ }+ zt |+t(ƒ�s~t td&|$t&|+ƒ¡ | |$¡ W Y d}+~+�q5d}+~+ww t)|&�s•dnt|#ƒ|t|| ƒƒ},|(�o­|%du�o­t|%ƒ|k }-|-�r¸t td'|$¡ tt|,ƒd( ƒ}.t*|.d)ƒ}.d}/d}0g }1g }2zIt+| |$|,||&|'|#|(|%|*ƒ
D ]9}3|1 |3¡ |2 ||3||�oødt|ƒ  k�oöd)kn  ƒ¡ |2d* |k�r|/d7 }/|/|.k�s|&�r|'�s n�qÛW n< t't(f�yR }+ z-|*�r:t td&|$t&|+ƒ¡ | |$¡ W Y d}+~+�q5t td+|$t&|+ƒ¡ |.}/d,}0W Y d}+~+nd}+~+ww |0�s�|�r�|(�s�z| td-ƒd… j,|$d.d/� W n# t'�yŽ }+ zt td0|$t&|+ƒ¡ | |$¡ W Y d}+~+�q5d}+~+ww |2�ršt-|2ƒt|2ƒ nd}4|4|k�s¦|/|.k�r:| |$¡ |$t.v �r·| /t.|$ ¡ |%du�rÊ|(�sÊ| 0t1|%ƒ|4g df¡ t td1|$|/t2|4d2 d3d4�¡ |	�r8|$dd|d5d6fv �r8|0�s8|%du �rzt&|'�sõ| n| t|#ƒd… |$d#�}%W n t't(f�y   t td7|$¡ Y �q5w |�rd}%t| |$||&g |%|d8�}5|$|k�r.|5}n
|$dk�r6|5}n|5}�q5|*�rvzt&|'�sD| n| t|#ƒd… |$d#�}%W n% t't(f�yu }+ zt td&|$t&|+ƒ¡ | |$¡ W Y d}+~+�q5d}+~+ww |%du�rh|(�sht1|%ƒ}6| 3|6¡}7|7du�rh|7\}8}9}:|:�r0t| |$|8|&|9|�r¥|$|ddfv �r§|%nd|d8�};|  |;¡ | |$¡ t td9|$t2|8d2 d3d4�¡ |$|ddfv �rö|8d:k �rö|8dk�rñt d;|;j4¡ |�rêt t	¡ t 
|
¡ t|;gƒ  S |! |;¡ t|!ƒ�r.|du �s||v �r.d|v �r.d|v �r.|! 5¡ }<t d;|<j4¡ |�r't t	¡ t 
|
¡ t|<gƒ  S �q5| |$¡ t td<|$¡ |	�rf|$dd|d5d6fv �rft| |$||&g |%|d8�}5|$|k�r\|5}n
|$dk�rd|5}n|5}�q5t td=|$t2|4d2 d3d4�¡ |(�s~t#|$ƒ}=nt$|$ƒ}=|=�r‘t td> |$t&|=ƒ¡¡ g }>|$dk�r³|=�r d? |=¡nd}?|1D ]}3||3||?ƒ}@|> |@¡ �q¤t6|>ƒ}A|A�rÄt td@ |A|$¡¡ t| |$|4|&|A|�rÕ|$|ddfv �r×|%nd|d8�}B|  |B¡ |%du�rô|(�sô| 0t1|%ƒ|4|Ad,f¡ |�r|(�s|4dAk �r|d7 }|$|ddfv �r5|4d:k �r5|4dk�r0t d;|Bj4¡ |�r)t t	¡ t 
|
¡ t|Bgƒ  S |! |B¡ t|!ƒ�rm|du �sD||v �rmd|v �rmd|v �rm|! 5¡ }<t d;|<j4¡ |�rft t	¡ t 
|
¡ t|<gƒ  S |�s¢|(�s¢|A�r�t*dBdC„ |AD ƒddD�nd}C|CdEk�r¢d|v �r¢d|v �r¢d,}| /|=¡ t tdF|$|4|C¡ |�sÝ|(�rÝ|-�rÝ|%du�rÝt|%ƒ|dG k �rÝ|$dHv�rÝd|v �rÝd|v �rÝd,}t tdI|$|4t|%ƒ|t|%ƒ| d2 ¡ |$|"k�rþt dJ|$¡ |�rõt t	¡ t 
|
¡ t| |$ gƒ  S �q5t| ƒdk�rX|�s|�s|�rt tdK¡ |�r&t dL|j4¡ |  |¡ n2|�r.|du �s@|�r;|�r;|j7|j7k�s@|du�rKt dM¡ |  |¡ n|�rXt dN¡ |  |¡ | �rjt dO|  5¡ j4t| ƒd ¡ nt dP¡ |�r|t t	¡ t 
|
¡ | S )Qaf  
    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Úcpr   r   r   Ú
<listcomp>l   ó    zfrom_bytes.<locals>.<listcomp>zacp_exclusion is set. use this flag for debugging purpose. limited list of encoding excluded : %s.c                 S  r8   r9   r:   r;   r   r   r   r>   w   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.)Úmaxsizeé   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    €„Ar   rE   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.é   é   éÿÿÿÿzaLazyStr Loading: After MD chunk decode, code page %s does not fit given bytes sequence at ALL. %sTg     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 %%.éd   é   )ÚndigitsrC   rD   z>%s does not decode the whole payload: fallback entry withheld.)Úpreemptive_declarationzM%s fast-tracked (identical decoded payload to a prior encoding, chaos=%f %%).r%   z.Encoding detection: %s is most likely the one.zZ%s fast-skipped (identical decoded payload to a prior encoding that failed chaos probing).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 r   r   )r<   Ú_Úvr   r   r   Ú	<genexpr>C  s   € zfrom_bytes.<locals>.<genexpr>)Údefaultg      à?zyDefinitive match found: %s (chaos=%.3f, coherence=%.2f). Encodings targeting different language families will be skipped.g\�Âõ(\ï?>	   rE   r6   rC   rD   Ú	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.)8Ú
isinstanceÚ	bytearrayÚbytesÚ	TypeErrorÚformatÚtypeÚloggerÚlevelÚ
addHandlerÚexplain_handlerÚsetLevelr   ÚlenÚdebugÚremoveHandlerr   r   ÚlogÚjoinr)   r   r   r   ÚappendÚsetr   r   r   r   r!   Úaddr   r   ÚModuleNotFoundErrorÚImportErrorr   r	   ÚintersectionÚstrÚUnicodeDecodeErrorÚLookupErrorÚrangeÚmaxr   ÚdecodeÚsumr   ÚupdateÚ
setdefaultÚhashÚroundÚgetr   Úbestr
   Úfingerprint)Dr&   r(   r*   r+   r-   r/   r0   r2   r3   r4   Ú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Úcached_mess_ratioÚcached_coherence_ratioÚ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Údeferred_decodingÚeÚr_Úmulti_byte_bonusÚmax_chunk_gave_upÚearly_stop_countÚlazy_str_hard_failureÚ	md_chunksÚ	md_ratiosÚchunkÚmean_mess_ratioÚfallback_entryÚpayload_hashÚcachedÚcached_messÚ	cached_cdÚcached_passedÚ
fast_matchÚprobable_resultÚtarget_languagesÚ	cd_ratiosÚlg_inclusionÚchunk_languagesÚcd_ratios_mergedÚcurrent_matchÚbest_coherencer   r   r   Ú
from_bytes2   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/   r0   r2   r3   r4   r   r   r   Ú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/   r0   r2   r3   r4   r¸   r   r   r   Ú	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/   r0   r2   r3   r4   )rY   ro   r   r¿   r[   rZ   r·   rº   )rÀ   r(   r*   r+   r-   r/   r0   r2   r3   r4   Úguessesr   r   r   Ú	is_binaryê  s\   ö-ßþþöóörÃ   )	r"   r#   r$   NNTFr%   T)r&   r'   r(   r)   r*   r)   r+   r,   r-   r.   r/   r.   r0   r1   r2   r1   r3   r,   r4   r1   r5   r   )r¸   r   r(   r)   r*   r)   r+   r,   r-   r.   r/   r.   r0   r1   r2   r1   r3   r,   r4   r1   r5   r   )r»   r¼   r(   r)   r*   r)   r+   r,   r-   r.   r/   r.   r0   r1   r2   r1   r3   r,   r4   r1   r5   r   )	r"   r#   r$   NNTFr%   F)rÀ   rÁ   r(   r)   r*   r)   r+   r,   r-   r.   r/   r.   r0   r1   r2   r1   r3   r,   r4   r1   r5   r1   )-Ú
__future__r   ÚloggingÚ	functoolsr   Úosr   Útypingr   Úcdr   r   r	   r
   Úconstantr   r   r   r   r   Úmdr   Úmodelsr   r   Úutilsr   r   r   r   r   r   Ú	getLoggerr_   ÚStreamHandlerrb   ÚsetFormatterÚ	FormatterÚsortedr!   Ú__annotations__r·   rº   r¿   rÃ   r   r   r   r   Ú<module>   s‚     
	ÿÿö       ö ö!ö