a
    h(                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlZeeZ	ee
jjd ZG dd dejZedddZeed	d
dZg dZedddZdd Zdd Zdd ZdS )    N)Pathlibc                       sH   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	  Z
S )_LazyImporterzLazily import module/extension.c                    s   t  | || _d | _d S N)super__init__import_funcmodule)selfnamer   	__class__ D/var/www/auris/lib/python3.9/site-packages/torio/_extension/utils.pyr      s    z_LazyImporter.__init__c                 C   s   |    t| j|S r   )_import_oncegetattrr	   )r
   itemr   r   r   __getattr__   s    z_LazyImporter.__getattr__c                 C   s4   | j d u r*d| j d| jj d| j dS t| j S )Nz	<module '.z("z")'>)r	   
__module__r   __name__r   reprr
   r   r   r   __repr__   s    
 z_LazyImporter.__repr__c                 C   s   |    t| jS r   )r   dirr	   r   r   r   r   __dir__!   s    z_LazyImporter.__dir__c                 C   s(   | j d u r$|  | _ | j| j j d S r   )r	   r   __dict__updater   r   r   r   r   %   s    

z_LazyImporter._import_oncec                 C   s&   z|    W n ty    Y dS 0 dS )NFT)r   	Exceptionr   r   r   r   is_available.   s
    z_LazyImporter.is_available)r   r   __qualname____doc__r   r   r   r   r   r   __classcell__r   r   r   r   r      s   	r   )r   c                 C   s(   t jdkrdnd}t|  d|  }|S )NntZpydsor   )osr   _LIB_DIR)r   suffixpathr   r   r   _get_lib_path6   s    r)   )r   returnc                 C   s$   t | }| sdS tj| dS )a  Load extension module

    Note:
        In case `torio` is deployed with `pex` format, the library file
        is not in a standard location.
        In this case, we expect that `libtorio` is available somewhere
        in the search path of dynamic loading mechanism, so that importing
        `_torio` will have library loader find and load `libtorio`.
        This is the reason why the function should not raising an error when the library
        file is not found.

    Returns:
        bool:
            True if the library file is found AND the library loaded without failure.
            False if the library file is not found (like in the case where torio
            is deployed with pex format, thus the shared library file is
            in a non-standard location.).
            If the library file is found but there is an issue loading the library,
            (such as missing dependency) then this function raises the exception as-is.

    Raises:
        Exception:
            If the library file is found, but there is an issue loading the library file,
            (when underlying `ctype.DLL` throws an exception), this function will pass
            the exception as-is, instead of catching it and returning bool.
            The expected case is `OSError` thrown by `ctype.DLL` when a dynamic dependency
            is not found.
            This behavior was chosen because the expected failure case is not recoverable.
            If a dependency is missing, then users have to install it.
    FT)r)   existstorchopsZload_library)r   r(   r   r   r   	_load_lib<   s
    r.   )654 )versionc                 C   sB   d|  }d|  }t j|s0td|  dt| t |S )Nztorio.lib._torio_ffmpegZlibtorio_ffmpegZFFmpegz extension is not available.)	importlibutil	find_specRuntimeErrorr.   import_module)r3   extr   r   r   r   !_find_versionsed_ffmpeg_extensione   s    

r:   c              	   C   sp   | D ]V}t d| zt|}t d| |W   S  tyX   t jd|dd Y qY q0 qtd|  dd S )NzLoading FFmpeg%szSuccessfully loaded FFmpeg%sz"Failed to load FFmpeg%s extension.T)exc_infoz6Failed to intialize FFmpeg extension. Tried versions: z;. Enable DEBUG logging to see more details about the error.)_LGdebugr:   r   ImportError)ffmpeg_vers
ffmpeg_verr9   r   r   r   _find_ffmpeg_extensionp   s    

rA   c                  C   s>   t } tjd }d ur:|| vr4td| d|  |g} | S )NZTORIO_USE_FFMPEG_VERSIONzThe FFmpeg version 'z[' (read from TORIO_USE_FFMPEG_VERSION) is not one of supported values. Possible values are )_FFMPEG_VERSr%   environget
ValueError)r?   r@   r   r   r   _get_ffmpeg_versions   s    rF   c                  C   s0   t  } t| }|  | dkr,|d |S )N   )rF   rA   initZget_log_levelZset_log_level)r?   r9   r   r   r   _init_ffmpeg   s    
rI   )r4   loggingr%   typespathlibr   r,   	getLoggerr   r<   __file__parentr&   
ModuleTyper   strr)   boolr.   rB   r:   rA   rF   rI   r   r   r   r   <module>   s   
)&