3
ãde÷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 )Ng        )Ú_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(   úP/var/www/agendate/envp3/lib/python3.6/site-packages/charset_normalizer/models.pyÚ__init__   s    	zCharsetMatch.__init__)ÚotherÚreturnc             C   s>   t |tƒs&tdj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”?g        )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 )Ng      ð?)Úlenr0   Úraw)r'   r(   r(   r)   r9   D   s    zCharsetMatch.multi_byte_usagec             C   s   t j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.0g      ð?)ÚwarningsÚwarnÚDeprecationWarningr   r0   )r'   r(   r(   r)   Úchaos_secondary_passH   s    z!CharsetMatch.chaos_secondary_passc             C   s   t j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.0g        )r=   r>   r?   )r'   r(   r(   r)   Úcoherence_non_latinU   s    z CharsetMatch.coherence_non_latinc             C   s,   t jdtƒ ttdt| ƒjƒ ƒ}t|jƒ ƒ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ú )	r=   r>   r?   r   r   r0   Ú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&   r0   r   r   )r'   r(   r(   r)   Ú__str__o   s    
zCharsetMatch.__str__c             C   s   dj | j| jƒS )Nz<CharsetMatch '{}' bytes({})>)r/   r2   r3   )r'   r(   r(   r)   Ú__repr__u   s    zCharsetMatch.__repr__c             C   s:   t |tƒ s|| kr$tdj|jƒƒ‚d |_| jj|ƒ d S )Nz;Unable to add instance <{}> as a submatch of a CharsetMatch)r-   r   r5   r/   r1   r&   r#   Úappend)r'   r+   r(   r(   r)   Úadd_submatchx   s    
zCharsetMatch.add_submatchc             C   s   | j S )N)r   )r'   r(   r(   r)   r2   ƒ   s    zCharsetMatch.encodingc             C   sH   g }x>t jƒ D ]2\}}| j|kr,|j|ƒ q| j|kr|j|ƒ qW |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'   Zalso_known_asÚuÚpr(   r(   r)   Úencoding_aliases‡   s    

zCharsetMatch.encoding_aliasesc             C   s   | j S )N)r!   )r'   r(   r(   r)   Úbom”   s    zCharsetMatch.bomc             C   s   | j S )N)r!   )r'   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    )r'   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.cdrU   rV   r   r2   r;   )r'   rU   rV   r   r(   r(   r)   Úlanguage¤   s    

zCharsetMatch.languagec             C   s   | j S )N)r   )r'   r(   r(   r)   r7   À   s    zCharsetMatch.chaosc             C   s   | j s
dS | j d d S )Ng        r   r   )r    )r'   r(   r(   r)   r8   Ä   s    zCharsetMatch.coherencec             C   s   t | jd dd�S )Néd   é   )Úndigits)Úroundr7   )r'   r(   r(   r)   Úpercent_chaosÊ   s    zCharsetMatch.percent_chaosc             C   s   t | jd dd�S )NrZ   r[   )r\   )r]   r8   )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 )N)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   )rQ   Úcharr(   r(   r)   rS   ç   s    z*CharsetMatch.alphabets.<locals>.<listcomp>c             S   s   h | ]}|r|’qS r(   r(   )rQ   Úrr(   r(   r)   ú	<setcomp>ê   s    z)CharsetMatch.alphabets.<locals>.<setcomp>)r"   r0   Ú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(   )r2   )rQ   Úmr(   r(   r)   rS   ô   s    z6CharsetMatch.could_be_from_charset.<locals>.<listcomp>)r   r#   )r'   r(   r(   r)   rX   í   s    z"CharsetMatch.could_be_from_charsetc             C   s   | S )z>
        Kept for BC reasons. Will be removed in 3.0.
        r(   )r'   r(   r(   r)   Úfirstö   s    zCharsetMatch.firstc             C   s   | S )z>
        Kept for BC reasons. Will be removed in 3.0.
        r(   )r'   r(   r(   r)   Úbestü   s    zCharsetMatch.bestÚutf_8)r2   r,   c             C   s2   | j dks| j |kr,|| _ t| ƒj|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%   r0   Úencoder$   )r'   r2   r(   r(   r)   Úoutput  s    zCharsetMatch.outputc             C   s   t | jƒ ƒjƒ S )zw
        Retrieve the unique SHA256 computed using the transformed (re-encoded) payload. Not the original one.
        )r   rn   Ú	hexdigest)r'   r(   r(   r)   r3     s    zCharsetMatch.fingerprint)N)rk   ))Ú__name__Ú
