U of @sddlmZddlZddlmZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZddl mZddlmZddlmZd d lmZd d lmZd d d d ddddZd d dddZd ddddZd d ddddZd d ddddZdS) ) annotationsN) ExitStack) ModuleType)Any)Optional) exceptions)Template)compat) CommandErrorstrrNone) template_filedestoutput_encodingkwreturnc Kst|d}z|jf||}WnPtjddd$}|t||j}W5QRXt d|Yn"Xt |d}||W5QRXdS)N)filenamez.txtF)suffixdeletezDTemplate rendering failed; see %s for a template-oriented traceback.wb) rZrender_unicodeencodetempfileNamedTemporaryFilewriterZtext_error_templatenamer open) rrrrtemplateoutputZntffnamefr!B/opt/hc_python/lib/python3.8/site-packages/alembic/util/pyfiles.pytemplate_to_files$    r#)rrcCsptj|sld|krl|d}t}t|jtj |d}|ddD] }||}qL| tj |}|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. :rr N) ospathisabssplitratexitregistercloser Zimportlib_resourcesfiles enter_contextZas_file)rtokensZ file_managerreftokr!r!r"coerce_resource_to_filename-s    r1z Optional[str])r&rcCsZtj|}tj|r|Stj|\}}tjjD] }tj||r4||Sq4dS)z,Given a python source path, locate the .pyc.N) importlibutilcache_from_sourcer%r&existssplitext machineryBYTECODE_SUFFIXES)r& candidatefilepathextr!r!r"pyc_file_from_pathFs   r<r)dir_rrcCstdd|}tj||}tj|\}}|dkrvtj|rLt||}qt|}|dkrjt d|qt||}n|dkrt||}ndst |S)z3Load a file from the given path as a Python module.z\W_z.pyNzCan't find Python file %s)z.pycz.pyoF) resubr%r&joinr6r5load_module_pyr< ImportErrorAssertionError)r=r module_idr&r>r;modulepyc_pathr!r!r"load_python_fileWs    rH)rEr&rcCs2tj||}|sttj|}|j||S)N)r2r3spec_from_file_locationrDmodule_from_specloader exec_module)rEr&specrFr!r!r"rBms   rB) __future__rr) contextlibrr2Zimportlib.machineryimportlib.utilr%r?rtypesrtypingrrmakorZ mako.templaterr excr r#r1r<rHrBr!r!r!r"s(