o
    d                      @   s   d Z ddlmZ ddlmZmZ dddZdd	 Zd
d Zdd Z	dddZ
G dd deZG dd deZG dd deZdddZdd ZdS )a  
This module contains a collection of utility function for dealing with property
groupings.

Terminology:

For the purpose of grouping and ungrouping, tuples/lists and dictionaries are considered
"composite values" and all other values are considered "scalar values".

A "grouping value" is either composite or scalar.

A "schema" is a grouping value that can be used to encode an expected grouping
structure

    )InvalidCallbackReturnValue   )AttributeDictstringify_idNc                    s^   du r nt   tttfrdd t D S ttr, fddD S  gS )a  
    Convert a grouping value to a list of scalar values

    :param grouping: grouping value to flatten
    :param schema: If provided, a grouping value representing the expected structure of
        the input grouping value. If not provided, the grouping value is its own schema.
        A schema is required in order to be able treat tuples and dicts in the input
        grouping as scalar values.

    :return: list of the scalar values in the input grouping
    Nc                 S   s$   g | ]\}}t ||D ]}|qqS  flatten_grouping).0group_el	schema_elgr   r   S/var/www/html/visualizacion-main/env/lib/python3.10/site-packages/dash/_grouping.py
<listcomp>&   s    z$flatten_grouping.<locals>.<listcomp>c                    s(   g | ]}t  | | D ]}|qqS r   r   r	   kr   groupingschemar   r   r   -   s   ( )validate_grouping
isinstancetuplelistzipdictr   r   r   r   r      s   

r   c                 C   sD   t | ttfrtdd | D S t | tr tdd |  D S dS )aU  
    Get the length of a grouping. The length equal to the number of scalar values
    contained in the grouping, which is equivalent to the length of the list that would
    result from calling flatten_grouping on the grouping value.

    :param grouping: The grouping value to calculate the length of
    :return: non-negative integer
    c                 S      g | ]}t |qS r   grouping_lenr	   r
   r   r   r   r   <       z grouping_len.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r   ?   r   r   )r   r   r   sumr   values)r   r   r   r   r   2   s
   	
r   c              	      sv    fdd t |tstdt| tt| }t||kr4td| dt| dt|  d|  | t|S )a  
    Make a grouping like the provided grouping schema, with scalar values drawn from a
    flat list by index.

    Note: Scalar values in schema are not used

    :param schema: Grouping value encoding the structure of the grouping to return
    :param flat_values: List of values with length matching the grouping_len of schema.
        Elements of flat_values will become the scalar values in the resulting grouping
    c                    sZ   t | ttfrt fddt| D S t | tr( fddt|  D S  dS )Nc                 3   s    | ]
\}} |V  qd S Nr   )r	   iel_perform_make_grouping_likenext_valuesr   r   	<genexpr>R   s
    
zNmake_grouping_by_index.<locals>._perform_make_grouping_like.<locals>.<genexpr>c                    s    i | ]\}\}}| |qS r   r   )r	   r"   r   vr$   r   r   
<dictcomp>X   s    

zOmake_grouping_by_index.<locals>._perform_make_grouping_like.<locals>.<dictcomp>r   )r   r   r   	enumerater   itemspop)valuer&   r%   )r&   r   r%   P   s   


z;make_grouping_by_index.<locals>._perform_make_grouping_likez@The flat_values argument must be a list. Received value of type z(The specified grouping pattern requires z elements but received z
    Grouping pattern: z
    Values: )r   r   
ValueErrortypelenr   repr)r   flat_valuesexpected_lengthr   r.   r   make_grouping_by_indexD   s&   
r5   c                    sL   t |ttfr fdd|D S t |tr"t fdd| D S  |S )a  
    Map a function over all of the scalar values of a grouping, maintaining the
    grouping structure

    :param fn: Single-argument function that accepts and returns scalar grouping values
    :param grouping: The grouping to map the function over
    :return: A new grouping with the same structure as input grouping with scalar
        values updated by the input function.
    c                    s   g | ]}t  |qS r   map_grouping)r	   r   fnr   r   r   |   s    z map_grouping.<locals>.<listcomp>c                    s   i | ]
