U
    ^g%i                     @  s  d dl mZ d dlZ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
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ddddddZdddddZdddddZddddddZddddddZdS )     )annotationsN)	ExitStack)
ModuleType)Any)Optional)
exceptions)Template   )compat)CommandErrorstrr   None)template_filedestoutput_encodingkwreturnc           	      K  s   t | d}z|jf ||}W nP   tjddd$}|t  | |j}W 5 Q R X t	d| Y n"X t
|d}|| W 5 Q R X d S )N)filenamez.txtF)suffixdeletezDTemplate rendering failed; see %s for a template-oriented traceback.wb)r   Zrender_unicodeencodetempfileNamedTemporaryFilewriter   Ztext_error_templatenamer   open)	r   r   r   r   templateoutputZntffnamef r!   S/var/www/html/api-medvista/venv/lib/python3.8/site-packages/alembic/util/pyfiles.pytemplate_to_file   s$    

r#   )r   r   c                 C  sp   t j| sld| krl| d}t }t|j tj	
|d }|dd D ]}|| }qL|tj	|} | S )zInterpret a filename as either a filesystem location or as a package
    resource.

    Names that are non absolute paths and contain a colon
    are interpreted as resources and coerced to a file location.

    :r   r	   N)ospathisabssplitr   atexitregistercloser
   Zimportlib_resourcesfilesenter_contextZas_file)r   tokensZfile_managerreftokr!   r!   r"   coerce_resource_to_filename-   s    


r1   zOptional[str])r&   r   c                 C  sZ   t j| }tj|r|S tj| \}}t jjD ] }tj|| r4||   S q4dS )z,Given a python source path, locate the .pyc.N)		importlibutilcache_from_sourcer%   r&   existssplitext	machineryBYTECODE_SUFFIXES)r&   	candidatefilepathextr!   r!   r"   pyc_file_from_pathF   s    r<   r   )dir_r   r   c                 C  s   t dd|}tj| |}tj|\}}|dkrvtj|rLt||}qt|}|dkrjt	d| qt||}n|dkrt||}ndst
|S )z3Load a file from the given path as a Python module.z\W_z.pyNzCan't find Python file %s)z.pycz.pyoF)resubr%   r&   joinr6   r5   load_module_pyr<   ImportErrorAssertionError)r=   r   	module_idr&   r>   r;   modulepyc_pathr!   r!   r"   load_python_fileW   s    rH   )rE   r&   r   c                 C  s2   t j| |}|stt j|}|j| |S )N)r2   r3   spec_from_file_locationrD   module_from_specloaderexec_module)rE   r&   specrF   r!   r!   r"   rB   m   s
    rB   )
__future__r   r)   
contextlibr   r2   Zimportlib.machineryimportlib.utilr%   r?   r   typesr   typingr   r   makor   Zmako.templater    r
   excr   r#   r1   r<   rH   rB   r!   r!   r!   r"   <module>   s(   