U
    ^g%i                     @  s*  U 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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Zi Zded< dddddZdddddddZddddddZdddd d!d"Zed#d+ddd%dd&d'd#Zed(d,ddd%dd&d)d*ZdS )-    )annotationsN)Any)Callable)Dict)List)Mapping)Optional)Union   )util)compatZREVISION_SCRIPT_FILENAMEdict	_registrystrr   )namereturnc                   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    Y/var/www/html/api-medvista/venv/lib/python3.8/site-packages/alembic/script/write_hooks.pydecorate&   s    zregister.<locals>.decorater   )r   r   r   r   r   register   s    r   zMapping[str, Union[str, int]]r   )r   revisionoptionsr   c              
   C  sR   zt |  }W n6 tk
rB } ztd|  d|W 5 d}~X Y nX |||S dS )a7  Invokes the formatter registered for the given name.

    :param name: The name of a formatter in the registry
    :param revision: A :class:`.MigrationRevision` instance
    :param options: A dict containing kwargs passed to the
        specified formatter.
    :raises: :class:`alembic.util.CommandError`
    zNo formatter with name 'z' registeredN)r   KeyErrorr   CommandError)r   r   r   hookker   r   r   _invoke-   s    
r   zMapping[str, str]None)pathhook_configr   c                   s   ddl m} | dd}|D ]s,q" fdd D }|d< z|d }W n: tk
r } ztd	 d
|W 5 d}~X Y q"X tjddd t|| | W 5 Q R X q"dS )z&Invoke hooks for a generated revision.   )_split_on_space_commahooks c                   s4   i | ],}| d  r|td d  | qS ).r"   N)
startswithlen).0keyr!   r   r   r   
<dictcomp>L   s    z_run_hooks.<locals>.<dictcomp>
_hook_nametypeKey z&.type is required for post write hook NzRunning post write hook T)newline)	baser#   splitgetr   r   r   statusr   )r    r!   r#   namesoptstype_r   r   r+   r   
_run_hooksB   s,     r8   z	List[str])cmdline_options_strr    r   c                   s:   t | k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)r)   optionr    r   r   
<listcomp>m   s   z*_parse_cmdline_options.<locals>.<listcomp>)r=   shlexr2   r   Zis_posix)r9   r    cmdline_options_listr   r?   r   _parse_cmdline_options_   s    	 
rC   console_scriptsFbool)r    r   ignore_outputr   c              
   C  s   z|d }W nB t k
rN } z$td|d  d|d |W 5 d }~X Y nX tdD ]}|j|krZ|} qqZtd| |dd }|dd	}t|| }	i }
|rtj	 |
d
< |
d< tj
tjdd|j d|j d|j dg|	 fd|i|
 d S )N
entrypointr/   r-   z,.entrypoint is required for post write hook rD   z*Could not find entrypoint console_scripts.cwdr   r%   stdoutstderrz-czimport z; r&   z())r   r   r   r   Zimportlib_metadata_getr   r3   rC   
subprocessDEVNULLrunsys
executablemoduleattr)r    r   rF   Zentrypoint_namer   entryimplrH   r9   rB   kwr   r   r   rD   t   sB    

execc           	   
   C  s   z|d }W nB t k
rN } z$td|d  d|d |W 5 d }~X Y nX |dd }|dd}t|| }i }|rtj |d< |d	< tj|f|fd|i| d S )
NrO   r/   r-   z,.executable is required for post write hook rH   r   r%   rI   rJ   )r   r   r   r3   rC   rK   rL   rM   )	r    r   rF   rO   r   rH   r9   rB   rT   r   r   r   exec_   s0    
rV   )F)F)
__future__r   rA   rK   rN   typingr   r   r   r   r   r   r	   r%   r   r   r=   r   __annotations__r   r   r8   rC   rD   rV   r   r   r   r   <module>   s0     &