o
    ßý°jÚ4  ã                   @  s¢   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 ddlmZmZmZ G dd	„ d	ƒZG d
d„ dƒZe
eef Ze	e ZG dd„ dƒZdS )é    )Úannotations)Úaliases)Úsub)ÚAnyÚIteratorÚListÚTupleé   )ÚRE_POSSIBLE_ENCODING_INDICATIONÚTOO_BIG_SEQUENCE)Ú	iana_nameÚis_multi_byte_encodingÚunicode_rangec                   @  sH  e Zd Z		dIdJdd„ZdKdd„ZdKdd„ZedLdd„ƒZdMdd„ZdMdd„Z	dNdd „Z
edMd!d"„ƒZedOd$d%„ƒZedPd&d'„ƒZedPd(d)„ƒZedOd*d+„ƒZedMd,d-„ƒZedLd.d/„ƒZedLd0d1„ƒZedLd2d3„ƒZedLd4d5„ƒZedQd6d7„ƒZedRd9d:„ƒZedPd;d<„ƒZedOd=d>„ƒZedOd?d@„ƒZdSdTdDdE„ZedUdGdH„ƒZdS )VÚCharsetMatchNÚpayloadúbytes | bytearrayÚguessed_encodingÚstrÚmean_mess_ratioÚfloatÚhas_sig_or_bomÚboolÚ	languagesÚCoherenceMatchesÚdecoded_payloadú
str | NoneÚpreemptive_declarationc                 C  sL   || _ || _|| _|| _|| _d | _g | _d| _d | _d | _	|| _
|| _d S )Nç        )Ú_payloadÚ	_encodingÚ_mean_mess_ratioÚ
_languagesÚ_has_sig_or_bomÚ_unicode_rangesÚ_leavesÚ_mean_coherence_ratioÚ_output_payloadÚ_output_encodingÚ_stringÚ_preemptive_declaration)Úselfr   r   r   r   r   r   r   © r+   úŒ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/charset_normalizer/models.pyÚ__init__   s   

zCharsetMatch.__init__ÚotherÚobjectÚreturnc                 C  s@   t |tƒst |tƒrt|dƒ| jkS dS | j|jko| j|jkS )NF)Ú
isinstancer   r   r   ÚencodingÚfingerprint©r*   r.   r+   r+   r,   Ú__eq__(   s
   

zCharsetMatch.__eq__c                 C  sŒ   t |tƒst‚t| j|j ƒ}t| j|j ƒ}|dk r%|dkr%| j|jkS |dk r@|dkr@t| jƒtkr:| j|jk S | j	|j	kS | j|jk S )zQ
        Implemented to make sorted available upon CharsetMatches items.
        g{®Gázt?g{®Gáz”?)
r1   r   Ú
ValueErrorÚabsÚchaosÚ	coherenceÚlenr   r   Úmulti_byte_usage)r*   r.   Úchaos_differenceÚcoherence_differencer+   r+   r,   Ú__lt__3   s   
zCharsetMatch.__lt__c                 C  s*   t | jƒ}|dkrdS dt t| ƒƒ|  S )Nr   r   g      ð?)r:   Úrawr   )r*   Úraw_lenr+   r+   r,   r;   I   s   
zCharsetMatch.multi_byte_usagec                 C  sV   | j d u r(t| j| jdƒ| _ | jr(| jdkr(| j r(| j d dkr(| j dd … | _ | j S )NÚstrictÚutf_7r   u   ï»¿r	   )r(   r   r   r   r"   ©r*   r+   r+   r,   Ú__str__Q   s   
ÿ
ÿzCharsetMatch.__str__c                 C  s   d| j › d| j› d�S )Nz<CharsetMatch 'z' fp(z)>)r2   r3   rC   r+   r+   r,   Ú__repr__a   s   zCharsetMatch.__repr__ÚNonec                 C  s8   t |tƒr	|| krtd |j¡ƒ‚d |_| j |¡ d S )Nz;Unable to add instance <{}> as a submatch of a CharsetMatch)r1   r   r6   ÚformatÚ	__class__r(   r$   Úappendr4   r+   r+   r,   Úadd_submatchd   s   ÿÿzCharsetMatch.add_submatchc                 C  ó   | j S ©N)r   rC   r+   r+   r,   r2   o   ó   zCharsetMatch.encodingú	list[str]c                 C  sD   g }t  ¡ D ]\}}| j|kr| |¡ q| j|kr| |¡ q|S )z‚
        Encoding name are known by many name, using this could help when searching for IBM855 when it's listed as CP855.
        )r   Úitemsr2   rI   )r*   Úalso_known_asÚuÚpr+   r+   r,   Úencoding_aliasess   s   


