o {nhj0@sddlmZddlmZddlmZddlmZddlm Z ddl m Z m Z m Z mZddlmZmZdd lmZmZmZGd d d ZGd d d ZeeefZe eZGdddZdS)) annotations)aliases)sha256)dumps)sub)AnyIteratorListTuple)RE_POSSIBLE_ENCODING_INDICATIONTOO_BIG_SEQUENCE) iana_nameis_multi_byte_encoding unicode_rangec@sHeZdZ  dGdHddZdIddZdIddZedJddZdKddZdKddZ dLdd Z edKd!d"Z edMd$d%Z edNd&d'Z edNd(d)ZedMd*d+ZedKd,d-ZedJd.d/ZedJd0d1ZedJd2d3ZedJd4d5ZedOd6d7ZedPd9d:ZedNd;d<ZedMd=d>ZedMd?d@ZdQdRdCdDZedKdEdFZdS)S CharsetMatchNpayloadbytesguessed_encodingstrmean_mess_ratiofloathas_sig_or_bombool languagesCoherenceMatchesdecoded_payload str | Nonepreemptive_declarationcCsL||_||_||_||_||_d|_g|_d|_d|_d|_ ||_ ||_ dS)N) _payload _encoding_mean_mess_ratio _languages_has_sig_or_bom_unicode_ranges_leaves_mean_coherence_ratio_output_payload_output_encoding_string_preemptive_declaration)selfrrrrrrrr-D/usr/local/lib/python3.10/dist-packages/charset_normalizer/models.py__init__s  zCharsetMatch.__init__otherobjectreturncCs>t|tst|trt||jkSdS|j|jko|j|jkS)NF) isinstancerrrencoding fingerprintr,r0r-r-r.__eq__*s  zCharsetMatch.__eq__cCst|tstt|j|j}t|j|j}|dkr%|dkr%|j|jkS|dkr@|dkr@t|jtkr:|j|jkS|j |j kS|j|jkS)zQ Implemented to make sorted available upon CharsetMatches items. g{Gz?g{Gz?) r3r ValueErrorabschaos coherencelenr r multi_byte_usage)r,r0chaos_differencecoherence_differencer-r-r.__lt__1s     zCharsetMatch.__lt__cCsdtt|t|jS)Ng?)r<rrawr,r-r-r.r=GszCharsetMatch.multi_byte_usagecCs"|jdurt|j|jd|_|jS)Nstrict)r*rr r!rBr-r-r.__str__Ks zCharsetMatch.__str__cCsd|jd|jdS)Nz)r4r5rBr-r-r.__repr__QszCharsetMatch.__repr__NonecCs8t|tr ||krtd|jd|_|j|dS)Nz;Unable to add instance <{}> as a submatch of a CharsetMatch)r3rr8format __class__r*r&appendr6r-r-r. add_submatchTszCharsetMatch.add_submatchcC|jSN)r!rBr-r-r.r4_zCharsetMatch.encoding list[str]cCsDg}tD]\}}|j|kr||q|j|kr||q|S)z Encoding name are known by many name, using this could help when searching for IBM855 when it's listed as CP855. )ritemsr4rI)r, also_known_asupr-r-r.encoding_aliasescs    zCharsetMatch.encoding_aliasescCrKrLr$rBr-r-r.bomprMzCharsetMatch.bomcCrKrLrTrBr-r-r.byte_order_marktrMzCharsetMatch.byte_order_markcCsdd|jDS)z Return the complete list of possible languages found in decoded sequence. Usually not really useful. Returned list may be empty even if 'language' property return something != 'Unknown'. cSsg|]}|dqS)rr-).0er-r-r. ~z*CharsetMatch.languages..r#rBr-r-r.rxszCharsetMatch.languagescCsp|js1d|jvr dSddlm}m}t|jr||jn||j}t|dks+d|vr-dS|dS|jddS)z Most probable language found in decoded sequence. If none were detected or inferred, the property will return "Unknown". asciiEnglishr)encoding_languagesmb_encoding_languagesz Latin BasedUnknown)r#could_be_from_charsetcharset_normalizer.cdr^r_rr4r<)r,r^r_rr-r-r.languages  zCharsetMatch.languagecCrKrL)r"rBr-r-r.r:rMzCharsetMatch.chaoscCs|jsdS|jddS)Nrrr r[rBr-r-r.r;szCharsetMatch.coherencecCt|jdddSNd)ndigits)roundr:rBr-r-r. percent_chaoszCharsetMatch.percent_chaoscCrdre)rir;rBr-r-r.percent_coherencerkzCharsetMatch.percent_coherencecCrK)z+ Original untouched bytes. )r rBr-r-r.rAszCharsetMatch.rawlist[CharsetMatch]cCrKrL)r&rBr-r-r.submatchrMzCharsetMatch.submatchcCt|jdkSNr)r<r&rBr-r-r. has_submatchszCharsetMatch.has_submatchcCs@|jdur|jSddt|D}ttdd|D|_|jS)NcSsg|]}t|qSr-)r)rWcharr-r-r.rYrZz*CharsetMatch.alphabets..cSsh|]}|r|qSr-r-)rWrr-r-r. rZz)CharsetMatch.alphabets..)r%rsortedlist)r,detected_rangesr-r-r. alphabetss zCharsetMatch.alphabetscCs|jgdd|jDS)z The complete list of encoding that output the exact SAME str result and therefore could be the originating encoding. This list does include the encoding available in property 'encoding'. cSsg|]}|jqSr-)r4)rWmr-r-r.rYsz6CharsetMatch.could_be_from_charset..)r!r&rBr-r-r.rasz"CharsetMatch.could_be_from_charsetutf_8r4cs~jdus j|kr<|_t}jdur5jdvr5ttfdd|dddd}||dd}||d_jS) z Method to get re-encoded bytes payload using given target encoding. Default to UTF-8. Any errors will be simply ignored by the encoder NOT replaced. N)zutf-8utf8rzcs<|j|d|d|dtjddS)Nrr _-)stringspanreplacegroupsrr))ryrBr-r.s z%CharsetMatch.output..i r )countr)r)rr+lowerrr encoder()r,r4decoded_stringpatched_headerr-rBr.outputs     zCharsetMatch.outputcCst|S)zw Retrieve the unique SHA256 computed using the transformed (re-encoded) payload. Not the original one. )rr hexdigestrBr-r-r.r5szCharsetMatch.fingerprint)NN)rrrrrrrrrrrrrr)r0r1r2r)r2rr2r)r0rr2rF)r2rNr2r)r2r)r2rm)rz)r4rr2r)__name__ __module__ __qualname__r/r7r@propertyr=rDrErJr4rSrUrVrrcr:r;rjrlrArnrqrxrarr5r-r-r-r.r sX                      rc@sbeZdZdZddddZd d d Zd!ddZd"ddZd#ddZd$ddZ d%ddZ d%ddZ dS)&CharsetMatchesz Container with every CharsetMatch items ordered by default from most probable to the less one. Act like a list(iterable) but does not implements all related methods. Nresultslist[CharsetMatch] | NonecCs|r t||_dSg|_dSrL)ru_results)r,rr-r-r.r/szCharsetMatches.__init__r2Iterator[CharsetMatch]ccs|jEdHdSrLrrBr-r-r.__iter__szCharsetMatches.__iter__item int | strrcCsJt|tr |j|St|tr#t|d}|jD] }||jvr"|Sqt)z Retrieve a single item either by its position or encoding name (alias may be used here). Raise KeyError upon invalid index or encoding not present in results. F)r3intrrrraKeyError)r,rresultr-r-r. __getitem__s      zCharsetMatches.__getitem__rcCs t|jSrLr<rrBr-r-r.__len__s zCharsetMatches.__len__rcCrorprrBr-r-r.__bool__szCharsetMatches.__bool__rFcCs|t|tstdt|jt|jtkr0|j D]}|j |j kr/|j |j kr/| |dSq|j |t|j |_ dS)z~ Insert a single match. Will be inserted accordingly to preserve sort. Can be inserted as a submatch. z-Cannot append instance '{}' to CharsetMatchesN)r3rr8rGrrHr<rAr rr5r:rJrIru)r,rmatchr-r-r.rIs    zCharsetMatches.appendCharsetMatch | NonecCs|jsdS|jdS)zQ Simply return the first match. Strict equivalent to matches[0]. NrrrBr-r-r.best)s zCharsetMatches.bestcCs|S)zP Redundant method, call the method best(). Kept for BC reasons. )rrBr-r-r.first1szCharsetMatches.firstrL)rr)r2r)rrr2r)r2rr)rrr2rF)r2r) rrr__doc__r/rrrrrIrrr-r-r-r.rs       rc@s.eZdZdddZedddZdddZdS)CliDetectionResultpathrr4rrSrNalternative_encodingsrcrxrrr:rr; unicode_path is_preferredc CsF||_| |_||_||_||_||_||_||_||_| |_ | |_ dSrL) rrr4rSrrcrxrr:r;r) r,rr4rSrrcrxrr:r;rrr-r-r.r/=s zCliDetectionResult.__init__r2dict[str, Any]c Cs2|j|j|j|j|j|j|j|j|j|j |j d S)N rr4rSrrcrxrr:r;rrrrBr-r-r.__dict__WszCliDetectionResult.__dict__cCst|jdddS)NT) ensure_asciiindent)rrrBr-r-r.to_jsongszCliDetectionResult.to_jsonN)rrr4rrSrNrrNrcrrxrNrrr:rr;rrrrr)r2rr)rrrr/rrrr-r-r-r.r<s   rN) __future__rencodings.aliasesrhashlibrjsonrrertypingrrr r constantr r utilsrrrrrrrCoherenceMatchrrr-r-r-r.s    i C