o
    2h<                     @   s>   d Z ddlZddlZddlmZ ddlmZ G dd dZdS )zDMiddleware for injecting client-side feature flags into the Context.    N)context)errorsc                   @   s0   e Zd ZdZdd Zdd Zdd Zd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                 C   s
   || _ dS )zxInitializes this middleware.

        Args:
          application: The WSGI application to wrap (see PEP 3333).
        N)_application)selfapplication r   c/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorboard/backend/client_feature_flags.py__init__&   s   
z%ClientFeatureFlagsMiddleware.__init__c                 C   sx   |  |d}| |d}|s|s| ||S | D ]\}}|||< qt|j|d}t|| | ||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ctxr   r   r	   __call__.   s   

z%ClientFeatureFlagsMiddleware.__call__c                 C   sJ   |si S zt |}W n t jy   tdw t|ts#t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   r   r   r	   r   D   s   
z@ClientFeatureFlagsMiddleware._parse_potential_header_param_flagsc                 C   s   |si S zt j|}W n ty   i  Y S w |dg }|s"i S z	t|d }W n tjy8   t	dw t
|tsCt	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   r   r   r	   r   V   s0   
z?ClientFeatureFlagsMiddleware._parse_potential_query_param_flagsN)__name__
__module____qualname____doc__r
   r   r   r   r   r   r   r	   r      s    r   )r/   r   urllib.parser%   tensorboardr   r   r   r   r   r   r	   <module>   s   