€zCharsetMatch.encoding_aliasesc                 C  rK   rL   ©r"   rC   r+   r+   r,   Úbom€   rM   zCharsetMatch.bomc                 C  rK   rL   rT   rC   r+   r+   r,   Úbyte_order_mark„   rM   zCharsetMatch.byte_order_markc                 C  s   dd„ | j D ƒS )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'.
        c                 S  s   g | ]}|d  ‘qS )r   r+   )Ú.0Úer+   r+   r,   Ú
<listcomp>Ž   ó    z*CharsetMatch.languages.<locals>.<listcomp>©r!   rC   r+   r+   r,   r   ˆ   s   zCharsetMatch.languagesc                 C  sp   | j s1d| jv r
dS ddlm}m} t| jƒr|| jƒn|| jƒ}t|ƒdks+d|v r-dS |d S | j d d S )z’
        Most probable language found in decoded sequence. If none were detected or inferred, the property will return
        "Unknown".
        ÚasciiÚEnglishr   )Úencoding_languagesÚmb_encoding_languageszLatin BasedÚUnknown)r!   Úcould_be_from_charsetÚcharset_normalizer.cdr^   r_   r   r2   r:   )r*   r^   r_   r   r+   r+   r,   Úlanguage�   s   
ÿýzCharsetMatch.languagec                 C  rK   rL   )r    rC   r+   r+   r,   r8   ¬   rM   zCharsetMatch.chaosc                 C  s   | j sdS | j d d S )Nr   r   r	   r[   rC   r+   r+   r,   r9   °   s   zCharsetMatch.coherencec                 C  ó   t | jd dd�S ©Néd   é   )Úndigits)Úroundr8   rC   r+   r+   r,   Úpercent_chaos¶   ó   zCharsetMatch.percent_chaosc                 C  rd   re   )ri   r9   rC   r+   r+   r,   Úpercent_coherenceº   rk   zCharsetMatch.percent_coherencec                 C  rK   )z+
        Original untouched bytes.
        )r   rC   r+   r+   r,   r?   ¾   s   zCharsetMatch.rawúlist[CharsetMatch]c                 C  rK   rL   )r$   rC   r+   r+   r,   ÚsubmatchÅ   rM   zCharsetMatch.submatchc                 C  ó   t | jƒdkS ©Nr   )r:   r$   rC   r+   r+   r,   Úhas_submatchÉ   s   zCharsetMatch.has_submatchc                 C  s@   | j d ur| j S dd„ t| ƒD ƒ}ttdd„ |D ƒƒƒ| _ | j S )Nc                 S  s   g | ]}t |ƒ‘qS r+   )r   )rW   Úcharr+   r+   r,   rY   Ò   rZ   z*CharsetMatch.alphabets.<locals>.<listcomp>c                 S  s   h | ]}|r|’qS r+   r+   )rW   Úrr+   r+   r,   Ú	<setcomp>Ô   rZ   z)CharsetMatch.alphabets.<locals>.<setcomp>)r#   r   ÚsortedÚlist)r*   Údetected_rangesr+   r+   r,   Ú	alphabetsÍ   s
   
zCharsetMatch.alphabetsc                 C  s   | j gdd„ | jD ƒ S )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'.
        c                 S  s   g | ]}|j ‘qS r+   )r2   )rW   Úmr+   r+   r,   rY   Þ   s    z6CharsetMatch.could_be_from_charset.<locals>.<listcomp>)r   r$   rC   r+   r+   r,   ra   ×   s   z"CharsetMatch.could_be_from_charsetÚutf_8r2   Úbytesc                   s~   ˆ j du s
ˆ j |kr<|ˆ _ tˆ ƒ}ˆ jdur5ˆ j ¡ dvr5tt‡ fdd„|dd… dd�}||dd…  }| |d¡ˆ _ˆ jS )	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)zutf-8Úutf8rz   c                   s<   | j |  ¡ d |  ¡ d …  |  ¡ d tˆ jƒ dd¡¡S )Nr   r	   Ú_ú-)ÚstringÚspanÚreplaceÚgroupsr   r'   )ry   rC   r+   r,   Ú<lambda>ï   s    
þz%CharsetMatch.output.<locals>.<lambda>i    r	   )Úcountr�   )r'   r   r)   Úlowerr   r
   Úencoder&   )r*   r2   Údecoded_stringÚpatched_headerr+   rC   r,   Úoutputà   s    
ÿ

ù
zCharsetMatch.outputÚintc                 C  s   t t| ƒƒS )z]
        Retrieve a hash fingerprint of the decoded payload, used for deduplication.
        )Úhashr   rC   r+   r+   r,   r3   ý   s   zCharsetMatch.fingerprint)NN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r.   r/   r0   r   )r0   r   ©r0   r   )r.   r   r0   rF   )r0   rN   ©r0   r   )r0   r   )r0   rm   )rz   )r2   r   r0   r{   ©r0   rŠ   )Ú__name__Ú
