a
    g	gt                     @  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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dS )(    )annotationsN)Any)Callable)Dict)List)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.

    .. versionadded:: 1.2.0

    .. seealso::

        :ref:`post_write_hooks_custom`


    c                   s   | t  < | S )N)r   )fnr    ]/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/alembic/script/write_hooks.pydecorate$   s    zregister.<locals>.decorater   )r   r   r   r   r   register   s    r   zDict[str, Union[str, int]]r   )r   revisionoptionsr   c              
   C  sP   zt |  }W n4 ty@ } ztd|  |W Y d}~nd}~0 0 |||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 '%s' registeredN)r   KeyErrorr
   CommandError)r   r   r   hookker   r   r   _invoke+   s    r   zDict[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 n8 ty } z td	f |W Y d
}~q"d
}~0 0 tjd t|| |dd 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>J   s   z_run_hooks.<locals>.<dictcomp>
_hook_nametypez.Key %s.type is required for post write hook %rNzRunning post write hook "%s"T)newline)	baser"   splitgetr   r
   r   statusr   )r   r    r"   namesoptstype_r   r   r*   r   
_run_hooks@   s2    
r6   z	List[str])cmdline_options_strr   r   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)r(   optionr   r   r   
<listcomp>o   s   z*_parse_cmdline_options.<locals>.<listcomp>)r;   shlexr0   r   Zis_posix)r7   r   cmdline_options_listr   r=   r   _parse_cmdline_optionsa   s    	
rA   console_scriptsFbool)r   r   ignore_outputr   c              
   C  s   z|d }W n@ t yL } z(td|d |d f |W Y d }~n
d }~0 0 tdD ]}|j|krX|} qqXtd| |dd }|dd}t|| }	i }
|rtj	 |
d	< |
d
< tj
tjdd|j|j|jf g|	 fd|i|
 d S )NZ
entrypointz4Key %s.entrypoint is required for post write hook %rr,   rB   z*Could not find entrypoint console_scripts.cwdr   r$   stdoutstderrz-czimport %s; %s.%s())r   r
   r   r   Zimportlib_metadata_getr   r1   rA   
subprocessDEVNULLrunsys
executablemoduleattr)r   r   rD   Zentrypoint_namer   entryimplrE   r7   r@   kwr   r   r   rB   v   sF    

)F)
__future__r   r?   rH   rK   typingr   r   r   r   r   r   r$   r
   r   r;   r   __annotations__r   r   r6   rA   rB   r   r   r   r   <module>   s(   ! 