a
    [g                     @  s   d Z ddlmZ ddlmZ 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ZddlZddlZ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 G dd dZdS )zsupport routines for the helpers in tools/.

These aren't imported by the enclosing util package as the are not
needed for normal library use.

    )annotations)ArgumentParser)	NamespaceN)Path)Any)Dict)Iterator)Optional)Union   )compatc                   @  s   e Zd ZU ded< ded< ded< ded< ded	< ded
< ddddZdddddZdddddZddddddZdddddZddddddZ	dddd d!d"Z
d#d#d$dd%d%dd&d'd(Zejd)d*d+d,Zejd-d*d.d/Zd#S )0code_writer_cmdr   parserr   argsboolsuppress_outputdiffs_detectedr   source_rootpyproject_toml_pathstr)tool_scriptc                 C  s^   t |jj| _| jt d | _| j s,J t | _| jjdddd | jjddddd	 d S )
Nzpyproject.tomlz--stdout
store_truez)Write to stdout instead of saving to file)actionhelp-cz--checkzDon't write the files back, just return the status. Return code 0 means nothing would change. Return code 1 means some files would be reformatted)r   r   )r   parentr   r   existsr   r   add_argument)selfr    r   j/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/sqlalchemy/util/tool_support.py__init__+   s    zcode_writer_cmd.__init__None)tempfilereturnc                 C  s"   |  t|dd| j d d S )NZzimportsz--toml-config 
entrypointoptions_run_console_scriptr   r   r   r#   r   r   r    run_zimports?   s    
zcode_writer_cmd.run_zimportsc                 C  s"   |  t|dd| j d d S )Nblackz	--config r%   r(   r*   r   r   r    	run_blackH   s    
zcode_writer_cmd.run_blackzDict[str, Any])pathr'   r$   c           
      C  s   t jdk}|d }tdD ]}|j|kr|} qBqtd| |dd}tj||d|g }i }	| jr~t	j
 |	d< |	d	< t	jtjd
d|j|j|jf g| fdt| ji|	 dS )zeRun a Python console application from within the process.

        Used for black, zimports

        posixr&   console_scriptsz*Could not find entrypoint console_scripts.r'    )r/   stdoutstderrr   zimport %s; %s.%s()cwdN)osnamer   Zimportlib_metadata_get	Exceptiongetshlexsplitr   
subprocessDEVNULLrunsys
executablemoduleattrr   r   )
r   r.   r'   Zis_posixZentrypoint_nameentryimplZcmdline_options_strZcmdline_options_listkwr   r   r    r)   Q   s<    

z#code_writer_cmd._run_console_script)textr$   c                 G  s   | j stjd| d S )N )r   r>   r3   writejoin)r   rE   r   r   r    write_statusx   s    zcode_writer_cmd.write_statuszUnion[str, Path])rE   destination_pathr$   c                 C  s^   | j jr| j||d nB| j jr*t| n0| d| d t|j|ddd | d d S )N)sourceWriting ...utf-8
)encodingnewlinedone
)r   check	_run_diffr2   printrI   r   
write_text)r   rE   rJ   r   r   r    write_output_file_from_text|   s    
z+code_writer_cmd.write_output_file_from_text)r#   rJ   r$   c                 C  s   | j jr"| j||d t| np| j jrjt|}t|  W d    n1 sT0    Y  t| n(| 	d| d t
|| | 	d d S )N)source_filerL   rM   rR   )r   rS   rT   r5   unlinkr2   openrU   readrI   shutilmove)r   r#   rJ   tfr   r   r    write_output_file_from_tempfile   s    
*z/code_writer_cmd.write_output_file_from_tempfileN)rK   rX   zOptional[str])rJ   rK   rX   r$   c          	   	   C  s   |r:t |dd}t|}W d    q\1 s.0    Y  n"|d urP|jdd}nds\J dt |ddR}tjt||t| ddd	d
}t|}|rd| _td	| W d    n1 s0    Y  d S )NrN   )rP   T)keependsFz!source or source_file is requiredz<proposed changes>   rO   )fromfiletofilenZlinetermr1   )
rZ   list
splitlinesdifflibZunified_diffr   as_posixr   rU   rH   )	r   rJ   rK   rX   r^   Zsource_linesZdpdZ	d_as_listr   r   r    rT      s&    (
zcode_writer_cmd._run_diffzIterator[ArgumentParser])r$   c                 c  s   | j V  d S )N)r   r   r   r   r    add_arguments   s    zcode_writer_cmd.add_argumentszIterator[None]c                 c  sf   | j  | _| jjr"d| _d| _n| jjr2d| _nd| _d V  | jjrX| jrXtd n
td d S )NFTr   r   )	r   
parse_argsr   rS   r   r   r2   r>   exitrj   r   r   r    run_program   s    zcode_writer_cmd.run_program)__name__
__module____qualname____annotations__r!   r+   r-   r)   rI   rW   r_   rT   
contextlibcontextmanagerrk   rn   r   r   r   r    r   #   s(   
		'r   )__doc__
__future__r   argparser   r   rs   rg   r5   pathlibr   r9   r\   r;   r>   typingr   r   r   r	   r
   r1   r   r   r   r   r   r    <module>   s$   