
    $g                          d Z ddlmZ ddlmZmZ ddZd Zd Zd	 Z	dd
Z
 G d de      Z G d de      Z G d de      ZddZd Zy)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           	      F   || }nt        | |       t        |t        t        f      r2t	        | |      D cg c]  \  }}t        ||      D ]  }|  c}}}S t        |t              r)|D cg c]  }t        | |   ||         D ]  }|  c}}S | gS c c}}}w c c}}w )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
    )validate_grouping
isinstancetuplelistzipflatten_groupingdict)groupingschemagroup_el	schema_elgks         K/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/dash/_grouping.pyr   r      s     ~(F+&5$-( (+8V'<
 
#)%h	:
  

 	
 &$!Ta+;HQKPQ+STaTTT:
 Us   B/ Bc                    t        | t        t        f      r"t        | D cg c]  }t	        |       c}      S t        | t
              r0t        | j                         D cg c]  }t	        |       c}      S yc c}w c c}w )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
    r   )r	   r
   r   sumgrouping_lenr   values)r   r   s     r   r   r   2   sh     (UDM*8DxL*DEE(D!8??;LMxL*MNN E Ns   A: A?c           
         fdt        |t              st        dt        |             t	        t        |             }t	        |      |k7  r)t        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           
      0   t        | t        t        f      rt        fdt        |       D              S t        | t              r9t        | j                               D ci c]  \  }\  }}| |       c}}}S j                  d      S c c}}}w )Nc              3   6   K   | ]  \  }} |        y wN ).0iel_perform_make_grouping_likenext_valuess      r   	<genexpr>zNmake_grouping_by_index.<locals>._perform_make_grouping_like.<locals>.<genexpr>R   s$      Ar ,B<s   r   )r	   r
   r   	enumerater   itemspop)valuer#   r    r   vr"   s    `   r   r"   z;make_grouping_by_index.<locals>._perform_make_grouping_likeP   s    eeT]+ &u-  
 eT" "+5;;=!9 Av1 .q+>> 
 q!!s   $Bz@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"   s      @r   make_grouping_by_indexr0   D   s    " k4(&&*;&7%8:
 	

 *623O
;?*66G H%%(%5$6 7%%)&\N 3&-)
 	
 'vtK/@AA    c                    t        |t        t        f      r|D cg c]  }t        | |       c}S t        |t              r7t        |j                         D ci c]  \  }}|t        | |       c}}      S  | |      S c c}w c c}}w )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.
    )r	   r
   r   map_groupingr   r   r&   )fnr   r   r   s       r   r3   r3   q   sv     (UDM*-56R#66(D!AQRAab!!44RSSh< 7 Ss   B B
c                 &    t        f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                 (    j                  |       S r   )get)sdefaultsources    r   <lambda>z&make_grouping_by_key.<locals>.<lambda>   s    &**Q"8 r1   )r3   )r   r:   r9   s    ``r   make_grouping_by_keyr<      s     8&AAr1   c                   .     e Zd Z fdZed        Z xZS )SchemaTypeValidationErrorc                 |    t         |   d| dt        |       d| dt        |       dt        |       d       y )N
                Schema: 
                Path: z 
                Expected type: z(
                Received value of type :
                    
                msg)super__init__r-   r+   )selfr(   full_schemapathexpected_type	__class__s        r   rG   z"SchemaTypeValidationError.__init__   s]    $ &Dzl #  - /((,U} 5%[M " 	 	
r1   c                 8    t        ||      st        ||||      y r   )r	   r>   )clsr(   rI   rJ   rK   s        r   checkzSchemaTypeValidationError.check   s"    %/+E;mTT 0r1   __name__
__module____qualname__rG   classmethodrO   __classcell__rL   s   @r   r>   r>          	
 U Ur1   r>   c                   .     e Zd Z fdZed        Z xZS )SchemaLengthValidationErrorc                 |    t         |   d| dt        |       d| dt        |       dt        |       d       y )Nr@   rA   z"
                Expected length: z*
                Received value of length rB   rC   rD   )rF   rG   r-   r,   )rH   r(   rI   rJ   expected_lenrL   s        r   rG   z$SchemaLengthValidationError.__init__   s]    $ &Dzl #"". 0**-e* 6%[M " 	 	
r1   c                 <    t        |      |k7  rt        ||||      y r   )r,   rY   )rN   r(   rI   rJ   r[   s        r   rO   z!SchemaLengthValidationError.check   s$    u:%-e[$UU &r1   rP   rV   s   @r   rY   rY      s    	
 V Vr1   rY   c                   .     e Zd Z fdZed        Z xZS )SchemaKeysValidationErrorc                     t         |   d| dt        |       d| dt        |j	                                dt        |       d       y )Nr@   rA   z 
                Expected keys: z*
                Received value with keys rB   rC   rD   )rF   rG   r-   setkeys)rH   r(   rI   rJ   expected_keysrL   s        r   rG   z"SchemaKeysValidationError.__init__   se    $ &Dzl #  - /**-ejjl*;)< =%[M " 	 	
r1   c                 j    t        |j                               t        |      k7  rt        ||||      y r   )r`   ra   r^   )rN   r(   rI   rJ   rb   s        r   rO   zSchemaKeysValidationError.check   s0    uzz|M 22+E;mTT 3r1   rP   rV   s   @r   r^   r^      rW   r1   r^   c                    ||}t        |t        t        f      rwt        j	                  | ||t        t        f       t
        j	                  | ||t        |             t        t        | |            D ]  \  }\  }}t        |||||fz           yt        |t              r^t        j	                  | ||t               t        j	                  | ||t        |             |D ]  }t        | |   ||   |||fz           yy)zx
    Validate that the provided grouping conforms to the provided schema.
    If not, raise a SchemaValidationError
    N)rI   rJ   )r	   r
   r   r>   rO   rY   r,   r%   r   r   r   r^   r`   )r   r   rI   rJ   r    r   r8   r   s           r   r   r      s    
 &5$-(!''+teT]S#))(Ks6{S"3x#89 	OIAv1a$!+N	O	FD	!!''+tTJ!''+tS[Q 	AVAYKdaTk	
 	r1   c                     t        | t              rft        | d         }| d| d    }| j                  d      |||v t        | d   t              rt	        | d         n| d   d}| j                  |       y y )Nid.propertyr(   )r(   str_id	triggeredrf   )r	   r   r   r7   r   update)r   rj   ri   prop_id
new_valuess        r   update_args_grouprn      s    !Tag&HAa
m_- UU7^ I-,6qw,E-$(1T7	

 	
 r1   r   )Nr   )__doc__dash.exceptionsr   _utilsr   r   r   r   r0   r3   r<   r>   rY   r^   r   rn   r   r1   r   <module>rr      se    7 /<$*BZ&BU : U$V"< V$U : U$4r1   