U
    ڲgj0                     @  s   d dl mZ d dlmZ 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)sha256)dumps)sub)AnyIteratorListTuple   )RE_POSSIBLE_ENCODING_INDICATIONTOO_BIG_SEQUENCE)	iana_nameis_multi_byte_encodingunicode_rangec                	   @  s  e Zd ZdAddddddddd	d
ZdddddZdddddZeddddZddddZddddZ	d ddddZ
eddddZeddddZeddd d!Zeddd"d#Zeddd$d%Zeddd&d'Zeddd(d)Zeddd*d+Zeddd,d-Zeddd.d/Zeddd0d1Zed2dd3d4Zeddd5d6Zeddd7d8Zeddd9d:ZdBddd<d=d>Zeddd?d@ZdS )CCharsetMatchNbytesstrfloatboolCoherenceMatches
str | None)payloadguessed_encodingmean_mess_ratiohas_sig_or_bom	languagesdecoded_payloadpreemptive_declarationc                 C  sL   || _ || _|| _|| _|| _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_preemptive_declaration)selfr   r   r   r   r   r   r    r,   =/tmp/pip-unpacked-wheel-y7coycdt/charset_normalizer/models.py__init__   s    
zCharsetMatch.__init__object)otherreturnc                 C  s>   t |ts&t |tr"t|| jkS dS | j|jko<| j|jkS )NF)
isinstancer   r   r   encodingfingerprintr+   r0   r,   r,   r-   __eq__*   s
    

zCharsetMatch.__eq__c                 C  s   t |tstt| j|j }t| j|j }|dk rJ|dkrJ| j|jkS |dk r|dkrt| jtkrt| j|jk S | j	|j	kS | j|jk S )zQ
        Implemented to make sorted available upon CharsetMatches items.
        g{Gz?g{Gz?)
r2   r   
ValueErrorabschaos	coherencelenr    r   multi_byte_usage)r+   r0   Zchaos_differenceZcoherence_differencer,   r,   r-   __lt__1   s    
zCharsetMatch.__lt__r1   c                 C  s   dt t| t | j  S )Ng      ?)r;   r   rawr+   r,   r,   r-   r<   G   s    zCharsetMatch.multi_byte_usagec                 C  s"   | j d krt| j| jd| _ | j S )Nstrict)r)   r   r    r!   r@   r,   r,   r-   __str__K   s    
zCharsetMatch.__str__c                 C  s   d| j  d| j dS )Nz<CharsetMatch 'z' bytes(z)>)r3   r4   r@   r,   r,   r-   __repr__Q   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)r2   r   r7   format	__class__r)   r&   appendr5   r,   r,   r-   add_submatchT   s    zCharsetMatch.add_submatchc                 C  s   | j S N)r!   r@   r,   r,   r-   r3   _   s    zCharsetMatch.encoding	list[str]c                 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   itemsr3   rG   )r+   Zalso_known_asupr,   r,   r-   encoding_aliasesc   s    

