o
    <‘ði¤.  ã                   @  sÐ   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZ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 d d	lmZ dddd„ZG dd„ dƒZdddd„Zedkrfeƒ  dS dS )é    )ÚannotationsN)Údumps)ÚabspathÚbasenameÚdirnameÚjoinÚrealpath)Úpython_version)Úunidata_version)Úfrom_fp)ÚCliDetectionResult)Ú__version__ÚyesÚquestionÚstrÚdefaultÚreturnÚboolc                 C  sT   |dkrdnd}	 t | | ƒ ¡  ¡ }|s|dkS |dv rdS |dv r%dS tdƒ q	)	zBAsk a yes/no question via input() and return the answer as a bool.r   z [Y/n] z [y/N] T)Úyr   )ÚnÚnoFzPlease respond with 'y' or 'n'.)ÚinputÚstripÚlowerÚprint)r   r   ÚpromptÚchoice© r   ú^/root/parts/websockify/install/lib/python3.10/site-packages/charset_normalizer/cli/__main__.pyÚquery_yes_no   s   ør   c                   @  s8   e Zd ZdZ				dddd„Zddd„Zddd„ZdS )ÚFileTypeaè  Factory for creating file object types

    Instances of FileType are typically passed as type= arguments to the
    ArgumentParser add_argument() method.

    Keyword Arguments:
        - mode -- A string indicating how the file is to be opened. Accepts the
            same values as the builtin open() function.
        - bufsize -- The file's desired buffer size. Accepts the same values as
            the builtin open() function.
        - encoding -- The file's encoding. Accepts the same values as the
            builtin open() function.
        - errors -- A string indicating how encoding and decoding errors are to
            be handled. Accepts the same value as the builtin open() function.

    Backported from CPython 3.12
    ÚréÿÿÿÿNÚmoder   ÚbufsizeÚintÚencodingú
str | NoneÚerrorsc                 C  s   || _ || _|| _|| _d S ©N)Ú_modeÚ_bufsizeÚ	_encodingÚ_errors)Úselfr#   r$   r&   r(   r   r   r   Ú__init__3   s   
zFileType.__init__Ústringr   ú	typing.IOc              
     sº   |dkr6dˆ j v rdˆ j v rtjjS tjS t‡ fdd„dD ƒƒr,dˆ j v r)tjjS tjS dˆ j › �}t|ƒ‚zt|ˆ j ˆ jˆ j	ˆ j
ƒW S  ty\ } zd|› d	|› �}t |¡‚d }~ww )
Nú-r!   Úbc                 3  s   � | ]}|ˆ j v V  qd S r)   )r*   )Ú.0Úc©r.   r   r   Ú	<genexpr>D   s   € z$FileType.__call__.<locals>.<genexpr>Úwaxzargument "-" with mode zcan't open 'z': )r*   ÚsysÚstdinÚbufferÚanyÚstdoutÚ
ValueErrorÚopenr+   r,   r-   ÚOSErrorÚargparseÚArgumentTypeError)r.   r0   ÚmsgÚeÚmessager   r6   r   Ú__call__?   s   

€þzFileType.__call__c                 C  sX   | j | jf}d| jfd| jfg}d dd„ |D ƒdd„ |D ƒ ¡}t| ƒj› d|› d�S )	Nr&   r(   ú, c                 S  s   g | ]
}|d krt |ƒ‘qS )r"   )Úrepr)r4   Úargr   r   r   Ú
<listcomp>U   s    z%FileType.__repr__.<locals>.<listcomp>c                 S  s&   g | ]\}}|d ur|› d|›�‘qS )Nú=r   )r4   ÚkwrI   r   r   r   rJ   V   s   & ú(ú))r*   r+   r,   r-   r   ÚtypeÚ__name__)r.   ÚargsÚkwargsÚargs_strr   r   r   Ú__repr__Q   s   ÿÿzFileType.__repr__)r!   r"   NN)r#   r   r$   r%   r&   r'   r(   r'   )r0   r   r   r1   )r   r   )rP   Ú
__module__Ú__qualname__Ú__doc__r/   rF   rT   r   r   r   r   r        s    û
r    Úargvúlist[str] | Noner%   c                   s$  t jdd�}|jdtdƒddd� |jdd	d
dddd� |jddd
dddd� |jddd
dddd� |jddd
dddd� |jddd
dddd� |jdd d
dd!d"d� |jd#d$d
dd%d&d� |jd'd(d)d*td+d,d-� |jd.d/d0 ttƒ tt	j
 ¡  d1¡r~d2nd3¡d4d5� | | ¡}|jd6u r©|jdu r©|jr |jD ]‰ˆ ¡  q™td7tjd8� d9S |jd6u rÉ|jdu rÉ|jrÀ|jD ]‰ˆ ¡  q¹td:tjd8� d9S |jd;k sÓ|jd<kré|jrà|jD ]‰ˆ ¡  qÙtd=tjd8� d9S g }|jD �]i‰tˆ|j|j|jdu d>�}| ¡ ‰ ˆ d?u �r1td@ ˆj|jd<k �rdAndB¡tjd8� | ttˆjƒd?g g dCg dd<d;d?d6ƒ¡ �nttˆjƒˆ jˆ j ‡ fdDdE„ˆ j!D ƒˆ j"ˆ j#ˆ j$ˆ j%ˆ j&d?d6ƒ}| |¡ t'|ƒd9k�r�|j(�r�|D ]+‰ˆˆ k�r�| ttˆjƒˆjˆj ‡fdFdE„ˆj!D ƒˆj"ˆj#ˆj$ˆj%ˆj&d?dƒ¡ �qc|jd6u �rNˆ j )dG¡d6u �r´tdH ˆj¡tjd8� ˆj*du �r³ˆ ¡  qît+t,ˆjƒƒ}t-t,ˆjƒƒ}| .dI¡}|jdu �rß| /dJˆ j¡ ˆj*du �rÞˆ ¡  n|jdu �rüt0dK ˆj¡dLƒdu �rüˆj*du �rûˆ ¡  qîz)t1|dI 1|¡ƒ|_2t3|j2dMƒ�}	|	 4ˆ  5¡ ¡ W d?  ƒ n	1 �sw   Y  W n( t6�yM }
 ztt7|
