a
    •˜ëjø  ã                   @  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 )z�support 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--checkz‘Don'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   ú[/mnt/bigstor/www/usecases/.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 )NZblackz	--config r%   r(   r*   r   r   r    Ú	run_blackI   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)   R   s<    

ÿÿþýûÿùøz#code_writer_cmd._run_console_script)Útextr$   c                 G  s   | j stj d |¡¡ d S )Nú )r   r=   r2   ÚwriteÚjoin)r   rD   r   r   r    Úwrite_statusy   s    zcode_writer_cmd.write_statuszUnion[str, Path])rD   Ú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_diffr1   ÚprintrH   r   Ú
write_text)r   rD   rI   r   r   r    Úwrite_output_file_from_text}   s    
ÿz+code_writer_cmd.write_output_file_from_text)r#   rI   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_filerK   rL   rQ   )r   rR   rS   r4   Úunlinkr1   ÚopenrT   ÚreadrH   ÚshutilÚmove)r   r#   rI   Útfr   r   r    Úwrite_output_file_from_tempfile‹   s    
*z/code_writer_cmd.write_output_file_from_tempfileN)rJ   rW   zOptional[str])rI   rJ   rW   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|ƒ}|r¬d| _td 	|¡ƒ W d   ƒ n1 sÀ0    Y  d S )NrM   )rO   T)ÚkeependsFz!source or source_file is requiredz<proposed changes>é   rN   )ÚfromfileÚtofileÚnZlinetermr0   )
rY   ÚlistÚ
splitlinesÚdifflibZunified_diffr   Úas_posixr   rT   rG   )	r   rI   rJ   rW   r]   Zsource_linesZdpÚdZ	d_as_listr   r   r    rS   š   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   rR   r   r   r1   r=   Úexitri   r   r   r    Úrun_program»   s    zcode_writer_cmd.run_program)Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r!   r+   r,   r)   rH   rV   r^   rS   Ú
contextlibÚcontextmanagerrj   rm   r   r   r   r    r   $   s(   
		'ûr   )Ú__doc__Ú
__future__r   Úargparser   r   rr   rf   r4   Úpathlibr   r8   r[   r:   r=   Útypingr   r   r   r	   r
   r0   r   r   r   r   r   r    Ú<module>   s$   