a oeT @sddlZddlmZddlmZmZmZmZmZddl m Z m Z m Z m Z ddlmZmZmZmZddlmZddlmZmZdd lmZmZmZmZmZmZmZe d Z!e"Z#e#$e%d dee&e'fe(e(e)eee*eee*e+e+e)e+ed ddZ,dee(e(e)eee*eee*e+e+e)e+ed ddZ-d ee*e&efe(e(e)eee*eee*e+e+e)e+ed ddZ.d!eee*ee&fe(e(e)eee*eee*e+e+e)e+e+d ddZ/dS)"N)PathLike)BinaryIOListOptionalSetUnion)coherence_ratioencoding_languagesmb_encoding_languagesmerge_coherence_ratios)IANA_SUPPORTEDTOO_BIG_SEQUENCETOO_SMALL_SEQUENCETRACE) mess_ratio) CharsetMatchCharsetMatches)any_specified_encodingcut_sequence_chunks iana_nameidentify_sig_or_bom is_cp_similaris_multi_byte_encodingshould_strip_sig_or_bomZcharset_normalizerz)%(asctime)s | %(levelname)s | %(message)s皙?TF皙?) sequencessteps chunk_size threshold cp_isolation cp_exclusionpreemptive_behaviourexplainlanguage_thresholdenable_fallbackreturnc / Cs t|ttfs tdt||r>tj} tt t t t |} | dkrt d|rvtt t | prtjtt|dddgdgS|durtt d d |d d |D}ng}|durtt d d |dd |D}ng}| ||krtt d||| d}| }|dkr:| ||kr:t| |}t |tk} t |tk} | rltt d| n| rtt d| g}|rt|nd}|dur||tt d|t}g}g}d}d}d}t}t|\}}|dur||tt dt |||dd|vr.|d|tD]}|rP||vrPq6|rd||vrdq6||vrrq6||d}||k}|ot|}|dvr|stt d|q6|dvr|stt d|q6z t|}Wn,t t!fytt d|Yq6Yn0zr| r^|dur^t"|durB|dtdn|t |td|dn&t"|durn|n|t |d|d}Wnbt#t$fy}zDt|t$stt d|t"|||WYd}~q6WYd}~n d}~00d}|D]} t%|| rd}qq|r*tt d|| q6t&|s6dnt || t| |}!|of|duoft || k}"|"r|tt d |tt |!d!}#t'|#d"}#d}$d}%g}&g}'zt(|||!|||||| D]|}(|&|(|'t)|(||duodt |kod"kn|'d#|kr|$d7}$|$|#ks4|r|durq>qWnBt#y}z(tt d$|t"||#}$d}%WYd}~n d}~00|%s| r|sz|td%dj*|d&d'WnRt#y}z8tt d(|t"|||WYd}~q6WYd}~n d}~00|'rt+|'t |'nd})|)|ks6|$|#kr||tt d)||$t,|)d*d+d,| r6|dd|fvr6|%s6t|||dg|}*||kr|*}n|dkr|*}n|*}q6tt d-|t,|)d*d+d,|st-|}+nt.|}+|+rtt d.|t"|+g},|dkrF|&D],}(t/|(||+r2d/|+nd}-|,|-qt0|,}.|.rhtt d0|.||t|||)||.|||ddfvr|)d1krt d2||rtt t | t||gS||kr6t d3||rtt t | t||gSq6t |dkr|s8|s8|rDtt d4|rdt d5|j1||nd|rt|dus|r|r|j2|j2ks|durt d6||n|rt d7|||rt d8|3j1t |dn t d9| rtt t | |S):af 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. z4Expected object of type bytes or bytearray, got: {0}rz[zfrom_bytes..zacp_exclusion is set. use this flag for debugging purpose. limited list of encoding excluded : %s.cSsg|]}t|dqSr,r-r.r0r0r1r2fr3z^override steps (%i) and chunk_size (%i) as content does not fit (%i byte(s) given) parameters.rz>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_16utf_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.z2Encoding %s does not provide an IncrementalDecodergA)encodingz9Code page %s does not fit given bytes sequence at ALL. %sTzW%s is deemed too similar to code page %s and was consider unsuited already. Continuing!zpCode 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. %sgj@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)ndigitsz=%s passed initial chaos probing. Mean measured chaos is %f %%z&{} should target any language(s) of {},z We detected language {} using {}rz.Encoding detection: %s is most likely the one.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.)4 isinstance bytearraybytes TypeErrorformattypeloggerlevelZ addHandlerexplain_handlerZsetLevelrlendebugZ removeHandlerloggingZWARNINGrrlogjoinintrrrappendsetrr addrrModuleNotFoundError ImportErrorstrUnicodeDecodeError LookupErrorrrangemaxrrdecodesumroundr r r r r8Z fingerprintZbest)/rr r!r"r#r$r%r&r'r(Zprevious_logger_levellengthZis_too_small_sequenceZis_too_large_sequenceZprioritized_encodingsZspecified_encodingZtestedZtested_but_hard_failureZtested_but_soft_failureZfallback_asciiZ fallback_u8Zfallback_specifiedresultsZ sig_encodingZ sig_payloadZ encoding_ianaZdecoded_payloadZbom_or_sig_availableZstrip_sig_or_bomZis_multi_byte_decodereZsimilar_soft_failure_testZencoding_soft_failedZr_Zmulti_byte_bonusZmax_chunk_gave_upZearly_stop_countZlazy_str_hard_failureZ md_chunksZ md_ratioschunkZmean_mess_ratioZfallback_entryZtarget_languagesZ cd_ratiosZchunk_languagesZcd_ratios_mergedr0r0r1 from_bytes!s                      $      &   $                        rb) fpr r!r"r#r$r%r&r'r(r)c Cst|||||||||| S)z Same thing than the function from_bytes but using a file pointer that is already ready. Will not close the file pointer. )rbread) rcr r!r"r#r$r%r&r'r(r0r0r1from_fpsre) pathr r!r"r#r$r%r&r'r(r)c CsHt|d*} t| ||||||||| WdS1s:0YdS)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)openre) rfr r!r"r#r$r%r&r'r(rcr0r0r1 from_paths ri) fp_or_path_or_payloadr r!r"r#r$r%r&r'r(r)c Cszt|ttfr,t|||||||||| d } nHt|ttfrXt|||||||||| d } nt|||||||||| 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()rBrVrrirDrCrbre) rjr r!r"r#r$r%r&r'r(Zguessesr0r0r1 is_binary3sX   rk) rrrNNTFrT) rrrNNTFrT) rrrNNTFrT) rrrNNTFrF)0rMosrtypingrrrrrZcdr r r r Zconstantr rrrZmdrZmodelsrrZutilsrrrrrrrZ getLoggerrHZ StreamHandlerrJZ setFormatterZ FormatterrDrCrPfloatrVboolrbrerirkr0r0r0r1s  $     Z       !