a
    -3e,                     @   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d8eeeedee dddZe	edddZ
e	edd	d
ZeedddZedddZedddZd ddddZeedddZeee dddZeedddZeedddZeee dddZeedddZeedd d!Zeedd"d#Zeedd$d%Zeedd&d'Zeedd(d)Zeed  dd*d+Zeedd,d-Zeee dd.d/Zeee dd0d1Z d9eed3d4d5Z!eedd6d7Z"dS ):CharsetMatchNCoherenceMatches)payloadguessed_encodingmean_mess_ratiohas_sig_or_bom	languages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%   L/home/pi/bot/my_env/lib/python3.9/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   sr   t |tstt| j|j }t| j|j }|dk rJ|dkrJ| j|jkS |dk rf|dkrf| j|jkS | j|jk S )zQ
        Implemented to make sorted available upon CharsetMatches items.
        g{Gz?g{Gz?)r*   r   
ValueErrorabschaos	coherencemulti_byte_usage)r$   r(   Zchaos_differenceZcoherence_differencer%   r%   r&   __lt__-   s    
zCharsetMatch.__lt__r)   c                 C   s   dt t| t | j  S )Ng      ?)lenr-   rawr$   r%   r%   r&   r7   @   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__D   s    
zCharsetMatch.__str__c                 C   s   d | j| jS )Nz<CharsetMatch '{}' bytes({})>)r,   r/   r0   r<   r%   r%   r&   __repr__J   s    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_submatchM   s    zCharsetMatch.add_submatchc                 C   s   | j S N)r   r<   r%   r%   r&   r/   X   s    zCharsetMatch.encodingc                 C   sD   g }t  D ]2\}}| 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   s   | j S rB   r   r<   r%   r%   r&   bomi   s    zCharsetMatch.bomc                 C   s   | j S rB   rG   r<   r%   r%   r&   byte_order_markm   s    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>w       z*CharsetMatch.languages.<locals>.<listcomp>r   r<   r%   r%   r&   r   q   s    zCharsetMatch.languagesc                 C   sp   | j sbd| jv rdS ddlm}m} t| jr8|| jn|| j}t|dksVd|v rZd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.cdrP   rQ   r   r/   r:   )r$   rP   rQ   r   r%   r%   r&   languagey   s    
zCharsetMatch.languagec                 C   s   | j S rB   )r   r<   r%   r%   r&   r5      s    zCharsetMatch.chaosc                 C   s   | j s
dS | j d d S )Nr   r   r   rN   r<   r%   r%   r&   r6      s    zCharsetMatch.coherencec                 C   s   t | jd ddS Nd      )ndigits)roundr5   r<   r%   r%   r&   percent_chaos   s    zCharsetMatch.percent_chaosc                 C   s   t | jd ddS rU   )rY   r6   r<   r%   r%   r&   percent_coherence   s    zCharsetMatch.percent_coherencec                 C   s   | j S )z+
        Original untouched bytes.
        )r   r<   r%   r%   r&   r;      s    zCharsetMatch.rawc                 C   s   | j S rB   )r    r<   r%   r%   r&   submatch   s    zCharsetMatch.submatchc                 C   s   t | jdkS Nr   )r:   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   )rJ   charr%   r%   r&   rL      s   z*CharsetMatch.alphabets.<locals>.<listcomp>c                 S   s   h | ]}|r|qS r%   r%   )rJ   rr%   r%   r&   	<setcomp>   rM   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/   )rJ   mr%   r%   r&   rL      rM   z6CharsetMatch.could_be_from_charset.<locals>.<listcomp>)r   r    r<   r%   r%   r&   rS      s    z"CharsetMatch.could_be_from_charsetutf_8)r/   r)   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   ri   	hexdigestr<   r%   r%   r&   r0      s    zCharsetMatch.fingerprint)N)rf   )#__name__
__module____qualname__bytesr-   floatboolr	   r'   objectr2   r8   propertyr7   r>   r?   rA   r/   r   rF   rH   rI   r   rT   r5   r6   rZ   r[   r;   r\   r^   rd   rS   ri   r0   r%   r%   r%   r&   r   
   sb    	r   c                   @   s   e Zd ZdZdeee  dddZee dddZ	e
eef ed	d
dZedddZedddZedd	ddZed dddZed 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)resultsc                 C   s   |rt |ng | _d S rB   )rb   _results)r$   rt   r%   r%   r&   r'      s    zCharsetMatches.__init__r9   c                 c   s   | j E d H  d S rB   ru   r<   r%   r%   r&   __iter__   s    zCharsetMatches.__iter__)itemr)   c                 C   sN   t |tr| j| S t |trFt|d}| jD ]}||jv r.|  S q.tdS )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.
        FN)r*   intru   r-   r   rS   KeyError)r$   rx   resultr%   r%   r&   __getitem__   s    






zCharsetMatches.__getitem__c                 C   s
   t | jS rB   r:   ru   r<   r%   r%   r&   __len__   s    zCharsetMatches.__len__c                 C   s   t | jdkS r]   r}   r<   r%   r%   r&   __bool__   s    zCharsetMatches.__bool__c                 C   s|   t |tstdt|jt|jtkr`| j	D ],}|j
|j
kr2|j|jkr2||  dS q2| 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.   r:   r;   r   ru   r0   r5   rA   r@   rb   )r$   rx   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   rv   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.first)N)rk   rl   rm   __doc__r	   r   r   r'   r   rw   r   ry   r-   r|   r~   rp   r   r@   r   r   r%   r%   r%   r&   rs      s   rs   c                   @   sj   e Zd Zeee ee ee eee eeeee edddZe	e
eef dddZedddZd	S )
CliDetectionResultpathr/   rF   alternative_encodingsrT   rd   r   r5   r6   unicode_pathis_preferredc                 C   sF   || _ |
| _|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
d S rB   )r   r   r/   rF   r   rT   rd   r   r5   r6   r   )r$   r   r/   rF   r   rT   rd   r   r5   r6   r   r   r%   r%   r&   r'   &  s    zCliDetectionResult.__init__r9   c                 C   s2   | j | j| j| j| j| j| j| j| j| j	| j
dS )Nr   r   r<   r%   r%   r&   __dict__@  s    zCliDetectionResult.__dict__c                 C   s   t | jdddS )NT   )ensure_asciiindent)r   r   r<   r%   r%   r&   to_jsonP  s    zCliDetectionResult.to_jsonN)rk   rl   rm   r-   r	   r   rp   ro   r'   rr   r   r   r   r   r%   r%   r%   r&   r   %  s   r   N)Zencodings.aliasesr   hashlibr   jsonr   typingr   r   r   r   r	   r
   r   Zconstantr   utilsr   r   r   r   rs   r-   ro   ZCoherenceMatchr   r   r%   r%   r%   r&   <module>   s   $ UC