U
    Á½„b÷3  ã                   @   sÚ   d dl 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
 d dlmZmZmZmZmZmZmZ dd	l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 )é    N)ÚCounter)Úaliases)Úsha256)Údumps)Úsub)ÚAnyÚDictÚIteratorÚListÚOptionalÚTupleÚUnioné   )ÚNOT_PRINTABLE_PATTERNÚTOO_BIG_SEQUENCE)Ú
mess_ratio)Ú	iana_nameÚis_multi_byte_encodingÚunicode_rangec                   @   s   e Zd ZdBe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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œd0d1„ƒZ!eedœd2d3„ƒZ"eee dœd4d5„ƒZ#eee dœd6d7„ƒZ$d dœd8d9„Z%d dœd:d;„Z&dCeed=œd>d?„Z'eedœd@dA„ƒZ(dS )DÚ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)   ú=/tmp/pip-unpacked-wheel-2ta4nrol/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   sv   t |tƒst‚t| j|j ƒ}t| j|j ƒ}|dk rj|dkrj|dkr^| j|j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.   r   Ú
ValueErrorÚabsÚchaosÚ	coherenceÚmulti_byte_usage)r(   r,   Zchaos_differenceZcoherence_differencer)   r)   r*   Ú__lt__1   s    
zCharsetMatch.__lt__©r-   c                 C   s   dt t| ƒƒt | jƒ  S )Nç      ð?)Úlenr1   Úraw©r(   r)   r)   r*   r;   D   s    zCharsetMatch.multi_byte_usagec                 C   s   t  dt¡ tt| ƒdƒS )z¶
        Check once again chaos in decoded text, except this time, with full content.
        Use with caution, this can be very slow.
        Notice: Will be removed in 3.0
        z=chaos_secondary_pass is deprecated and will be removed in 3.0r>   )ÚwarningsÚwarnÚDeprecationWarningr   r1   rA   r)   r)   r*   Úchaos_secondary_passH   s
    þz!CharsetMatch.chaos_secondary_passc                 C   s   t  dt¡ dS )zy
        Coherence ratio on the first non-latin language detected if ANY.
        Notice: Will be removed in 3.0
        z<coherence_non_latin is deprecated and will be removed in 3.0r   )rB   rC   rD   rA   r)   r)   r*   Úcoherence_non_latinU   s
    þz CharsetMatch.coherence_non_latinc                 C   s,   t  dt¡ ttdt| ƒ ¡ ƒ}t| ¡ ƒS )z_
        Word counter instance on decoded text.
        Notice: Will be removed in 3.0
        z2w_counter is deprecated and will be removed in 3.0ú )	rB   rC   rD   r   r   r1   Úlowerr   Úsplit)r(   Zstring_printable_onlyr)   r)   r*   Ú	w_countera   s     ÿzCharsetMatch.w_counterc                 C   s"   | j d krt| j| jdƒ| _ | j S )NÚstrict)r'   r1   r   r   rA   r)   r)   r*   Ú__str__o   s    
zCharsetMatch.__str__c                 C   s   d  | j| j¡S )Nz<CharsetMatch '{}' bytes({})>)r0   r3   r4   rA   r)   r)   r*   Ú__repr__u   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   r7   r0   r2   r'   r$   Úappendr5   r)   r)   r*   Úadd_submatchx   s    ÿÿzCharsetMatch.add_submatchc                 C   s   | j S ©N)r   rA   r)   r)   r*   r3   ƒ   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   Úitemsr3   rN   )r(   Zalso_known_asÚuÚpr)   r)   r*   Úencoding_aliases‡   s    

