a
    g	g.                     @  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
d
ddddZd
d
dddZd
ddddZd
d
dddZd
d
dddZdS )    )annotationsN)	ExitStack)Optional)
exceptions)Template   )compat)CommandErrorstrNone)template_filedestoutput_encodingreturnc           	      K  s   t | d}z|jf i ||}W nd   tjddd.}|t  | |j}W d    n1 sn0    Y  t	d| Y n60 t
|d}|| W d    n1 s0    Y  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   kwtemplateoutputZntffnamef r   W/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/alembic/util/pyfiles.pytemplate_to_file   s$    
$
r!   )r   r   c                 C  sp   t j| sld| v 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_contextas_file)r   tokensZfile_managerreftokr   r   r    coerce_resource_to_filename+   s    


r0   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_pathE   s    r;   )dir_r   c                 C  s   t dd|}tj| |}tj|\}}|dkrvtj|rLt||}qt|}|du rjt	d| qt||}n|dv rt||}|S )z3Load a file from the given path as a Python module.z\W_z.pyNzCan't find Python file %s)z.pycz.pyo)
resubr#   r$   joinr5   r4   load_module_pyr;   ImportError)r<   r   	module_idr$   r=   r:   modulepyc_pathr   r   r    load_python_fileV   s    
rF   )rC   r$   c                 C  s2   t j| |}|sJ t j|}|j| |S )N)r1   r2   spec_from_file_locationmodule_from_specloaderexec_module)rC   r$   specrD   r   r   r    rA   j   s
    rA   )
__future__r   r'   
contextlibr   r1   Zimportlib.machineryimportlib.utilr#   r>   r   typingr   Zmakor   Zmako.templater    r   excr	   r!   r0   r;   rF   rA   r   r   r   r    <module>   s$   