o
    Šå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 )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
< d1dd„Zd2dd„Zd2dd„Zd3dd„Zd4dd„Zd5d d!„Z	d6d"d#„Z
d$d$d%œd7d)d*„Zejd8d,d-„ƒZejd9d/d0„ƒZd$S ):Úcode_writer_cmdr   Úparserr   ÚargsÚboolÚsuppress_outputÚdiffs_detectedr   Úsource_rootÚpyproject_toml_pathÚtool_scriptÚstrc                 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   úX/var/www/html/flaskapp/venv/lib/python3.10/site-packages/sqlalchemy/util/tool_support.pyÚ__init__+   s   ý
úzcode_writer_cmd.__init__ÚtempfileÚreturnÚNonec                 C  ó"   |   t|ƒdd| j› �dœ¡ d S )NÚzimportsz--toml-config ©Ú
entrypointÚoptions©Ú_run_console_scriptr   r   ©r   r"   r   r   r    Úrun_zimports?   ó   
þþzcode_writer_cmd.run_zimportsc                 C  r%   )NÚblackz	--config r'   r*   r,   r   r   r    Ú	run_blackH   r.   zcode_writer_cmd.run_blackÚpathr)   úDict[str, Any]c           
      C  sº   t jdk}|d }t d¡D ]}|j|kr|} nq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)   Ú )r3   ÚstdoutÚstderrr   zimport %s; %s.%s()ÚcwdN)ÚosÚnamer   Úimportlib_metadata_getÚ	ExceptionÚgetÚshlexÚsplitr   Ú
subprocessÚDEVNULLÚrunÚsysÚ
executableÚmoduleÚattrr   r   )
r   r1   r)   Úis_posixÚentrypoint_nameÚentryÚimplÚcmdline_options_strÚcmdline_options_listÚkwr   r   r    r+   Q   s>   

þÿÿþýûÿù
øz#code_writer_cmd._run_console_scriptÚtextc                 G  s    | j stj d |¡¡ d S d S )Nú )r   rC   r7   ÚwriteÚjoin)r   rN   r   r   r    Úwrite_statusx   s   ÿzcode_writer_cmd.write_statusÚdestination_pathúUnion[str, Path]c                 C  sb   | j jr| j||d� d S | j jrt|ƒ d S |  d|› d�¡ t|ƒj|ddd� |  d¡ d S )N)ÚsourceúWriting ú...úutf-8Ú
)ÚencodingÚnewlineúdone
)r   ÚcheckÚ	_run_diffr6   ÚprintrR   r   Ú
write_text)r   rN   rS   r   r   r    Úwrite_output_file_from_text|   s   ÿz+code_writer_cmd.write_output_file_from_textc                 C  sš   | j jr| j||d� t |¡ d S | j jr7t|ƒ�}t| ¡ ƒ W d   ƒ n1 s+w   Y  t |¡ d S |  	d|› d�¡ t
 ||¡ |  	d¡ d S )N)Úsource_filerV   rW   r\   )r   r]   r^   r9   Úunlinkr6   Úopenr_   ÚreadrR   ÚshutilÚmove)r   r"   rS   Útfr   r   r    Úwrite_output_file_from_tempfileŠ   s   
ÿz/code_writer_cmd.write_output_file_from_tempfileN)rU   rb   rU   úOptional[str]rb   c          	   	   C  sÜ   |rt |dd��}t|ƒ}W d   ƒ n1 sw   Y  n|d ur(|jdd�}nJ dƒ‚t |dd��2}tjt|ƒ|t|ƒ ¡ ddd	d
�}t|ƒ}|r\d| _td 	|¡ƒ W d   ƒ d S W d   ƒ d S 1 sgw   Y  d S )NrX   )rZ   T)ÚkeependsFz!source or source_file is requiredz<proposed changes>é   rY   )ÚfromfileÚtofileÚnÚlinetermr5   )
rd   ÚlistÚ
splitlinesÚdifflibÚunified_diffr   Úas_posixr   r_   rQ   )	r   rS   rU   rb   rh   Úsource_linesÚdpÚdÚ	d_as_listr   r   r    r^   ™   s0   
ÿ€
úô
"özcode_writer_cmd._run_diffúIterator[ArgumentParser]c                 c  s   � | j V  d S )N)r   ©r   r   r   r    Úadd_arguments¶   s   €zcode_writer_cmd.add_argumentsúIterator[None]c                 c  sj   � | j  ¡ | _| jjrd| _d| _n| jjrd| _nd| _d V  | jjr.| jr.t d¡ d S t d¡ d S )NFTr   r   )	r   Ú
parse_argsr   r]   r   r   r6   rC   Úexitr{   r   r   r    Úrun_programº   s   €zcode_writer_cmd.run_program)r   r   )r"   r   r#   r$   )r1   r   r)   r2   r#   r$   )rN   r   r#   r$   )rN   r   rS   rT   r#   r$   )r"   r   rS   r   r#   r$   )rS   rT   rU   rj   rb   rj   r#   r$   )r#   rz   )r#   r}   )Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r!   r-   r0   r+   rR   ra   ri   r^   Ú
contextlibÚcontextmanagerr|   r€   r   r   r   r    r   #   s*   
 


	
	
'

ûr   )Ú__doc__Ú
__future__r   Úargparser   r   r…   rs   r9   Úpathlibr   r>   rf   r@   rC   Útypingr   r   r   r	   r
   r5   r   r   r   r   r   r    Ú<module>   s&   