zCharsetMatch.encoding_aliasesc                 C   s   | j S rP   ©r"   rA   r)   r)   r*   Úbom”   s    zCharsetMatch.bomc                 C   s   | j S rP   rU   rA   r)   r)   r*   Úbyte_order_mark˜   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!   rA   r)   r)   r*   r   œ   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.cdr]   r^   r   r3   r?   )r(   r]   r^   r   r)   r)   r*   Úlanguage¤   s    
ÿýzCharsetMatch.languagec                 C   s   | j S rP   )r    rA   r)   r)   r*   r9   À   s    zCharsetMatch.chaosc                 C   s   | j s
dS | j d d S )Nr   r   r   r[   rA   r)   r)   r*   r:   Ä   s    zCharsetMatch.coherencec                 C   s   t | jd dd�S ©Néd   é   )Úndigits)Úroundr9   rA   r)   r)   r*   Úpercent_chaosÊ   s    zCharsetMatch.percent_chaosc                 C   s   t | jd dd�S rb   )rf   r:   rA   r)   r)   r*   Úpercent_coherenceÎ   s    zCharsetMatch.percent_coherencec                 C   s   | j S )z+
        Original untouched bytes.
        )r   rA   r)   r)   r*   r@   Ò   s    zCharsetMatch.rawc                 C   s   | j S rP   )r$   rA   r)   r)   r*   ÚsubmatchÙ   s    zCharsetMatch.submatchc                 C   s   t | jƒdkS ©Nr   )r?   r$   rA   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   )rX   Úcharr)   r)   r*   rZ   æ   s    z*CharsetMatch.alphabets.<locals>.<listcomp>c                 S   s   h | ]}|r|’qS r)   r)   )rX   Úrr)   r)   r*   Ú	<setcomp>ê   s      z)CharsetMatch.alphabets.<locals>.<setcomp>)r#   r1   Ú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   )rX   Úmr)   r)   r*   rZ   ô   s     z6CharsetMatch.could_be_from_charset.<locals>.<listcomp>)r   r$   rA   r)   r)   r*   r`   í   s    z"CharsetMatch.could_be_from_charsetc                 C   s   | S ©z>
        Kept for BC reasons. Will be removed in 3.0.
        r)   rA   r)   r)   r*   Úfirstö   s    zCharsetMatch.firstc                 C   s   | S rs   r)   rA   r)   r)   r*   Úbestü   s    zCharsetMatch.bestÚutf_8)r3   r-   c                 C   s2   | j dk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&   r1   Úencoder%   )r(   r3   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   ry   Ú	hexdigestrA   r)   r)   r*   r4     s    zCharsetMatch.fingerprint)N)rv   ))Ú__name__Ú
__module__Ú__qualname__Úbytesr1   ÚfloatÚboolr   r+   Úobjectr6   r<   Úpropertyr;   rE   rF   r   rJ   rL   rM   rO   r3   r
   rT   rV   rW   r   ra   r9   r:   rg   rh   r@   ri   rk   rq   r`   rt   ru   ry   r4   r)   r)   r)   r*   r      sr    ùù	r   c                   @   sž   e Zd ZdZd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 rP   )ro   Ú_results)r(   r„   r)   r)   r*   r+     s    zCharsetMatches.__init__r=   c                 c   s   | j E d H  d S rP   ©r…   rA   r)   r)   r*   Ú__iter__  s    zCharsetMatches.__iter__)Úitemr-   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)r.   Úintr…   r1   r   r`   ÚKeyError)r(   rˆ   Úresultr)   r)   r*   Ú__getitem__!  s    






zCharsetMatches.__getitem__c                 C   s
   t | jƒS rP   ©r?   r…   rA   r)   r)   r*   Ú__len__/  s    zCharsetMatches.__len__c                 C   s   t | jƒdkS rj   r�   rA   r)   r)   r*   Ú__bool__2  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   r7   r0   r1   r2   r?   r@   r   r…   r4   r9   rO   rN   ro   )r(   rˆ   Úmatchr)   r)   r*   rN   5  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†   rA   r)   r)   r*   ru   I  s    zCharsetMatches.bestc                 C   s   |   ¡ S )zP
        Redundant method, call the method best(). Kept for BC reasons.
        )ru   rA   r)   r)   r*   rt   Q  s    zCharsetMatches.first)N)r{   r|   r}   Ú__doc__r
   r   r+   r	   r‡   r   r‰   r1   rŒ   rŽ   r€   r�   rN   r   ru   rt   r)   r)   r)   r*   rƒ     s   rƒ   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©Úpathr3   rT   Úalternative_encodingsra   rq   r   r9   r:   Úunicode_pathÚis_preferredc                 C   sF   || _ |
| _|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
d S rP   )r”   r–   r3   rT   r•   ra   rq   r   r9   r:   r—   )r(   r”   r3   rT   r•   ra   rq   r   r9   r:   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 )Nr“   r“   rA   r)   r)   r*   Ú__dict__w  s    õzCliDetectionResult.__dict__c                 C   s   t | jddd�S )NTé   )Úensure_asciiÚindent)r   r˜   rA   r)   r)   r*   Úto_json‡  s    zCliDetectionResult.to_jsonN)r{   r|   r}   r1   r   r
   r€   r   r+   r‚   r   r   r˜   rœ   r)   r)   r)   r*   r’   \  s   ôr’   )#rB   Úcollectionsr   Zencodings.aliasesr   Úhashlibr   Újsonr   Úrer   Útypingr   r   r	   r
   r   r   r   Zconstantr   r   Zmdr   Úutilsr   r   r   r   rƒ   r1   r   ZCoherenceMatchr   r’   r)   r)   r)   r*   Ú<module>   s    $  	C