
    Mf                          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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                       nt                      t          t          t          f          rd t	                     D             S t          t
                    r f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                 >    g | ]\  }}t          ||          D ]}|S  flatten_grouping).0group_el	schema_elgs       M/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/dash/_grouping.py
<listcomp>z$flatten_grouping.<locals>.<listcomp>&   sM     
 
 
#)%h	::
 
  
 
 
 
    c                 R    g | ]#}t          |         |                   D ]}|$S r	   r
   )r   kr   groupingschemas      r   r   z$flatten_grouping.<locals>.<listcomp>-   s:    TTTa+;HQKPQ+S+STTaTTTTr   )validate_grouping
isinstancetuplelistzipdict)r   r   s   ``r   r   r      s     ~(F+++&5$-(( 

 
'*8V'<'<
 
 
 	
 &$ UTTTTT6TTTT:r   c                     t          | t          t          f          rt          d | D                       S t          | t                    r+t          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                 ,    g | ]}t          |          S r	   grouping_lenr   r   s     r   r   z grouping_len.<locals>.<listcomp><   s     DDDxL**DDDr   c                 ,    g | ]}t          |          S r	   r   r!   s     r   r   z grouping_len.<locals>.<listcomp>?   s     MMMxL**MMMr   r   )r   r   r   sumr   values)r   s    r   r    r    2   sw     (UDM** FDD8DDDEEE(D!! OMM8??;L;LMMMNNN1r   c           
      p   fdt          |t                    st          dt          |                     t	          t          |                     }t	          |          |k    r5t          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                 @   t          | t          t          f          r)t          fdt          |           D                       S t          | t                    r.fdt          |                                           D             S                     d          S )Nc              3   6   K   | ]\  }} |          V  d S Nr	   )r   iel_perform_make_grouping_likenext_valuess      r   	<genexpr>zNmake_grouping_by_index.<locals>._perform_make_grouping_like.<locals>.<genexpr>R   sH        Ar ,+B<<     r   c                 6    i | ]\  }\  }}| |          S r	   r	   )r   r)   r   vr+   r,   s       r   
