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   Y/home/kim/smarthome/.venv/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
    rN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 ]  qtd7tjd8 d9S |jd6u r|jdu r|jr|jD ]  qtd:tjd8 d9S |jd;k s|jd<kr|jr|jD ]  qt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 rtdHjtjd8 j*du r  qt+t,j}t-t,j}|.dI}|jdu r|/dJ j j*du rވ  n|jdu rt0dKjdLdu rj*du r  qz)t1|dI1||_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dS1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.pyZ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   ZexplainZ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                   rr   r   rs   rt   )elr   r   rJ     rw   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   rx   r   r   r   rJ   U  s    r      )ensure_asciiindentrG   c                   s&   g | ]}|j t jkr|jpd qS )Z	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&   Zencoding_aliasesZcould_be_from_charsetlanguageZ	alphabetsbomZpercent_chaosZpercent_coherencelenrd   
startswithclosedr   r   r   splitinsertr   r   Zunicode_pathr?   writeoutputr@   r   rf   r   r}   )rX   parserrQ   Zx_matchesZ
cli_resultZdir_path	file_nameZo_fprD   r   )rv   rx   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   Zos.pathr   r   r   r   r   platformr	   unicodedatar
   Zcharset_normalizer.mdmdr   Zcharset_normalizerr   Zcharset_normalizer.modelsr   Zcharset_normalizer.versionr   r   r    r   rP   r   r   r   r   <module>   s(    ;  
