o
    NK&hC                     @   s   d dl Z d dlmZ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mZ d dlmZ d	ed
efddZ	ddeeeef defddZdS )    N)module_from_specspec_from_file_location)environ)Path)findall)Union)LoadFileExceptionPyFileError)import_stringvalreturnc                 C   s.   |   } | dv r
dS | dv rdS td|  )ad  Takes string and tries to turn it into bool as human would do.

    If val is in case insensitive (
        "y", "yes", "yep", "yup", "t",
        "true", "on", "enable", "enabled", "1"
    ) returns True.
    If val is in case insensitive (
        "n", "no", "f", "false", "off", "disable", "disabled", "0"
    ) returns False.
    Else Raise ValueError.>
   1tyonyepyesyuptrueenableenabledT>   0fnnoofffalsedisabledisabledFzInvalid truth value )lower
ValueError)r    r!   >/var/www/html/venv/lib/python3.10/site-packages/sanic/utils.pystr_to_bool   s   r#   utf8locationencodingc              
   O   s  t | tr
| |} t | tsd| v sd| v rt | tsGttd| }|t }|r6t	dd
| |D ]}| d| d t| } q8t| } d| v ry| dd	 d
d }t|| g|R i |}|dusmJ t|}	|j|	 |	S td}	t| |	_z%t| }
tt|
 | d|	j W d   W |	S 1 sw   Y  W |	S  ty } zd|_ d}~w ty } zt| |d}~ww zt| W S  ty   tdt|  w )a  Returns loaded module provided as a file path.

    :param args:
        Corresponds to importlib.util.spec_from_file_location location
        parameters,but with this differences:
        - It has to be of a string or bytes type.
        - You can also use here environment variables
          in format ${some_env_var}.
          Mark that $some_env_var will not be resolved as environment variable.
    :encoding:
        If location parameter is of a bytes type, then use this encoding
        to decode it into string.
    :param args:
        Corresponds to the rest of importlib.util.spec_from_file_location
        parameters.
    :param kwargs:
        Corresponds to the rest of importlib.util.spec_from_file_location
        parameters.

    For example You can:

        some_module = load_module_from_file_location(
            "some_module_name",
            "/some/path/${some_env_var}"
        )
    /$z	\${(.+?)}z1The following environment variables are not set: z, z${}z.py.r   Nconfigexecz.Unable to load configuration file (e.strerror)zUnable to load configuration %s) 
isinstancebytesdecoder   set
re_findall
difference
os_environkeysr   joinreplacestrsplitr   r   loaderexec_moduletypes
ModuleType__file__openr-   compileread__dict__OSErrorstrerror	Exceptionr	   r
   r    )r%   r&   argskwargsenv_vars_in_locationnot_defined_env_varsenv_varname	_mod_specmoduleconfig_fileer!   r!   r"   load_module_from_file_location-   sv   







rP   )r$   )r<   importlib.utilr   r   osr   r4   pathlibr   rer   r2   typingr   sanic.exceptionsr   r	   sanic.helpersr
   r8   boolr#   r/   rP   r!   r!   r!   r"   <module>   s    !