o
    8 h                     @  sL  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 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< d8ddZd9ddZd:d#d$Zd;d'd(Zd<d)d*Zd=d.d/Zed0	1d>d?d2d0Z ed3d>d?d4d5Z!ed6d>d?d7d6Z"dS )@    )annotationsN)Any)Callable)Dict)List)Optional)TYPE_CHECKING)Union   )util)compat)_preserving_path_as_str)PostWriteHookConfigZ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    b/var/www/html/figdemos/bartoux_crm/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   _invoke2   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_hooksJ   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>m   s    
z*_parse_cmdline_options.<locals>.<listcomp>)r1   shlexsplitr   is_posix)r,   r"   cmdline_options_listr   r4   r   _parse_cmdline_options_   s   	
r:   c                 C  sJ   z| | W S  t y$ } ztd| d  d| d| d |d }~ww )NzKey r&   .z! is required for post write hook )r   r   r   )r   r   r    r   r   r   _get_required_optiont   s   
r<   ignore_outputboolcommandc                 C  s^   | dd }| dd}t|| }i }|rtj |d< |d< tjg ||fd|i| d S )Ncwdr    stdoutstderr)getr:   
subprocessDEVNULLrun)r"   r   r=   r?   r@   r,   r9   kwr   r   r   	_run_hook~   s   
"rI   console_scriptsFc              	   C  st   t |d}tdD ]}|j|kr|} n	q
td| tjdd|j d|j d|j	 dg}t
| ||| d S )	N
entrypointrJ   z*Could not find entrypoint console_scripts.z-czimport z; r;   z())r<   r   importlib_metadata_getr   r   r   sys
executablemoduleattrrI   )r"   r   r=   Zentrypoint_nameentryimplr?   r   r   r   rJ      s   

execc                 C  s    t |d}t| |||gd d S )NrN   )r?   )r<   rI   )r"   r   r=   rN   r   r   r   exec_   s   
rT   rO   c                 C  sH   t |d}tj|d u rtd| tjd|g}t| ||| d S )NrO   zCould not find module z-m)r<   	importlibr   	find_specr   rM   rN   rI   )r"   r   r=   module_namer?   r   r   r   rO      s
   
)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   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   importlib.utilrU   osr6   rE   rM   typingr   r   r   r   r   r   r	   rA   r   r   util.pyfilesr   configr   r1   r   __annotations__r   r!   r+   r:   r<   rI   rJ   rT   rO   r   r   r   r   <module>   sB   






