o
    voic                     @  s   d dl mZ d dlZ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Zd/ddZd0d"d#Zd1d'd(Zd2d*d+Zd3d,d-ZdS )4    )annotationsN)	ExitStack)	resources)
ModuleType)Any)Optional)Union)
exceptions)Template   )CommandErrorF)append_with_newlinestemplate_fileUnion[str, os.PathLike[str]]destoutput_encodingstrr   boolkwr   returnNonec          
      K  s   t t| d}z|jd	i ||}W n/   tjddd}|t  | |j	}W d    n1 s9w   Y  t
d| t||rJdnd}	|rX|	d| |	| W d    d S 1 shw   Y  d S )
N)filenamez.txtF)suffixdeletezDTemplate rendering failed; see %s for a template-oriented traceback.abwbz

 )r
   _preserving_path_as_strrender_unicodeencodetempfileNamedTemporaryFilewriter	   text_error_templatenamer   open)
r   r   r   r   r   templateoutputntffnamefr   r   H/home/ubuntu/.local/lib/python3.10/site-packages/alembic/util/pyfiles.pytemplate_to_file   s,   "r,   fname_or_resourcepathlib.Pathc                 C  sr   t j| s4d| v r4| d}t }t|j t	|d }|dd D ]}|| }q%|
t|} t| 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   filesenter_contextas_filepathlibPath)r-   tokensfile_managerreftokr   r   r+   coerce_resource_to_filename6   s   	


r@   r1   Optional[pathlib.Path]c                 C  s`   t | }t tj| }| r|S |j}tjj	D ]}|
| r-|
|  S qdS )z,Given a python source path, locate the .pyc.N)r:   r;   	importlibutilcache_from_sourceas_posixexistsr   	machineryBYTECODE_SUFFIXESwith_suffix)r1   pathpath	candidateextr   r   r+   pyc_file_from_pathP   s   
rM   dir_r   r   c                 C  s   t | } t |}|j}tdd|}| | }|j}|dkr?| r*t||}|S t|}|du r8t	d| t||}|S |dv rJt||}|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)
r:   r;   r$   resubr   rF   load_module_pyrM   ImportError)rN   r   filename_as_path	module_idr1   rL   modulepyc_pathr   r   r+   load_python_filef   s&   




rX   rU   c                 C  s2   t j| |}|sJ t j|}|j| |S )N)rB   rC   spec_from_file_locationmodule_from_specloaderexec_module)rU   r1   specrV   r   r   r+   rR      s
   rR   c                 C  s0   t | tr| S t | tjrt| S tt| S )zreceive str/pathlike and return a string.

    Does not convert an incoming string path to a Path first, to help with
    unit tests that are doing string path round trips without OS-specific
    processing if not necessary.

    )
isinstancer   r:   PurePathr;   )r1   r   r   r+   r      s
   
r   )r   r   r   r   r   r   r   r   r   r   r   r   )r-   r   r   r.   )r1   r   r   rA   )rN   r   r   r   r   r   )rU   r   r1   r   r   r   )r1   r   r   r   )
__future__r   r4   
contextlibr   rB   r   importlib.machineryimportlib.utilr0   r:   rP   r    typesr   typingr   r   r   makor	   mako.templater
   excr   r,   r@   rM   rX   rR   r   r   r   r   r+   <module>   s2    




