o
    win2                     @   s   d dl Z d dlZd dlZd dlmZ eeZeej	 e
e  dZG dd deZedd ZG dd	 d	eZd
d ZG dd dZdddddZddddddZddddZddddZdS )    N)contextmanagerzInstall GPU packages via `pip install --extra-index-url https://pypi.nvidia.com nemo-curator[cuda12x]`
or use `pip install --extra-index-url https://pypi.nvidia.com ".[cuda12x]"` if installing from sourcec                   @   s   e Zd ZdZdS )UnavailableErrorzDError thrown if a symbol is unavailable due to an issue importing itN)__name__
__module____qualname____doc__ r   r   T/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/utils/import_utils.pyr   #   s    r   c                  O   s8   t |dkrt | dkrt| d r| d S dd }|S )null_decoratorr      c                 S   s   | S Nr   )funcr   r   r	   inner.      znull_decorator.<locals>.inner)lencallable)argskwargsr   r   r   r	   r
   '   s   $r
   c                       s  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	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dZd[ Z0d\d] Z1d^d_ Z2  Z3S )`UnavailableMetaau  A metaclass for generating placeholder objects for unavailable symbols

    This metaclass allows errors to be deferred from import time to the time
    that a symbol is actually used in order to streamline the usage of optional
    dependencies. This is particularly useful for attempted imports of GPU-only
    modules which will only be invoked if GPU-only functionality is
    specifically used.

    If an attempt to import a symbol fails, this metaclass is used to generate
    a class which stands in for that symbol. Any attempt to call the symbol
    (instantiate the class) or access its attributes will throw an
    UnavailableError exception. Furthermore, this class can be used in
    e.g. isinstance checks, since it will (correctly) fail to match any
    instance it is compared against.

    In addition to calls and attribute access, a number of dunder methods are
    implemented so that other common usages of imported symbols (e.g.
    arithmetic) throw an UnavailableError, but this is not guaranteed for
    all possible uses. In such cases, other exception types (typically
    TypeErrors) will be thrown instead.
    c                    s>   | dd d u r| d|d< d| }tt| | |||S )N_msg could not be importedMISSING)getsuperr   __new__)metanamebasesdct	__class__r   r	   r   K   s   
zUnavailableMeta.__new__c                 O   
   t | jr   r   r   clsr   r   r   r   r	   __call__Q      
zUnavailableMeta.__call__c                 C   r!   r   r"   r$   r   r   r   r	   __getattr__T   r&   zUnavailableMeta.__getattr__c                 C   r!   r   r"   r$   otherr   r   r	   __eq__W   r&   zUnavailableMeta.__eq__c                 C   r!   r   r"   r)   r   r   r	   __lt__Z   r&   zUnavailableMeta.__lt__c                 C   r!   r   r"   r)   r   r   r	   __gt__]   r&   zUnavailableMeta.__gt__c                 C   r!   r   r"   r)   r   r   r	   __le__`   r&   zUnavailableMeta.__le__c                 C   r!   r   r"   r)   r   r   r	   __ge__c   r&   zUnavailableMeta.__ge__c                 C   r!   r   r"   r)   r   r   r	   __ne__f   r&   zUnavailableMeta.__ne__c                 C   r!   r   r"   r$   r   r   r	   __abs__i   r&   zUnavailableMeta.__abs__c                 C   r!   r   r"   r)   r   r   r	   __add__l   r&   zUnavailableMeta.__add__c                 C   r!   r   r"   r)   r   r   r	   __radd__o   r&   zUnavailableMeta.__radd__c                 C   r!   r   r"   r)   r   r   r	   __iadd__r   r&   zUnavailableMeta.__iadd__c                 C   r!   r   r"   r)   r   r   r	   __floordiv__u   r&   zUnavailableMeta.__floordiv__c                 C   r!   r   r"   r)   r   r   r	   __rfloordiv__x   r&   zUnavailableMeta.__rfloordiv__c                 C   r!   r   r"   r)   r   r   r	   __ifloordiv__{   r&   zUnavailableMeta.__ifloordiv__c                 C   r!   r   r"   r)   r   r   r	   
__lshift__~   r&   zUnavailableMeta.__lshift__c                 C   r!   r   r"   r)   r   r   r	   __rlshift__   r&   zUnavailableMeta.__rlshift__c                 C   r!   r   r"   r)   r   r   r	   __mul__   r&   zUnavailableMeta.__mul__c                 C   r!   r   r"   r)   r   r   r	   __rmul__   r&   zUnavailableMeta.__rmul__c                 C   r!   r   r"   r)   r   r   r	   __imul__   r&   zUnavailableMeta.__imul__c                 C   r!   r   r"   r)   r   r   r	   __ilshift__   r&   zUnavailableMeta.__ilshift__c                 C   r!   r   r"   r)   r   r   r	   __pow__   r&   zUnavailableMeta.__pow__c                 C   r!   r   r"   r)   r   r   r	   __rpow__   r&   zUnavailableMeta.__rpow__c                 C   r!   r   r"   r)   r   r   r	   __ipow__   r&   zUnavailableMeta.__ipow__c                 C   r!   r   r"   r)   r   r   r	   
__rshift__   r&   zUnavailableMeta.__rshift__c                 C   r!   r   r"   r)   r   r   r	   __rrshift__   r&   zUnavailableMeta.__rrshift__c                 C   r!   r   r"   r)   r   r   r	   __irshift__   r&   zUnavailableMeta.__irshift__c                 C   r!   r   r"   r)   r   r   r	   __sub__   r&   zUnavailableMeta.__sub__c                 C   r!   r   r"   r)   r   r   r	   __rsub__   r&   zUnavailableMeta.__rsub__c                 C   r!   r   r"   r)   r   r   r	   __isub__   r&   zUnavailableMeta.__isub__c                 C   r!   r   r"   r)   r   r   r	   __truediv__   r&   zUnavailableMeta.__truediv__c                 C   r!   r   r"   r)   r   r   r	   __rtruediv__   r&   zUnavailableMeta.__rtruediv__c                 C   r!   r   r"   r)   r   r   r	   __itruediv__   r&   zUnavailableMeta.__itruediv__c                 C   r!   r   r"   r)   r   r   r	   
__divmod__   r&   zUnavailableMeta.__divmod__c                 C   r!   r   r"   r)   r   r   r	   __rdivmod__   r&   zUnavailableMeta.__rdivmod__c                 C   r!   r   r"   r1   r   r   r	   __neg__   r&   zUnavailableMeta.__neg__c                 C   r!   r   r"   r1   r   r   r	   
__invert__   r&   zUnavailableMeta.__invert__c                 C   r!   r   r"   r1   r   r   r	   __hash__   r&   zUnavailableMeta.__hash__c                 C   r!   r   r"   r1   r   r   r	   	__index__   r&   zUnavailableMeta.__index__c                 C   r!   r   r"   r1   r   r   r	   __iter__   r&   zUnavailableMeta.__iter__c                 C   r!   r   r"   r'   r   r   r	   __delitem__   r&   zUnavailableMeta.__delitem__c                 C   r!   r   r"   )r$   r   valuer   r   r	   __setitem__   r&   zUnavailableMeta.__setitem__c                 O   r!   r   r"   r#   r   r   r	   	__enter__   r&   zUnavailableMeta.__enter__c                 O   r!   r   r"   r#   r   r   r	   __get__   r&   zUnavailableMeta.__get__c                 O   r!   r   r"   r#   r   r   r	   
__delete__   r&   zUnavailableMeta.__delete__c                 C   r!   r   r"   r1   r   r   r	   __len__   r&   zUnavailableMeta.__len__)4r   r   r   r   r   r%   r(   r+   r,   r-   r.   r/   r0   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rT   rU   rV   rW   rX   __classcell__r   r   r   r	   r   4   sb    r   c                 C   s   t | tu S )z9Helper to check if given symbol is actually a placeholder)typer   )objr   r   r	   is_unavailable   s   r\   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	UnavailableNullContextzA placeholder class for unavailable context managers

    This context manager will return a value which will throw an
    UnavailableError if used in any way, but the context manager itself can be
    safely invoked.
    c                 O      d S r   r   selfr   r   r   r   r	   __init__   r   zUnavailableNullContext.__init__c                 C   s   t ddddiS )NMissingContextValuer   r   z:Attempted to make use of placeholder context return value.)r   )r`   r   r   r	   rU      s
   z UnavailableNullContext.__enter__c                 O   r^   r   r   r_   r   r   r	   __exit__   r   zUnavailableNullContext.__exit__N)r   r   r   r   ra   rU   rc   r   r   r   r	   r]      s
    r]   msgaltc             	   C   s   zt | dfW S  ty    t }td|  d|  Y n ty+   t } w |du r5|  d}|du rHt| 	dd dd	|id
fS |d
fS )aJ  A function used to import modules that may not be available

    This function will attempt to import a module with the given name, but it
    will not throw an ImportError if the module is not found. Instead, it will
    return a placeholder object which will raise an exception only if used.

    Parameters
    ----------
    module: str
        The name of the module to import.
    msg: str or None
        An optional error message to be displayed if this module is used
        after a failed import.
    alt: object
        An optional module to be used in place of the given module if it
        fails to import

    Returns
    -------
    Tuple(object, bool)
        The imported module, the given alternate, or a class derived from
        UnavailableMeta, and a boolean indicating whether the intended import was successful.
    T
Import of  failed with: Nr   .r   r   F)
	importlibimport_moduleImportError	traceback
format_excloggerdebug	Exceptionr   rsplit)modulere   rf   exception_textr   r   r	   safe_import   s   
rv   re   rf   fallback_modulec                C   s   zt | }t||dfW S  ty%   t }td|  d|  Y n5 tyN   |dur:t	||||dd Y S t }t
d| d|  d|  Y n tyY   t } w |du rf|  d| d}|du rtt|d	d
|idfS |dfS )a  A function used to import symbols from modules that may not be available

    This function will attempt to import a symbol with the given name from
    the given module, but it will not throw an ImportError if the symbol is not
    found. Instead, it will return a placeholder object which will raise an
    exception only if used.

    Parameters
    ----------
    module: str
        The name of the module in which the symbol is defined.
    symbol: str
        The name of the symbol to import.
    msg: str or None
        An optional error message to be displayed if this symbol is used
        after a failed import.
    alt: object
        An optional object to be used in place of the given symbol if it fails
        to import
    fallback_module: str
        Alternative name of the model in which the symbol is defined. The function will first to
        import using the `module` value and if that fails will also try the `fallback_module`.

    Returns
    -------
    Tuple(object, bool)
        The imported symbol, the given alternate, or a class derived from
        UnavailableMeta, and a boolean indicating whether the intended import was successful.
    Trg   rh   Nrw   z from ri   r   r   r   F)rk   rl   getattrrm   rn   ro   rp   rq   AttributeErrorsafe_import_frominforr   r   )rt   symbolre   rf   rx   imported_moduleru   r   r   r	   r{     s(   
 r{   )rf   c                C   s   t | |  dt |dS )a)  A function used to import modules required only in GPU installs

    This function will attempt to import a module with the given name.
    This function will attempt to import a symbol with the given name from
    the given module, but it will not throw an ImportError if the symbol is not
    found. Instead, it will return a placeholder object which will raise an
    exception only if used with instructions on installing a GPU build.

    Parameters
    ----------
    module: str
        The name of the module to import.
    alt: object
        An optional module to be used in place of the given module if it
        fails to import in a non-GPU-enabled install

    Returns
    -------
    object
        The imported module, the given alternate, or a class derived from
        UnavailableMeta.
    zB is not enabled in non GPU-enabled installations or environemnts. rd   )rv   GPU_INSTALL_STRING)rt   rf   r   r   r	   gpu_only_importT  s
   r   c                C   s    t | ||  d| dt |dS )ac  A function used to import symbols required only in GPU installs

    This function will attempt to import a module with the given name.
    This function will attempt to import a symbol with the given name from
    the given module, but it will not throw an ImportError if the symbol is not
    found. Instead, it will return a placeholder object which will raise an
    exception only if used with instructions on installing a GPU build.

    Parameters
    ----------
    module: str
        The name of the module to import.
    symbol: str
        The name of the symbol to import.
    alt: object
        An optional object to be used in place of the given symbol if it fails
        to import in a non-GPU-enabled install

    Returns
    -------
    object
        The imported symbol, the given alternate, or a class derived from
        UnavailableMeta.
    ri   zB is not enabled in non GPU-enabled installations or environments. rd   )r{   r   )rt   r}   rf   r   r   r	   gpu_only_import_froms  s   r   )rk   loggingrn   
contextlibr   	getLoggerr   rp   setLevelINFO
addHandlerStreamHandlerr   rr   r   r
   rZ   r   r\   r]   rv   r{   r   r   r   r   r   r	   <module>   s(   

 )(5