ƒtjd8� ˆj*du �rAˆ ¡  W Y d?}
~
 dNS d?}
~
ww ˆj*du �rXˆ ¡  qî|j8du �r{tt9t'|ƒd9k�rodOdE„ |D ƒn|dP j:d6dQdR�ƒ dPS |jD ]‰tdS 1‡fdTdE„|D ƒ¡ƒ �q~dPS )Uzˆ
    CLI assistant using ARGV and ArgumentParser
    :param argv:
    :return: 0 if everything is fine, anything else equal trouble
    zvThe Real First Universal Charset Detector. Discover originating encoding used on text file. Normalize text to unicode.)ÚdescriptionÚfilesÚrbú+zFile(s) to be analysed)rO   ÚnargsÚhelpz-vz	--verboseÚ
store_trueFÚverbosezjDisplay complementary information about file if any. Stdout will contain logs about the detection process.)Úactionr   Údestr_   z-az--with-alternativeÚalternativeszIOutput complementary possibilities if any. Top-level JSON WILL be a list.z-nz--normalizeÚ	normalizezLPermit to normalize input file. If not set, program does not write anything.z-mz	--minimalÚminimalzBOnly output the charset detected to STDOUT. Disabling JSON output.z-rz	--replaceÚreplacezGReplace file when trying to normalize it instead of creating a new one.z-fz--forceÚforcezHReplace file without asking if you are sure, use this flag with caution.z-iz--no-preemptiveÚno_preemptivez>Disable looking at a charset declaration to hint the detector.z-tz--thresholdÚstoregš™™™™™É?Ú	thresholdzUDefine a custom maximum amount of noise allowed in decoded content. 0. <= noise <= 1.)rb   r   rO   rc   r_   z	--versionÚversionz;Charset-Normalizer {} - Python {} - Unicode {} - SpeedUp {}z.pyÚOFFÚONz"Show version information and exit.)rb   rl   r_   Tz.Use --replace in addition of --normalize only.)Úfileé   z*Use --force in addition of --replace only.g        g      ð?z---threshold VALUE should be between 0. AND 1.)rk   ÚexplainÚpreemptive_behaviourNz4Unable to identify originating encoding for "{}". {}z-Maybe try increasing maximum amount of chaos.Ú ÚUnknownc                   ó   g | ]	}|ˆ j kr|‘qS r   ©r&   ©r4   Úcp)Ú
best_guessr   r   rJ   û   ó
    
þzcli_detect.<locals>.<listcomp>c                   ru   r   rv   rw   )Úelr   r   rJ     rz   ÚutfzJ"{}" file does not need to be normalized, as it already came from unicode.Ú.r"   z0Are you sure to normalize "{}" by replacing it ?r   Úwbé   c                 S  s   g | ]}|j ‘qS r   )Ú__dict__©r4   r{   r   r   r   rJ   U  s    r   é   )Úensure_asciiÚindentrG   c                   s&   g | ]}|j tˆ jƒkr|jpd ‘qS )Ú	undefined)Úpathr   Únamer&   r�   )Úmy_filer   r   rJ   ^  s
    þ);rA   ÚArgumentParserÚadd_argumentr    ÚfloatÚformatr   r	   r
   Ú	md_moduleÚ__file__r   ÚendswithÚ
parse_argsrg   re   r[   Úcloser   r9   Ústderrrh   rk   r   ra   ri   Úbestr‡   Úappendr   r   r&   Úencoding_aliasesÚcould_be_from_charsetÚlanguageÚ	alphabetsÚbomÚpercent_chaosÚpercent_coherenceÚlenrd   Ú
startswithÚclosedr   r   r   ÚsplitÚinsertr   r   Úunicode_pathr?   ÚwriteÚoutputr@   r   rf   r   r€   )rX   ÚparserrQ   Úx_ÚmatchesÚ
cli_resultÚdir_pathÚ	file_nameÚo_ÚfprD   r   )ry   r{   rˆ   r   Ú
cli_detect[   sÚ  ÿÿú	úúúúúúù	ü÷






ü
ÿû÷õÿ
þñ


þñÿ€ÿü
€ÿüúÿ€€ü€$ýÿ
õ
þÿÿ
r¬   Ú__main__)r   )r   r   r   r   r   r   r)   )rX   rY   r   r%   )Ú
__future__r   rA   r9   ÚtypingÚjsonr   Úos.pathr   r   r   r   r   Úplatformr	   Úunicodedatar
   Úcharset_normalizer.mdÚmdr�   Úcharset_normalizerr   Úcharset_normalizer.modelsr   Úcharset_normalizer.versionr   r   r    r¬   rP   r   r   r   r   Ú<module>   s(    ;  
ÿ