__module__Ú__qualname__Úbytesr0   ÚfloatÚboolr   r*   Úobjectr4   r:   Úpropertyr9   r@   rA   r   rE   rG   rH   rJ   r2   r
   rN   rO   rP   r   rY   r7   r8   r^   r_   r<   r`   ra   rg   rX   ri   rj   rn   r3   r(   r(   r(   r)   r      sb   	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 )N)re   Ú_results)r'   ry   r(   r(   r)   r*     s    zCharsetMatches.__init__)r,   c             c   s   | j E d H  d S )N)rz   )r'   r(   r(   r)   Ú__iter__  s    zCharsetMatches.__iter__)Úitemr,   c             C   sN   t |tƒr| j| S t |tƒrFt|dƒ}x| jD ]}||jkr0|S q0W 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-   Úintrz   r0   r   rX   ÚKeyError)r'   r|   Úresultr(   r(   r)   Ú__getitem__!  s    




zCharsetMatches.__getitem__c             C   s
   t | jƒS )N)r;   rz   )r'   r(   r(   r)   Ú__len__/  s    zCharsetMatches.__len__c             C   s   t | jƒdkS )Nr   )r;   rz   )r'   r(   r(   r)   Ú__bool__2  s    zCharsetMatches.__bool__c             C   s~   t |tƒstdjt|jƒƒƒ‚t|jƒtkrbx4| j	D ]*}|j
|j
kr4|j|jkr4|j|ƒ dS q4W | j	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   r5   r/   r0   r1   r;   r<   r   rz   r3   r7   rJ   rI   re   )r'   r|   Úmatchr(   r(   r)   rI   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   )rz   )r'   r(   r(   r)   rj   I  s    zCharsetMatches.bestc             C   s   | j ƒ S )zP
        Redundant method, call the method best(). Kept for BC reasons.
        )rj   )r'   r(   r(   r)   ri   Q  s    zCharsetMatches.first)N)rp   rq   rr   Ú__doc__r
   r   r*   r	   r{   r   r}   r0   r€   r�   ru   r‚   rI   r   rj   ri   r(   r(   r(   r)   rx     s   rx   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)Úpathr2   rN   Úalternative_encodingsrY   rg   r   r7   r8   Úunicode_pathÚis_preferredc             C   sF   || _ |
| _|| _|| _|| _|| _|| _|| _|| _|	| _	|| _
d S )N)r†   rˆ   r2   rN   r‡   rY   rg   r   r7   r8   r‰   )r'   r†   r2   rN   r‡   rY   rg   r   r7   r8   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†   r2   rN   r‡   rY   rg   r   r7   r8   rˆ   r‰   )r†   r2   rN   r‡   rY   rg   r   r7   r8   rˆ   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_json‡  s    zCliDetectionResult.to_jsonN)rp   rq   rr   r0   r   r
   ru   rt   r*   rw   r   r   rŠ   rŽ   r(   r(   r(   r)   r…   \  s   r…   )#r=   Úcollectionsr   Zencodings.aliasesr   Úhashlibr   Újsonr   Úrer   Útypingr   r   r	   r
   r   r   r   Zconstantr   r   Zmdr   Úutilsr   r   r   r   rx   r0   rt   ZCoherenceMatchr   r…   r(   r(   r(   r)   Ú<module>   s    $  	C