
    &Vf<                     L    d Z ddlZddlZddlmZ ddlmZ  G d d          ZdS )zDMiddleware for injecting client-side feature flags into the Context.    N)context)errorsc                   *    e Zd ZdZd Zd Zd Zd ZdS )ClientFeatureFlagsMiddlewarea  Middleware for injecting client-side feature flags into the Context.

    The client webapp is expected to include a json-serialized version of its
    FeatureFlags in the `X-TensorBoard-Feature-Flags` header or the
    `tensorBoardFeatureFlags` query parameter. This middleware extracts the
    header or query parameter value and converts it into the client_feature_flags
    property for the DataProvider's Context object, where client_feature_flags
    is a Dict of string keys and arbitrary value types.

    In the event that both the header and query parameter are specified, the
    values from the header will take precedence.
    c                     || _         dS )zxInitializes this middleware.

        Args:
          application: The WSGI application to wrap (see PEP 3333).
        N)_application)selfapplications     e/var/www/html/software/conda/lib/python3.11/site-packages/tensorboard/backend/client_feature_flags.py__init__z%ClientFeatureFlagsMiddleware.__init__&   s     (    c                    |                      |                    d                    }|                     |                    d                    }|s|s|                     ||          S |                                D ]
\  }}|||<   t          j        |                              |          }t          j        ||           |                     ||          S )N HTTP_X_TENSORBOARD_FEATURE_FLAGSQUERY_STRING)client_feature_flags)	#_parse_potential_header_param_flagsget"_parse_potential_query_param_flagsr   itemsr   from_environreplaceset_in_environ)r	   environstart_responseheader_feature_flagsquery_string_feature_flagsflagvaluectxs           r   __call__z%ClientFeatureFlagsMiddleware.__call__.   s    #GGKK:;; 
  
 &*%L%LKK''&
 &
" $ 	>,F 	>$$Wn=== 05577 	5 	5KD%/4&t,,"7++33!; 4 
 
 	w,,,  .999r   c                     |si S 	 t          j        |          }n'# t           j        $ r t          j        d          w xY wt          |t                    st          j        d          |S )Nz3X-TensorBoard-Feature-Flags cannot be JSON decoded.z8X-TensorBoard-Feature-Flags cannot be decoded to a dict.)jsonloadsJSONDecodeErrorr   InvalidArgumentError
isinstancedict)r	   header_stringr   s      r   r   z@ClientFeatureFlagsMiddleware._parse_potential_header_param_flagsD   s     	I	#':m#<#<  # 	 	 	-E  	
 .55 	-J   $#s    $?c                    |si S 	 t           j                            |          }n# t          $ r i cY S w xY w|                    dg           }|si S 	 t          j        |d                   }n'# t
          j        $ r t          j	        d          w xY wt          |t                    st          j	        d          |S )NtensorBoardFeatureFlagsr   z/tensorBoardFeatureFlags cannot be JSON decoded.z4tensorBoardFeatureFlags cannot be decoded to a dict.)urllibparseparse_qs
ValueErrorr   r"   r#   r$   r   r%   r&   r'   )r	   query_stringquery_string_jsonpotential_feature_flagsr   s        r   r   z?ClientFeatureFlagsMiddleware._parse_potential_query_param_flagsV   s    	I	 & 5 5l C C 	 	 	III	 #4"7"7%r#
 #
 ' 	I	#':.Ea.H#I#I  # 	 	 	-A  	
 .55 	-F   $#s   & 55A. .$BN)__name__
__module____qualname____doc__r   r    r   r    r   r   r   r      sZ         ( ( (: : :,$ $ $$$ $ $ $ $r   r   )r5   r"   urllib.parser+   tensorboardr   r   r   r6   r   r   <module>r9      s    K J                 Y$ Y$ Y$ Y$ Y$ Y$ Y$ Y$ Y$ Y$r   