o
    voi                     @  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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rGdd
lmZ dZi Zded< d6ddZd7ddZd8dd Zd9d#d$Zd:d%d&Zd;d*d+Zed,	-	d<d=d0d,Zed1d>d?d2d3Zed4d>d?d5d4ZdS )@    )annotationsN)Any)Callable)TYPE_CHECKING   )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    N/home/ubuntu/.local/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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   _invoke.   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_hooksF   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>i   s    
z*_parse_cmdline_options.<locals>.<listcomp>)r.   shlexsplitr   is_posix)r)   r   cmdline_options_listr   r1   r   _parse_cmdline_options[   s   	
r7   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_optionp   s   
r9   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)getr7   
subprocessDEVNULLrun)r   r   r:   r<   r=   r)   r6   kwr   r   r   	_run_hookz   s   
"rF   console_scriptsFverify_versiontuple[int, ...] | Nonec           	      C  s   t |d}tdD ]}|j|kr|} n	q
td| |r>d|j d|j d| d| d|j d	|j d
|j d}nd|j d|j d
|j d}tj	d|g}t
| ||| d S )N
entrypointrG   z*Could not find entrypoint console_scripts.zimport z; assert tuple(int(x) for x in z.__version__.split('.')) >= z, 'need exactly version z of z'; r8   z()z; z-c)r9   r   importlib_metadata_getr   r   r   moduleattrsys
executablerF   )	r   r   r:   rH   entrypoint_nameentryimplpyscriptr<   r   r   r   rG      s6   


execc                 C  s    t |d}t| |||gd d S )NrO   )r<   )r9   rF   )r   r   r:   rO   r   r   r   exec_   s   
rU   rL   c                 C  sH   t |d}tj|d u rtd| tjd|g}t| ||| d S )NrL   zCould not find module z-m)r9   	importlibr   	find_specr   rN   rO   rF   )r   r   r:   module_namer<   r   r   r   rL      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"   )FN)
r   r   r   r   r:   r;   rH   rI   r   r"   )F)r   r   r   r   r:   r;   r   r"   )
__future__r   importlib.utilrV   osr3   rB   rN   typingr   r   r   r>   r   r   util.pyfilesr	   configr
   r.   r   __annotations__r   r   r(   r7   r9   rF   rG   rU   rL   r   r   r   r   <module>   s<   






