o
    %ݫi	                     @   s&   d Z ddlZdd ZG dd dZdS )z=Chaining together callables, if some require relative lengths    Nc                 C   s   t | }d|j|j v S )a  Check if func takes ``lengths`` keyword argument.

    Arguments
    ---------
    func : callable
        The function, method, or other callable to search for the lengths arg.

    Returns
    -------
    True if func takes ``lengths`` keyword argument.
    lengths)inspectgetfullargspecargs
kwonlyargs)funcspec r	   P/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/utils/callchains.pylengths_arg_exists   s   
r   c                   @   s2   e Zd ZdZdd ZdddZdd Zd	d
 ZdS )LengthsCapableChaina2  Chain together callables. Can handle relative lengths.

    This is a more light-weight version of
    speechbrain.nnet.containers.LengthsCapableSequential

    Arguments
    ---------
    *funcs : list, optional
        Any number of functions or other callables, given in order of
        execution.
    c                 G   s$   g | _ g | _|D ]}| | qd S N)funcstakes_lengthsappend)selfr   r   r	   r	   r
   __init__#   s
   zLengthsCapableChain.__init__Nc                 C   sP   | j s|S t| j | jD ]\}}|r|||}n||}t|tr%|d }q|S )a  Run the chain of callables on the given input

        Arguments
        ---------
        x : Any
            The main input
        lengths : Any
            The lengths argument which will be conditionally passed to
            any functions in the chain that take a 'lengths' argument.
            In SpeechBrain the convention is to use relative lengths.

        Returns
        -------
        The input as processed by each function. If no functions were given,
        simply returns the input.

        Note
        ----
        By convention, if a callable in the chain returns multiple outputs
        (returns a tuple), only the first output is passed to the next
        callable in the chain.
        r   )r   zipr   
isinstancetuple)r   xr   r   give_lengthsr	   r	   r
   __call__)   s   
zLengthsCapableChain.__call__c                 C   s    | j | | jt| dS )zAdd a function to the chainN)r   r   r   r   )r   r   r	   r	   r
   r   K   s   zLengthsCapableChain.appendc                 C   s8   | j j}| jr| dddd | jD  S d| S )Nz:

c                 s   s    | ]}t |V  qd S r   )str).0fr	   r	   r
   	<genexpr>S   s    z.LengthsCapableChain.__str__.<locals>.<genexpr>zEmpty )	__class____name__r   join)r   clsnamer	   r	   r
   __str__P   s    
zLengthsCapableChain.__str__r   )r   
__module____qualname____doc__r   r   r   r"   r	   r	   r	   r
   r      s    
"r   )r%   r   r   r   r	   r	   r	   r
   <module>   s    