
    Zi                    z   U d dl mZ d dl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 erdd
lmZ dZi Zded<   ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZddZddZ	 	 	 	 	 	 	 	 	 	 ddZ ed      	 	 d 	 	 	 	 	 	 	 	 	 d!d       Z ed      d"d#d       Z ed      d"d#d       Zy)$    )annotationsN)Any)Callable)TYPE_CHECKING   )util)compat)_preserving_path_as_str)PostWriteHookConfigREVISION_SCRIPT_FILENAMEdict	_registryc                      fd}|S )zA function decorator that will register that function as a write hook.

    See the documentation linked below for an example.

    .. seealso::

        :ref:`post_write_hooks_custom`


    c                    | t         <   | S )N)r   )fnnames    V/home/portmadness/app/.venv/lib/python3.12/site-packages/alembic/script/write_hooks.pydecoratezregister.<locals>.decorate'   s    	$	     )r   r   s   ` r   registerr      s     Or   c                    t        |      }	 t        |    } |||      S # t        $ r}t        j                  d|  d      |d}~ww xY w)a1  Invokes the formatter registered for the given name.

    :param name: The name of a formatter in the registry
    :param revision: string path to the revision file
    :param options: A dict containing kwargs passed to the
        specified formatter.
    :raises: :class:`alembic.util.CommandError`
    zNo formatter with name 'z' registeredN)r
   r   KeyErrorr   CommandError)r   revision_pathoptionshookkes        r   _invoker   .   s_     ,M:M, M7++  &tfL9
	s   	 	AAAc                    |D ]=  }|d   }	 |d   }t        j                  d|d      5  t        || |       ddd       ? y# 1 sw Y   JxY w# t        $ r!}t        j                  d| d|      |d}~ww xY w)	z&Invoke hooks for a generated revision.
_hook_nametypezRunning post write hook T)newlineNzKey 'z;.type' (or 'type' in toml) is required for post write hook )r   statusr   r   r   )pathhooksr   r   type_r   s         r   
_run_hooksr(   F   s    
  +L!	+LE *4(3T + tT*+ +++ +  	##v ''+h0 	s"   AAA		A:A55A:c                    t         | vrt         dz   | z   } t        j                  | t        j                        }|D cg c]  }|j                  t         |       }}|S c c}w )zParse options from a string into a list.

    Also substitutes the revision script token with the actual filename of
    the revision script.

    If the revision script token doesn't occur in the options string, it is
    automatically prepended.
     )posix)REVISION_SCRIPT_TOKENshlexsplitr	   is_posixreplace)cmdline_options_strr%   cmdline_options_listoptions       r   _parse_cmdline_optionsr4   [   so     $773c9<OO ;;6??
 + 	,d3   	s   Ac           
     z    	 | |   S # t         $ r*}t        j                  d| d    d| d| d         |d }~ww xY w)NzKey r!   .z! is required for post write hook )r   r   r   )r   r   r   s      r   _get_required_optionr7   p   sa    t} 7<()4& 1!,/24
 	s    	:%5:c                    |j                  dd       }|j                  dd      }t        ||       }i }|rt        j                  x|d<   |d<   t        j                  g ||fd|i| y )Ncwdr    stdoutstderr)getr4   
subprocessDEVNULLrun)r%   r   ignore_outputcommandr9   r1   r2   kws           r   	_run_hookrD   z   su     kk%.C!++i412EtLB&0&8&888r(|NN4W434D#DDr   console_scriptsc                   t        |d      }t        j                  d      D ]  }|j                  |k(  s|} n t	        j
                  d|       |rKd|j                   d|j                   d| d| d|j                   d	|j                   d
|j                   d}n*d|j                   d|j                   d
|j                   d}t        j                  d|g}t        | |||       y )N
entrypointrE   z*Could not find entrypoint console_scripts.zimport z; assert tuple(int(x) for x in z.__version__.split('.')) >= z, 'need exactly version z of z'; r6   z()z; z-c)r7   r	   importlib_metadata_getr   r   r   moduleattrsys
executablerD   )	r%   r   rA   verify_versionentrypoint_nameentryimplpyscriptrB   s	            r   rE   rE      s    +7LAO../@A 
::(D

 88IJ
 	
 dkk] #,,0KK=8TUcTd e%%3$4D3{{m1TYYKr+ 	 T[[MDKK=$))BG~~tX.GdG]G4r   execc                <    t        |d      }t        | |||g       y )NrL   )rB   )r7   rD   )r%   r   rA   rL   s       r   exec_rT      s    %g|<JdG]ZLAr   rI   c                    t        |d      }t        j                  j                  |      t        j                  d|       t
        j                  d|g}t        | |||       y )NrI   zCould not find module z-m)r7   	importlibr   	find_specr   rK   rL   rD   )r%   r   rA   module_namerB   s        r   rI   rI      sZ    &w9K~~,4"8 FGG~~t[1GdG]G4r   )r   strreturnr   )r   rY   r   str | os.PathLike[str]r   r   rZ   r   )r%   r[   r&   zlist[PostWriteHookConfig]rZ   None)r1   rY   r%   rY   rZ   	list[str])r   r   r   rY   rZ   rY   )
r%   rY   r   r   rA   boolrB   r]   rZ   r\   )FN)
r%   rY   r   r   rA   r^   rM   ztuple[int, ...] | NonerZ   r\   )F)r%   rY   r   r   rA   r^   rZ   r\   )
__future__r   importlib.utilrV   osr-   r>   rK   typingr   r   r   r:   r   r	   util.pyfilesr
   configr   r,   r   __annotations__r   r   r(   r4   r7   rD   rE   rT   rI   r   r   r   <module>rf      sM   #  	   
       2,2 	4 &,
,), !, 		,0+
 +)B+	+* *E
EE-1E<EE	E 

  -1	5
55 5 +	5
 
5 5< 
&B B
 
(5 5r   