o
    h-                     @  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Zd!ddZd"ddZd#ddZd$ddZdS )%    )annotationsN)	ExitStack)Optional)
exceptions)Template   )compat)CommandErrortemplate_filestrdestoutput_encoding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   kwtemplateoutputntffnamefr   r   W/var/www/html/aiguide_backend/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 )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   importlib_resourcesfilesenter_contextas_file)r!   tokensfile_managerreftokr   r   r#   coerce_resource_to_filename+   s   


r5   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_pathD   s   rA   dir_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 rEt||}|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'   joinr;   r:   load_module_pyrA   ImportError)rB   r   	module_idr'   rC   r@   modulepyc_pathr   r   r#   load_python_fileU   s   
	

rL   rI   c                 C  s2   t j| |}|sJ t j|}|j| |S )N)r7   r8   spec_from_file_locationmodule_from_specloaderexec_module)rI   r'   specrJ   r   r   r#   rG   i   s
   rG   )r
   r   r   r   r   r   r   r   )r!   r   r   r   )r'   r   r   r6   )rB   r   r   r   )rI   r   r'   r   )
__future__r   r*   
contextlibr   r7   importlib.machineryimportlib.utilr&   rD   r   typingr   makor   mako.templater    r   excr	   r$   r5   rA   rL   rG   r   r   r   r#   <module>   s&    



