o
    ­}”eh-  ã                   @   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
mZmZmZ ddl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 )é    )Úaliases)Úsha256)Údumps)ÚAnyÚDictÚIteratorÚListÚOptionalÚTupleÚUnioné   )ÚTOO_BIG_SEQUENCE)Ú	iana_nameÚis_multi_byte_encodingÚunicode_rangec                   @   sÚ  e Zd Z	d=dededededddee fd	d
„Zde	defdd„Z
de	defdd„Zedefdd„ƒZdefdd„Zdefdd„Zd>dd„Zedefdd„ƒZedee fdd„ƒZedefdd„ƒZedefdd „ƒZedee fd!d"„ƒZedefd#d$„ƒZedefd%d&„ƒZedefd'd(„ƒZedefd)d*„ƒZedefd+d,„ƒZedefd-d.„ƒZeded  fd/d0„ƒZedefd1d2„ƒZedee fd3d4„ƒZedee fd5d6„ƒZ d?d8edefd9d:„Z!edefd;d<„ƒZ"dS )@ÚCharsetMatchNÚpayloadÚguessed_encodingÚmean_mess_ratioÚhas_sig_or_bomÚ	languagesÚCoherenceMatchesÚdecoded_payloadc                 C   sF   || _ || _|| _|| _|| _d | _g | _d| _d | _d | _	|| _
d S )Nç        )Ú_payloadÚ	_encodingÚ_mean_mess_ratioÚ
_languagesÚ_has_sig_or_bomÚ_unicode_rangesÚ_leavesZ_mean_coherence_ratioÚ_output_payloadÚ_output_encodingÚ_string)Úselfr   r   r   r   r   r   © r%   úb/Users/abhinavbhinder/Documents/map/venv/lib/python3.10/site-packages/charset_normalizer/models.pyÚ__init__   s   	
zCharsetMatch.__init__ÚotherÚreturnc                 C   s>   t |tƒstd t|jƒt| jƒ¡ƒ‚| j|jko| j|jkS )Nz&__eq__ cannot be invoked on {} and {}.)Ú
isinstancer   Ú	TypeErrorÚformatÚstrÚ	__class__Ú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áz„?g{®Gáz”?)
r*   r   Ú
ValueErrorÚabsÚchaosÚ	coherenceÚlenr   r   Úmulti_byte_usage)r$   r(   Zchaos_differenceZcoherence_differencer%   r%   r&   Ú__lt__-   s   
zCharsetMatch.__lt__c                 C   s   dt t| ƒƒt | jƒ  S )Ng      ð?)r7   r-   Úraw©r$   r%   r%   r&   r8   C   s   zCharsetMatch.multi_byte_usagec                 C   s"   | j d u rt| j| jdƒ| _ | j S )NÚstrict)r#   r-   r   r   r;   r%   r%   r&   Ú__str__G   s   
zCharsetMatch.__str__c                 C   s   d  | j| j¡S )Nz<CharsetMatch '{}' bytes({})>)r,   r/   r0   r;   r%   r%   r&   Ú__repr__M   ó   zCharsetMatch.__repr__c                 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)r*   r   r3   r,   r.   r#   r    Úappendr1   r%   r%   r&   Úadd_submatchP   s   ÿÿzCharsetMatch.add_submatchc                 C   ó   | j S ©N)r   r;   r%   r%   r&   r/   [   ó   zCharsetMatch.encodingc                 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   Úitemsr/   r@   )r$   Zalso_known_asÚuÚpr%   r%   r&   Úencoding_aliases_   s   


€zCharsetMatch.encoding_aliasesc                 C   rB   rC   ©r   r;   r%   r%   r&   Úboml   rD   zCharsetMatch.bomc                 C   rB   rC   rI   r;   r%   r%   r&   Úbyte_order_markp   rD   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   ó    z*CharsetMatch.languages.<locals>.<listcomp>©r   r;   r%   r%   r&   r   t   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".
        ÚasciiZEnglishr   )Úencoding_languagesÚmb_encoding_languageszLatin BasedÚUnknown)r   Úcould_be_from_charsetZcharset_normalizer.cdrR   rS   r   r/   r7   )r$   rR   rS   r   r%   r%   r&   Úlanguage|   s   
ÿýzCharsetMatch.languagec                 C   rB   rC   )r   r;   r%   r%   r&   r5   ˜   rD   zCharsetMatch.chaosc                 C   s   | j sdS | j d d S )Nr   r   r   rP   r;   r%   r%   r&   r6   œ   s   zCharsetMatch.coherencec                 C   ó   t | jd dd�S ©Néd   é   )Úndigits)Úroundr5   r;   r%   r%   r&   Úpercent_chaos¢   ó   zCharsetMatch.percent_chaosc                 C   rW   rX   )r\   r6   r;   r%   r%   r&   Úpercent_coherence¦   r^   zCharsetMatch.percent_coherencec                 C   rB   )z+
        Original untouched bytes.
        )r   r;   r%   r%   r&   r:   ª   s   zCharsetMatch.rawc                 C   rB   rC   )r    r;   r%   r%   r&   Úsubmatch±   rD   zCharsetMatch.submatchc                 C   ó   t | jƒdkS ©Nr   )r7   r    r;   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   )rL   Úcharr%   r%   r&   rN   ¾   s    ÿz*CharsetMatch.alphabets.<locals>.<listcomp>c                 S   s   h | ]}|r|’qS r%   r%   )rL   Úrr%   r%   r&   Ú	<setcomp>Â   rO   z)CharsetMatch.alphabets.<locals>.<setcomp>)r   r-   ÚsortedÚlist)r$   Z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%   )r/   )rL   Úmr%   r%   r&   rN   Ì   s    z6CharsetMatch.could_be_from_charset.<locals>.<listcomp>)r   r    r;   r%   r%   r&   rU   Å   s   z"CharsetMatch.could_be_from_charsetÚutf_8r/   c                 C   s2   | j du s
