
    
 @gQ                        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  G 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   )compatc                      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Zd'dZd'dZd(dZd)dZd*dZ	d+dZ
dddd,d!Zej        d-d#            Zej        d.d%            ZdS )/code_writer_cmdr   parserr   argsboolsuppress_outputdiffs_detectedr   source_rootpyproject_toml_pathtool_scriptstrc                L   t          |          j        j        | _        | j        t          d          z  | _        | j                                        sJ t                      | _        | j                            ddd           | j                            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   s     P/var/www/piapp/venv/lib/python3.11/site-packages/sqlalchemy/util/tool_support.py__init__zcode_writer_cmd.__init__*   s    ,,3:#'#3d;K6L6L#L '..00000$&&  < 	! 	
 	
 	

 	  B   	! 	
 	
 	
 	
 	
    tempfilereturnNonec                b    |                      t          |          dd| j         d           d S )Nzimportsz--toml-config 
entrypointoptions_run_console_scriptr   r   r    r$   s     r!   run_zimportszcode_writer_cmd.run_zimports>   sH      MM(FD,DFF 	
 	
 	
 	
 	
r#   c                b    |                      t          |          dd| j         d           d S )Nblackz	--config r)   r,   r.   s     r!   	run_blackzcode_writer_cmd.run_blackG   sH      MM%At'?AA 	
 	
 	
 	
 	
r#   pathr+   Dict[str, Any]c                   t           j        dk    }|d         }t          j        d          D ]}|j        |k    r|} nt	          d|           |                    dd          }t          j        ||          |gz   }i }	| j        rt          j
        x|	d<   |	d	<   t          j        t          j        d
d|j        d|j        d|j        dg|z   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+    )r6   stdoutstderrr   zimport z; .z()cwdN)osnamer   importlib_metadata_get	Exceptiongetshlexsplitr   
subprocessDEVNULLrunsys
executablemoduleattrr   r   )
r    r3   r+   is_posixentrypoint_nameentryimplcmdline_options_strcmdline_options_listkws
             r!   r-   z#code_writer_cmd._run_console_scriptP   sJ    7g%!,/23DEE 	 	Ez_,, - N_NN   &kk)R88${x 
  
  
F    	=*4*<<BxL2h<(,T[[[$)))L
 ##		
 		
 D$%%		
 		
 		
 		
 		
 		
r#   textc                |    | j         s4t          j                            d                    |                     d S d S )N )r   rG   r:   writejoin)r    rR   s     r!   write_statuszcode_writer_cmd.write_statusw   s=    # 	-JSXXd^^,,,,,	- 	-r#   destination_pathc                *   | j         j        r|                     ||           d S | j         j        rt	          |           d S |                     d| d           t          |                              |           |                     d           d S )N)sourceWriting ...done
)r   check	_run_diffr9   printrW   r   
write_text)r    rR   rX   s      r!   write_output_file_from_textz+code_writer_cmd.write_output_file_from_text{   s     9? 	(NN+DN99999Y 	($KKKKK>)9>>>???!""--d333h'''''r#   c                   | j         j        r-|                     ||           t          j        |           d S | j         j        r^t          |          5 }t          |                                           d d d            n# 1 swxY w Y   t          j        |           d S | 	                    d| d           t          j        ||           | 	                    d           d S )N)source_filer[   r\   r]   )r   r^   r_   r=   unlinkr9   openr`   readrW   shutilmove)r    r$   rX   tfs       r!   write_output_file_from_tempfilez/code_writer_cmd.write_output_file_from_tempfile   s"    9? 
	(NN+NBBBIhY 	(h !2bggii   ! ! ! ! ! ! ! ! ! ! ! ! ! ! !Ih>)9>>>???K"2333h'''''s   "BB
BN)rZ   rd   rZ   Optional[str]rd   c          	        |r7t          |          5 }t          |          }d d d            n# 1 swxY w Y   n#||                    d          }n
J d            t          |          5 }t          j        t          |          ||ddd          }t          |          }|r)d| _        t          d	                    |                     d d d            d S # 1 swxY w Y   d S )
NT)keependsFz!source or source_file is requiredz<proposed changes>   
)fromfiletofilenlinetermr8   )rf   list
splitlinesdifflibunified_diffr   r`   rV   )	r    rX   rZ   rd   rj   source_linesdpd	d_as_lists	            r!   r_   zcode_writer_cmd._run_diff   s{     	>k"" (b#Bxx( ( ( ( ( ( ( ( ( ( ( ( ( ( (!,,d,;;LL====="## 	*r$R)+  A QI *&*#bggi(()))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s   .22,A"CC"CIterator[ArgumentParser]c              #     K   | j         V  d S )N)r   r    s    r!   add_argumentszcode_writer_cmd.add_arguments   s      kr#   Iterator[None]c              #  4  K   | j                                         | _        | j        j        rd| _        d| _        n| j        j        rd| _        nd| _        d V  | j        j        r| j        rt          j        d           d S t          j        d           d S )NFTr   r   )	r   
parse_argsr   r^   r   r   r9   rG   exitr   s    r!   run_programzcode_writer_cmd.run_program   s      K**,,	9? 	)"'D#'D  Y 	)#'D  #(D 9? 	t2 	HQKKKKKHQKKKKKr#   )r   r   )r$   r   r%   r&   )r3   r   r+   r4   r%   r&   )rR   r   r%   r&   )rR   r   rX   r   r%   r&   )r$   r   rX   r   r%   r&   )rX   r   rZ   rl   rd   rl   r%   r&   )r%   r}   )r%   r   )__name__
__module____qualname____annotations__r"   r/   r2   r-   rW   rb   rk   r_   
contextlibcontextmanagerr   r    r#   r!   r   r   "   sV        OOO
 
 
 
(
 
 
 

 
 
 
%
 %
 %
 %
N- - - -
( 
( 
( 
(( ( ( (& !%%)* * * * * *:          r#   r   )__doc__
__future__r   argparser   r   r   rw   r=   pathlibr   rB   rh   rD   rG   typingr   r   r	   r
   r8   r   r   r   r#   r!   <module>r      s4    # " " " " " # # # # # #            				             



                              d d d d d d d d d dr#   