\}}|t  |qS r   r6   r   r8   r   r   r)      s    z map_grouping.<locals>.<dictcomp>)r   r   r   r   r   r+   )r9   r   r   r8   r   r7   q   s
   

r7   c                    s   t  fdd| S )a  
    Create a grouping from a schema by using the schema's scalar values to look up
    items in the provided source object.

    :param schema: A grouping of potential keys in source
    :param source: Dict-like object to use to look up scalar grouping value using
        scalar grouping values as keys
    :param default: Default scalar value to use if grouping scalar key is not present
        in source
    :return: grouping
    c                    s    |  S r!   )get)sdefaultsourcer   r   <lambda>   s    z&make_grouping_by_key.<locals>.<lambda>r6   )r   r>   r=   r   r<   r   make_grouping_by_key   s   r@   c                       (   e Zd Z fddZedd Z  ZS )SchemaTypeValidationErrorc                    >   t  jd| dt| d| dt| dt| dd d S )N
                Schema: 
                Path: z 
                Expected type: z(
                Received value of type :
                    
                msg)super__init__r2   r0   )selfr-   full_schemapathexpected_type	__class__r   r   rK         
z"SchemaTypeValidationError.__init__c                 C   s   t ||st||||d S r!   )r   rB   )clsr-   rM   rN   rO   r   r   r   check   s   
zSchemaTypeValidationError.check__name__
__module____qualname__rK   classmethodrT   __classcell__r   r   rP   r   rB          rB   c                       rA   )SchemaLengthValidationErrorc                    rC   )NrD   rE   z"
                Expected length: z*
                Received value of length rF   rG   rH   )rJ   rK   r2   r1   )rL   r-   rM   rN   expected_lenrP   r   r   rK      rR   z$SchemaLengthValidationError.__init__c                 C   s   t ||krt||||d S r!   )r1   r\   )rS   r-   rM   rN   r]   r   r   r   rT      s   z!SchemaLengthValidationError.checkrU   r   r   rP   r   r\      r[   r\   c                       rA   )SchemaKeysValidationErrorc                    sB   t  jd| dt| d| dt|  dt| dd d S )NrD   rE   z 
                Expected keys: z*
                Received value with keys rF   rG   rH   )rJ   rK   r2   setkeys)rL   r-   rM   rN   expected_keysrP   r   r   rK      s   

z"SchemaKeysValidationError.__init__c                 C   s&   t | t |krt||||d S r!   )r_   r`   r^   )rS   r-   rM   rN   ra   r   r   r   rT      s   zSchemaKeysValidationError.checkrU   r   r   rP   r   r^      r[   r^   r   c                 C   s   |du r|}t |ttfr<t| ||ttf t| ||t| tt| |D ]\}\}}t	|||||f d q(dS t |t
rit| ||t
 t| ||t| |D ]}t	| | || |||f d qUdS 	 dS )zx
    Validate that the provided grouping conforms to the provided schema.
    If not, raise a SchemaValidationError
    N)rM   rN   )r   r   r   rB   rT   r\   r1   r*   r   r   r   r^   r_   )r   r   rM   rN   r"   r   r;   r   r   r   r   r      s"   
r   c                 C   sp   t | tr6t| d }| d| d  }| d|||v t | d tr)t| d n| d d}| | d S d S )Nid.propertyr-   )r-   str_id	triggeredrb   )r   r   r   r:   r   update)r   rf   re   prop_id
new_valuesr   r   r   update_args_group   s   
 rj   r!   )Nr   )__doc__dash.exceptionsr   _utilsr   r   r   r   r5   r7   r@   rB   r\   r^   r   rj   r   r   r   r   <module>   s    
-

