o
    Ph                     @  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)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    _/var/www/html/stock_analysis/be/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>)r2   shlexsplitr   is_posix)r-   r#   cmdline_options_listr   r5   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@   rA   r-   r:   kwr   r   r   	_run_hook~   s   
"rJ   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
entrypointrK   z*Could not find entrypoint console_scripts.z-czimport z; r<   z())r=   r   importlib_metadata_getr   r   r   sys
executablemoduleattrrJ   )r#   r   r>   entrypoint_nameentryimplr@   r   r   r   rK      s   

execc                 C  s    t |d}t| |||gd d S )NrO   )r@   )r=   rJ   )r#   r   r>   rO   r   r   r   exec_   s   
rV   rP   c                 C  sH   t |d}tj|d u rtd| tjd|g}t| ||| d S )NrP   zCould not find module z-m)r=   	importlibr   	find_specr   rN   rO   rJ   )r#   r   r>   module_namer@   r   r   r   rP      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.utilrW   osr7   rF   rN   typingr   r   r   r   r   r   r	   rB   r   r   util.pyfilesr   configr   r2   r   __annotations__r   r"   r,   r;   r=   rJ   rK   rV   rP   r   r   r   r   <module>   sB   






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