o
    ODi                     @  s  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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 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< d1ddZd2ddZd3d#d$Zd4d'd(Zed)	*d5d6d-d)Zed.d5d6d/d0ZdS )7    )annotationsN)Any)Callable)Dict)List)Optional)TYPE_CHECKING)Union   )util)compat)_preserving_path_as_str)PostWriteHookConfigREVISION_SCRIPT_FILENAMEdict	_registrynamestrreturnr   c                   s    fd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                   s   | t  < | S )N)r   )fnr    S/var/www/Datamplify/venv/lib/python3.10/site-packages/alembic/script/write_hooks.pydecorate*   s   zregister.<locals>.decorater   )r   r   r   r   r   register   s   r   revision_pathUnion[str, os.PathLike[str]]optionsr   r   c              
   C  sL   t |}zt|  }W n ty  } z
td|  d|d}~ww |||S )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   r   r   hookker   r   r   _invoke1   s   

r"   pathhookslist[PostWriteHookConfig]Nonec                 C  s   |D ]E}|d }z|d }W n t y& } ztd| d||d}~ww tjd|dd t|| | W d   n1 sBw   Y  qdS )	z&Invoke hooks for a generated revision.
_hook_nametypezKey 'z;.type' (or 'type' in toml) is required for post write hook NzRunning post write hook T)newline)r   r   r   statusr"   )r#   r$   r    r   type_r!   r   r   r   
_run_hooksI   s*   
r,   cmdline_options_str	List[str]c                   s:   t | vr
t d |  } tj| tjd} fdd|D }|S )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.
     )posixc                   s   g | ]}| t qS r   )replaceREVISION_SCRIPT_TOKEN).0optionr#   r   r   
<listcomp>l   s    
z*_parse_cmdline_options.<locals>.<listcomp>)r2   shlexsplitr   is_posix)r-   r#   cmdline_options_listr   r5   r   _parse_cmdline_options^   s   	
r;   console_scriptsFignore_outputboolc              
   C  s   z|d }W n t y" } ztd|d  d|d |d }~ww tdD ]}|j|kr3|} n	q(td| |dd }|dd	}t|| }	i }
|rZtj	 |
d
< |
d< tj
tjdd|j d|j d|j dg|	 fd|i|
 d S )N
entrypointKey r'   z,.entrypoint is required for post write hook r<   z*Could not find entrypoint console_scripts.cwdr    stdoutstderrz-czimport z; .z())r   r   r   r   importlib_metadata_getr   getr;   
subprocessDEVNULLrunsys
executablemoduleattr)r#   r   r=   entrypoint_namer!   entryimplrA   r-   r:   kwr   r   r   r<   s   sL   


execc           	   
   C  s   z|d }W n t y" } ztd|d  d|d |d }~ww |dd }|dd}t|| }i }|rAtj |d< |d	< tj|g|fd|i| d S )
NrL   r@   r'   z,.executable is required for post write hook rA   r   rB   rC   rD   )r   r   r   rG   r;   rH   rI   rJ   )	r#   r   r=   rL   r!   rA   r-   r:   rR   r   r   r   exec_   s8   

rT   )r   r   r   r   )r   r   r   r   r   r   r   r   )r#   r   r$   r%   r   r&   )r-   r   r#   r   r   r.   )F)r#   r   r   r   r=   r>   r   r&   )
__future__r   osr7   rH   rK   typingr   r   r   r   r   r   r	   rB   r   r   util.pyfilesr   configr   r2   r   __annotations__r   r"   r,   r;   r<   rT   r   r   r   r   <module>   s8   



&