a
    
d<                     @   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)selfZapplication r   q/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/tensorboard/backend/client_feature_flags.py__init__&   s    z%ClientFeatureFlagsMiddleware.__init__c                 C   sx   |  |d}| |d}|s4|s4| ||S | D ]\}}|||< q<t|j|d}t|| | ||S )NZ HTTP_X_TENSORBOARD_FEATURE_FLAGSQUERY_STRING)client_feature_flags)	#_parse_potential_header_param_flagsget"_parse_potential_query_param_flagsr   itemsr   Zfrom_environreplaceZset_in_environ)r   environZstart_responseheader_feature_flagsZquery_string_feature_flagsflagvaluectxr   r   r   __call__.   s    

z%ClientFeatureFlagsMiddleware.__call__c                 C   sN   |si S zt |}W n t jy4   tdY n0 t|tsJt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   Z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 0 |dg }|sDi S zt|d }W n tjyt   t	dY n0 t
|tst	d|S )NZ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   Zquery_stringZquery_string_jsonZpotential_feature_flagsr   r   r   r   r   V   s,    


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   Ztensorboardr   r   r   r   r   r   r   <module>   s
   