o
    h                     @  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Zd/ddZd0d d!Zd1d$d%Zed&	'd2d3d*d&Zed+d2d3d,d-ZdS )4    )annotationsN)Any)Callable)Dict)List)Mapping)Optional)Union   )util)compat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    n/var/www/html/construction_image-detection-poc/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optionsMapping[str, Union[str, int]]r   c              
   C  sD   zt |  }W n ty } z
td|  d|d}~ww |||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    pathhook_configMapping[str, str]Nonec                   s   ddl m} | dd}|D ]Rsq fdd D }|d< z|d }W n tyB } ztd	 d
|d}~ww tjddd t|| | W d   n1 s^w   Y  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   s4   
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)r,   optionr!   r   r   
<listcomp>m   s    
z*_parse_cmdline_options.<locals>.<listcomp>)rA   shlexr5   r   is_posix)r<   r!   cmdline_options_listr   rC   r   _parse_cmdline_options_   s   	
rH   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
entrypointr2   r0   z,.entrypoint is required for post write hook rI   z*Could not find entrypoint console_scripts.cwdr   r(   stdoutstderrz-czimport z; r)   z())r   r   r   r   importlib_metadata_getr   r6   rH   
subprocessDEVNULLrunsys
executablemoduleattr)r!   r   rJ   entrypoint_namer   entryimplrM   r<   rG   kwr   r   r   rI   t   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 )
NrU   r2   r0   z,.executable is required for post write hook rM   r   r(   rN   rO   )r   r   r   r6   rH   rQ   rR   rS   )	r!   r   rJ   rU   r   rM   r<   rG   r[   r   r   r   exec_   s8   

r]   )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   rJ   rK   r   r$   )
__future__r   rE   rQ   rT   typingr   r   r   r   r   r   r	   r(   r   r   rA   r   __annotations__r   r    r;   rH   rI   r]   r   r   r   r   <module>   s0   



&