| j |kr|| _ t| ƒ |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Úreplace)r"   r-   Úencoder!   )r$   r/   r%   r%   r&   ÚoutputÎ   s   zCharsetMatch.outputc                 C   s   t |  ¡ ƒ ¡ S )zw
        Retrieve the unique SHA256 computed using the transformed (re-encoded) payload. Not the original one.
        )r   rn   Ú	hexdigestr;   r%   r%   r&   r0   Ù   s   zCharsetMatch.fingerprintrC   )r(   r   r)   N)rk   )#Ú__name__Ú
__module__Ú__qualname__Úbytesr-   ÚfloatÚboolr	   r'   Úobjectr2   r9   Úpropertyr8   r=   r>   rA   r/   r   rH   rJ   rK   r   rV   r5   r6   r]   r_   r:   r`   rc   ri   rU   rn   r0   r%   r%   r%   r&   r   
   sn    ùþýüûú
ù	
r   c                   @   s¦   e Zd ZdZddeee  fdd„Zdee fdd„Z	d	e
eef defd
d„Zdefdd„Zdefdd„Zd	eddfdd„Zded fdd„Zded f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Úresultsc                 C   s   |r	t |ƒ| _d S g | _d S rC   )rg   Ú_results)r$   ry   r%   r%   r&   r'   ç   s   zCharsetMatches.__init__r)   c                 c   s   � | j E d H  d S rC   ©rz   r;   r%   r%   r&   Ú__iter__ê   s   €zCharsetMatches.__iter__Úitemc                 C   sJ   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)r*   Úintrz   r-   r   rU   ÚKeyError)r$   r}   Úresultr%   r%   r&   Ú__getitem__í   s   





ÿzCharsetMatches.__getitem__c                 C   s
   t | jƒS rC   ©r7   rz   r;   r%   r%   r&   Ú__len__û   s   
zCharsetMatches.__len__c                 C   ra   rb   r‚   r;   r%   r%   r&   Ú__bool__þ   s   zCharsetMatches.__bool__c                 C   s|   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	 |¡ t| j	ƒ| _	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 CharsetMatchesN)r*   r   r3   r,   r-   r.   r7   r:   r   rz   r0   r5   rA   r@   rg   )r$   r}   Úmatchr%   r%   r&   r@     s   
ÿÿ

€zCharsetMatches.appendr   c                 C   s   | j sdS | j d S )zQ
        Simply return the first match. Strict equivalent to matches[0].
        Nr   r{   r;   r%   r%   r&   Úbest  s   
zCharsetMatches.bestc                 C   s   |   ¡ S )zP
        Redundant method, call the method best(). Kept for BC reasons.
        )r†   r;   r%   r%   r&   Úfirst  s   zCharsetMatches.firstrC   )rp   rq   rr   Ú__doc__r	   r   r   r'   r   r|   r   r~   r-   r�   rƒ   ru   r„   r@   r†   r‡   r%   r%   r%   r&   rx   á   s    rx   c                   @   s~   e Zd Zdedee dee dee dedee deded	ed
ee defdd„Ze	de
eef fdd„ƒZdefdd„ZdS )ÚCliDetectionResultÚpathr/   rH   Úalternative_encodingsrV   ri   r   r5   r6   Úunicode_pathÚis_preferredc                 C   sF   || _ |
| _|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
d S rC   )rŠ   rŒ   r/   rH   r‹   rV   ri   r   r5   r6   r�   )r$   rŠ   r/   rH   r‹   rV   ri   r   r5   r6   rŒ   r�   r%   r%   r&   r'   )  s   
zCliDetectionResult.__init__r)   c                 C   s2   | j | j| j| j| j| j| j| j| j| j	| j
dœS )N©rŠ   r/   rH   r‹   rV   ri   r   r5   r6   rŒ   r�   rŽ   r;   r%   r%   r&   Ú__dict__C  s   õzCliDetectionResult.__dict__c                 C   s   t | jddd�S )NTé   )Úensure_asciiÚindent)r   r�   r;   r%   r%   r&   Úto_jsonS  r?   zCliDetectionResult.to_jsonN)rp   rq   rr   r-   r	   r   ru   rt   r'   rw   r   r   r�   r“   r%   r%   r%   r&   r‰   (  s6    þýüûúùø	÷
öõ
ôr‰   N)Zencodings.aliasesr   Úhashlibr   Újsonr   Útypingr   r   r   r   r	   r
   r   Zconstantr   Úutilsr   r   r   r   rx   r-   rt   ZCoherenceMatchr   r‰   r%   r%   r%   r&   Ú<module>   s    $ XC