o
    ‹å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
lmZ d	dlmZ d%dd„Zd&dd„Zd'dd„Zd(d d!„Zd)d#d$„ZdS )*é    )ÚannotationsN)Ú	ExitStack)Ú
ModuleType)ÚAny)ÚOptional)Ú
exceptions)ÚTemplateé   )Úcompat)ÚCommandErrorÚtemplate_fileÚstrÚdestÚoutput_encodingÚkwr   ÚreturnÚNonec           	      K  s¾   t | d�}z|jdi |¤Ž |¡}W n/   tjddd��}| t ¡  ¡  |¡¡ |j}W d   ƒ n1 s7w   Y  t	d| ƒ‚t
|dƒ�}| |¡ W d   ƒ d S 1 sXw   Y  d S )N)Úfilenamez.txtF)ÚsuffixÚdeletezDTemplate rendering failed; see %s for a template-oriented traceback.Úwb© )r   Úrender_unicodeÚencodeÚtempfileÚNamedTemporaryFileÚwriter   Útext_error_templateÚnamer   Úopen)	r   r   r   r   ÚtemplateÚoutputÚntfÚfnameÚfr   r   úP/var/www/html/flaskapp/venv/lib/python3.10/site-packages/alembic/util/pyfiles.pyÚtemplate_to_file   s(   
ýúÿÿ"ÿr&   r#   c                 C  sp   t j | ¡s6d| v r6|  d¡}tƒ }t |j¡ tj	 
|d ¡}|dd… D ]}|| }q&| tj	 |¡¡} | S )zÔInterpret 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
   Úimportlib_resourcesÚfilesÚenter_contextÚas_file)r#   ÚtokensÚfile_managerÚrefÚtokr   r   r%   Úcoerce_resource_to_filename-   s   


ÿr7   r)   úOptional[str]c                 C  sZ   t j | ¡}tj |¡r|S tj | ¡\}}t jjD ]}tj || ¡r*||   S qd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   ÿrC   Údir_r   r   c                 C  s’   t  dd|¡}tj | |¡}tj |¡\}}|dkr<tj |¡r't||ƒ}|S t|ƒ}|du r5t	d| ƒ‚t||ƒ}|S |dv rGt||ƒ}|S J ‚)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)   Újoinr=   r<   Úload_module_pyrC   ÚImportError)rD   r   Ú	module_idr)   rE   rB   ÚmoduleÚpyc_pathr   r   r%   Úload_python_fileW   s    
÷
ü
ÿrN   rK   c                 C  s2   t j | |¡}|sJ ‚t j |¡}|j |¡ |S )N)r9   r:   Úspec_from_file_locationÚmodule_from_specÚloaderÚexec_module)rK   r)   ÚspecrL   r   r   r%   rI   m   s
   rI   )
r   r   r   r   r   r   r   r   r   r   )r#   r   r   r   )r)   r   r   r8   )rD   r   r   r   r   r   )rK   r   r)   r   r   r   )Ú
__future__r   r,   Ú
contextlibr   r9   Úimportlib.machineryÚimportlib.utilr(   rF   r   Útypesr   Útypingr   r   Úmakor   Úmako.templater   Ú r
   Úexcr   r&   r7   rC   rN   rI   r   r   r   r%   Ú<module>   s*    