zCharsetMatch.encoding_aliasesc                 C  s   | j S rI   r$   r@   r,   r,   r-   bomp   s    zCharsetMatch.bomc                 C  s   | j S rI   rO   r@   r,   r,   r-   byte_order_markt   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>~   s     z*CharsetMatch.languages.<locals>.<listcomp>r#   r@   r,   r,   r-   r   x   s    zCharsetMatch.languagesc                 C  sp   | j sbd| jkrdS ddlm}m} t| jr8|| jn|| j}t|dksVd|k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.cdrW   rX   r   r3   r;   )r+   rW   rX   r   r,   r,   r-   language   s    
zCharsetMatch.languagec                 C  s   | j S rI   )r"   r@   r,   r,   r-   r9      s    zCharsetMatch.chaosc                 C  s   | j s
dS | j d d S )Nr   r   r   rU   r@   r,   r,   r-   r:      s    zCharsetMatch.coherencec                 C  s   t | jd ddS Nd      )ndigits)roundr9   r@   r,   r,   r-   percent_chaos   s    zCharsetMatch.percent_chaosc                 C  s   t | jd ddS r\   )r`   r:   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.rawzlist[CharsetMatch]c                 C  s   | j S rI   )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 k	r| j S dd t| D }ttdd |D | _ | j S )Nc                 S  s   g | ]}t |qS r,   )r   )rR   charr,   r,   r-   rT      s     z*CharsetMatch.alphabets.<locals>.<listcomp>c                 S  s   h | ]}|r|qS r,   r,   )rR   rr,   r,   r-   	<setcomp>   s      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,   )r3   )rR   mr,   r,   r-   rT      s     z6CharsetMatch.could_be_from_charset.<locals>.<listcomp>)r!   r&   r@   r,   r,   r-   rZ      s    z"CharsetMatch.could_be_from_charsetutf_8)r3   r1   c                   s~    j dks j |krx| _ t } jdk	rj j dkrjt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utf8rm   c                   s<   | j |  d |  d  |  d t jddS )Nr   r   _-)stringspanreplacegroupsr   r(   )rl   r@   r,   r-   <lambda>   s   
z%CharsetMatch.output.<locals>.<lambda>i    r   )countrs   )r(   r   r*   lowerr   r   encoder'   )r+   r3   decoded_stringZpatched_headerr,   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   rz   	hexdigestr@   r,   r,   r-   r4      s    zCharsetMatch.fingerprint)NN)rm   )__name__
__module____qualname__r.   r6   r=   propertyr<   rB   rC   rH   r3   rN   rP   rQ   r   r[   r9   r:   ra   rb   r?   rc   re   rk   rZ   rz   r4   r,   r,   r,   r-   r      sV     	r   c                   @  s   e Zd ZdZdddddZddd	d
ZdddddZddddZddddZdddddZ	ddddZ
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.
    Nzlist[CharsetMatch] | None)resultsc                 C  s   |rt |ng | _d S rI   )ri   _results)r+   r   r,   r,   r-   r.      s    zCharsetMatches.__init__zIterator[CharsetMatch]r>   c                 c  s   | j E d H  d S rI   r   r@   r,   r,   r-   __iter__   s    zCharsetMatches.__iter__z	int | strr   )itemr1   c                 C  sN   t |tr| j| S t |trFt|d}| jD ]}||jk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)r2   intr   r   r   rZ   KeyError)r+   r   resultr,   r,   r-   __getitem__  s    






zCharsetMatches.__getitem__r   c                 C  s
   t | jS rI   r;   r   r@   r,   r,   r-   __len__  s    zCharsetMatches.__len__r   c                 C  s   t | jdkS rd   r   r@   r,   r,   r-   __bool__  s    zCharsetMatches.__bool__rD   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)r2   r   r7   rE   r   rF   r;   r?   r   r   r4   r9   rH   rG   ri   )r+   r   matchr,   r,   r-   rG     s    


zCharsetMatches.appendz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@   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-   first1  s    zCharsetMatches.first)N)r|   r}   r~   __doc__r.   r   r   r   r   rG   r   r   r,   r,   r,   r-   r      s   r   c                   @  sN   e Zd ZddddddddddddddZed	d
ddZdd
ddZdS )CliDetectionResultr   r   rJ   r   r   pathr3   rN   alternative_encodingsr[   rk   r   r9   r:   unicode_pathis_preferredc                 C  sF   || _ |
| _|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
d S rI   )r   r   r3   rN   r   r[   rk   r   r9   r:   r   )r+   r   r3   rN   r   r[   rk   r   r9   r:   r   r   r,   r,   r-   r.   =  s    zCliDetectionResult.__init__zdict[str, Any]r>   c                 C  s2   | j | j| j| j| j| j| j| j| j| j	| j
dS )Nr   r   r@   r,   r,   r-   __dict__W  s    zCliDetectionResult.__dict__c                 C  s   t | jdddS )NT   )ensure_asciiindent)r   r   r@   r,   r,   r-   to_jsong  s    zCliDetectionResult.to_jsonN)r|   r}   r~   r.   r   r   r   r,   r,   r,   r-   r   <  s   "r   N)
__future__r   Zencodings.aliasesr   hashlibr   jsonr   rer   typingr   r   r	   r
   Zconstantr   r   utilsr   r   r   r   r   r   r   ZCoherenceMatchr   r   r,   r,   r,   r-   <module>   s    iC