__module__Ú__qualname__r-   r5   r>   Úpropertyr;   rD   rE   rJ   r2   rS   rU   rV   r   rc   r8   r9   rj   rl   r?   rn   rq   rx   ra   r‰   r3   r+   r+   r+   r,   r      sX    ø




	r   c                   @  sl   e Zd ZdZd d!dd„Zd"d	d
„Zd#dd„Zd$dd„Zd%dd„Zd&dd„Z	d'dd„Z
d(dd„Zd(dd„ZdS ))Ú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.
    NÚresultsúlist[CharsetMatch] | Nonec                 C  s   |rt |ƒng | _d| _d S ©NT)ru   Ú_resultsÚ
_is_sorted)r*   r”   r+   r+   r,   r-     s   
zCharsetMatches.__init__r0   rF   c                 C  s   | j s| j ¡  d| _ d S d S r–   )r˜   r—   ÚsortrC   r+   r+   r,   Ú_ensure_sorted  s   

þzCharsetMatches._ensure_sortedúIterator[CharsetMatch]c                 c  s   � |   ¡  | jE d H  d S rL   )rš   r—   rC   r+   r+   r,   Ú__iter__  s   €zCharsetMatches.__iter__Úitemú	int | strr   c                 C  sR   t |tƒr|  ¡  | j| S t |tƒr't|dƒ}| jD ]}||jv r&|  S qt‚)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)r1   rŠ   rš   r—   r   r   ra   ÚKeyError)r*   r�   Úresultr+   r+   r,   Ú__getitem__  s   





ÿzCharsetMatches.__getitem__rŠ   c                 C  s
   t | jƒS rL   ©r:   r—   rC   r+   r+   r,   Ú__len__'  s   
zCharsetMatches.__len__r   c                 C  ro   rp   r¢   rC   r+   r+   r,   Ú__bool__*  s   zCharsetMatches.__bool__c                 C  sv   t |tƒstd t|jƒ¡ƒ‚t|jƒtk r0| j	D ]}|j
|j
kr/|j|jkr/| |¡  dS q| j	 |¡ d| _dS )z~
        Insert a single match. Will be inserted accordingly to preserve sort.
        Can be inserted as a submatch.
        z-Cannot append instance '{}' to CharsetMatchesNF)r1   r   r6   rG   r   rH   r:   r?   r   r—   r3   r8   rJ   rI   r˜   )r*   r�   Úmatchr+   r+   r,   rI   -  s   
ÿÿ

€
zCharsetMatches.appendúCharsetMatch | Nonec                 C  s   | j sdS |  ¡  | j d S )zQ
        Simply return the first match. Strict equivalent to matches[0].
        Nr   )r—   rš   rC   r+   r+   r,   ÚbestA  s   
zCharsetMatches.bestc                 C  s   |   ¡ S )zP
        Redundant method, call the method best(). Kept for BC reasons.
        )r§   rC   r+   r+   r,   ÚfirstJ  s   zCharsetMatches.firstrL   )r”   r•   )r0   rF   )r0   r›   )r�   rž   r0   r   rŽ   r�   )r�   r   r0   rF   )r0   r¦   )r�   r�   r‘   Ú__doc__r-   rš   rœ   r¡   r£   r¤   rI   r§   r¨   r+   r+   r+   r,   r“     s    






	r“   c                   @  s.   e Zd Zddd„Zeddd„ƒZddd„ZdS )ÚCliDetectionResultÚpathr   r2   r   rS   rN   Úalternative_encodingsrc   rx   r   r   r8   r   r9   Úunicode_pathÚis_preferredc                 C  sF   || _ |
| _|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
d S rL   )r«   r­   r2   rS   r¬   rc   rx   r   r8   r9   r®   )r*   r«   r2   rS   r¬   rc   rx   r   r8   r9   r­   r®   r+   r+   r,   r-   V  s   
zCliDetectionResult.__init__r0   údict[str, Any]c                 C  s2   | j | j| j| j| j| j| j| j| j| j	| j
dœS )N©r«   r2   rS   r¬   rc   rx   r   r8   r9   r­   r®   r°   rC   r+   r+   r,   Ú__dict__p  s   õzCliDetectionResult.__dict__c                 C  s   ddl m} || jddd�S )Nr   )ÚdumpsTé   )Úensure_asciiÚindent)Újsonr²   r±   )r*   r²   r+   r+   r,   Úto_json€  s   zCliDetectionResult.to_jsonN)r«   r   r2   r   rS   rN   r¬   rN   rc   r   rx   rN   r   r   r8   r   r9   r   r­   r   r®   r   )r0   r¯   rŒ   )r�   r�   r‘   r-   r’   r±   r·   r+   r+   r+   r,   rª   U  s
    
rª   N)Ú
__future__r   Úencodings.aliasesr   Úrer   Útypingr   r   r   r   Úconstantr
   r   Úutilsr   r   r   r   r“   r   r   ÚCoherenceMatchr   rª   r+   r+   r+   r,   Ú<module>   s     {L