<dictcomp>zOmake_grouping_by_index.<locals>._perform_make_grouping_like.<locals>.<dictcomp>X   sA       Av1 ..q+>>  r   r   )r   r   r   	enumerater   itemspop)valuer,   r+   s    `r   r+   z;make_grouping_by_index.<locals>._perform_make_grouping_likeP   s    eeT]++ 	     &u--     
 eT"" 	    !*5;;==!9!9   
 q!!!r   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_indexr;   D   s    " " " " " k4(( 
:&*;&7&7: :
 
 	

 *62233O
;?**) ) )%(%5%5) )%)&\\) ) ') )
 
 	
 '&vtK/@/@AAAr   c                      t          |t          t          f          r fd|D             S t          |t                    r-t	           f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                 0    g | ]}t          |          S r	   map_grouping)r   r   fns     r   r   z map_grouping.<locals>.<listcomp>|   s#    666R##666r   c                 8    i | ]\  }}|t          |          S r	   r>   )r   r   r   r@   s      r   r0   z map_grouping.<locals>.<dictcomp>   s)    RRRAab!!4!4RRRr   )r   r   r   r   r   r2   )r@   r   s   ` r   r?   r?   q   s     (UDM** 76666X6666(D!! TRRRRAQAQRRRSSS2h<<r   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                 0                         |           S r(   )get)sdefaultsources    r   <lambda>z&make_grouping_by_key.<locals>.<lambda>   s    &**Q"8"8 r   r>   )r   rG   rF   s    ``r   make_grouping_by_keyrI      s#     88888&AAAr   c                   4     e Zd Z fdZed             Z xZS )SchemaTypeValidationErrorc                     t                                          d| dt          |           d| dt          |           dt          |           d           d S )N
                Schema: 
                Path: z 
                Expected type: z(
                Received value of type :
                    
                msg)super__init__r8   r6   )selfr4   full_schemapathexpected_type	__class__s        r   rT   z"SchemaTypeValidationError.__init__   s    $ Dzz  !.  )-U	 
 %[[   	 	
 	
 	
 	
 	
r   c                 J    t          ||          st          ||||          d S r(   )r   rK   )clsr4   rV   rW   rX   s        r   checkzSchemaTypeValidationError.check   s5    %// 	U+E;mTTT	U 	Ur   __name__
__module____qualname__rT   classmethodr\   __classcell__rY   s   @r   rK   rK      Z        	
 	
 	
 	
 	
 U U [U U U U Ur   rK   c                   4     e Zd Z fdZed             Z xZS )SchemaLengthValidationErrorc                     t                                          d| dt          |           d| dt          |           dt          |           d           d S )NrM   rN   z"
                Expected length: z*
                Received value of length rO   rP   rQ   )rS   rT   r8   r7   )rU   r4   rV   rW   expected_lenrY   s        r   rT   z$SchemaLengthValidationError.__init__   s    $ Dzz  #/  +.e**	 
 %[[   	 	
 	
 	
 	
 	
r   c                 P    t          |          |k    rt          ||||          d S r(   )r7   rf   )r[   r4   rV   rW   rh   s        r   r\   z!SchemaLengthValidationError.check   s/    u::%%-e[$UUU &%r   r]   rc   s   @r   rf   rf      sZ        	
 	
 	
 	
 	
 V V [V V V V Vr   rf   c                   4     e Zd Z fdZed             Z xZS )SchemaKeysValidationErrorc                     t                                          d| dt          |           d| dt          |                                           dt          |           d           d S )NrM   rN   z 
                Expected keys: z*
                Received value with keys rO   rP   rQ   )rS   rT   r8   setkeys)rU   r4   rV   rW   expected_keysrY   s        r   rT   z"SchemaKeysValidationError.__init__   s    $ Dzz  !.  +.ejjll*;*;	 
 %[[   	 	
 	
 	
 	
 	
r   c                     t          |                                          t          |          k    rt          ||||          d S r(   )rm   rn   rk   )r[   r4   rV   rW   ro   s        r   r\   zSchemaKeysValidationError.check   sA    uzz||M 2 222+E;mTTT 32r   r]   rc   s   @r   rk   rk      rd   r   rk   r	   c                    ||}t          |t          t          f          rt                              | ||t          t          f           t
                              | ||t          |                     t          t          | |                    D ]\  }\  }}t          |||||fz               dS t          |t                    rvt                              | ||t                     t                              | ||t          |                     |D ]%}t          | |         ||         |||fz              &dS dS )zx
    Validate that the provided grouping conforms to the provided schema.
    If not, raise a SchemaValidationError
    N)rV   rW   )r   r   r   rK   r\   rf   r7   r1   r   r   r   rk   rm   )r   r   rV   rW   r)   r   rE   r   s           r   r   r      sY   
 &5$-(( !''+teT]SSS#))(Ks6{{SSS"3x#8#899 	O 	OIAv1a$!+NNNNN	O 	O	FD	!	! 	!''+tTJJJ!''+tS[[QQQ 	 	AVAYKdaTk    	 	
 	r   c                 F   t          | t                    rt          | d                   }| d| d          }|                     d          |||v t          | d         t                    rt	          | d                   n| d         d}|                     |           d S d S )Nid.propertyr4   )r4   str_id	triggeredrs   )r   r   r   rD   r   update)r   rw   rv   prop_id
new_valuess        r   update_args_groupr{      s    !T 
ag&&--a
m-- UU7^^ I-,6qw,E,ER-$(((1T7	
 

 	

 
r   r(   )Nr	   )__doc__dash.exceptionsr   _utilsr   r   r   r    r;   r?   rI   rK   rf   rk   r   r{   r	   r   r   <module>r      s^    7 6 6 6 6 6 / / / / / / / /   <  $*B *B *BZ  &B B B BU U U U U : U U U$V V V V V"< V V V$U U U U U : U U U$   4    r   