U
    %d9                     @   s   d dl Zd dlZd dlmZ d dlmZ d dlZee	dddZ
eddd	Zdeee d
ddZdd Zdd Zdd Ze Zdd Zdd Zdd Zdd ZdS )    Nwraps)Optional)modulesreturnc                  G   s   t dd | D S )aP  Returns if a top-level module with :attr:`name` exists *without**
    importing it. This is generally safer than try-catch block around a
    `import X`. It avoids third party libraries breaking assumptions of some of
    our tests, e.g., setting multiprocessing start method when imported
    (see librosa/#747, torchvision/#544).
    c                 s   s   | ]}t j|d k	V  qd S N)	importlibutil	find_spec.0m r   E/tmp/pip-unpacked-wheel-lbdmvq91/torchaudio/_internal/module_utils.py	<genexpr>   s     z&is_module_available.<locals>.<genexpr>)allr   r   r   r   is_module_available	   s    r   r   c                     sP   dd | D }|sdd }n0t |dkr6d|d  nd|   fd	d}|S )
zDecorate function to give error message if invoked without required optional modules.

    This decorator is to give better error message to users rather
    than raising ``NameError:  name 'module' is not defined`` at random places.
    c                 S   s   g | ]}t |s|qS r   )r   r   r   r   r   
<listcomp>   s      z#requires_module.<locals>.<listcomp>c                 S   s   | S r   r   funcr   r   r   	decorator   s    z"requires_module.<locals>.decorator   zmodule: r   z	modules: c                    s   t   fdd}|S )Nc                     s    t  j d j d d S )N.z
 requires RuntimeError
__module____name__argskwargs)r   reqr   r   wrapped$   s    z3requires_module.<locals>.decorator.<locals>.wrappedr   r   r"   r!   r   r   r   #   s    )len)r   missingr   r   r$   r   requires_module   s    
$r'   	directionversionc                    s    fdd}|S )zDecorator to add deprecation message

    Args:
        direction (str): Migration steps to be given to users.
        version (str or int): The version when the object will be removed
    c                    s   t   fdd}|S )Nc                     sB   j  dj dd krdn d  }tj|dd | |S )Nr   z. has been deprecated and will be removed from futurez
 release.    )
stacklevel)r   r   warningswarn)r   r    message)r)   r   r*   r   r   r"   6   s    (z.deprecated.<locals>.decorator.<locals>.wrappedr   r#   r(   r   r   r   5   s    	zdeprecated.<locals>.decoratorr   )r)   r*   r   r   r(   r   
deprecated-   s    r1   c                   C   s   t dotjj S Nztorchaudio._torchaudio)r   torchops
torchaudiois_kaldi_availabler   r   r   r   r6   E   s    r6   c                  C   s   t  rdd } ndd } | S )Nc                 S   s   | S r   r   r   r   r   r   r   L   s    z!requires_kaldi.<locals>.decoratorc                    s   t   fdd}|S )Nc                     s   t  j d j dd S )Nr   z requires kaldir   r   r   r   r   r"   R   s    z2requires_kaldi.<locals>.decorator.<locals>.wrappedr   r#   r   r   r   r   Q   s    )r6   r   r   r   r   requires_kaldiI   s    
r8   c                  C   s@   t dsdS zdd l} W dS  tk
r:   td Y dS X d S )N	soundfileFr   TzAFailed to import soundfile. 'soundfile' backend is not available.)r   r9   	Exceptionr.   r/   )r9   r   r   r   _check_soundfile_importable[   s    
r;   c                   C   s   t S r   )_is_soundfile_importabler   r   r   r   is_soundfile_availablej   s    r=   c                  C   s   t  rdd } ndd } | S )Nc                 S   s   | S r   r   r   r   r   r   r   q   s    z%requires_soundfile.<locals>.decoratorc                    s   t   fdd}|S )Nc                     s   t  j d j dd S )Nr   z requires soundfiler   r   r   r   r   r"   w   s    z6requires_soundfile.<locals>.decorator.<locals>.wrappedr   r#   r   r   r   r   v   s    )r=   r7   r   r   r   requires_soundfilen   s    
r>   c                   C   s   t dotjj S r2   )r   r3   r4   r5   is_sox_availabler   r   r   r   r?      s    r?   c                  C   s   t  rdd } ndd } | S )Nc                 S   s   | S r   r   r   r   r   r   r      s    zrequires_sox.<locals>.decoratorc                    s   t   fdd}|S )Nc                     s   t  j d j dd S )Nr   z requires soxr   r   r   r   r   r"      s    z0requires_sox.<locals>.decorator.<locals>.wrappedr   r#   r   r   r   r      s    )r?   r7   r   r   r   requires_sox   s    
r@   )N)importlib.utilr   r.   	functoolsr   typingr   r3   strboolr   r'   r1   r6   r8   r;   r<   r=   r>   r?   r@   r   r   r   r   <module>   s   
