o
    e$                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z
 ddlmZ dZd	Zd
Zd
ZdZdZdZdZdZdd Zdd Zdd Zdd Zdd Zdd ZG dd deZdS )z=Initial server communication to determine session parameters.    )messageN)logging)version)metadata)server_info_pb2
   i  i  i   i   i  < i   i @  c                 C   s$   t  }tj|_| |jjdd< |S )zGenerates a ServerInfoRequest

    Args:
      upload_plugins: List of plugin names requested by the user and to be
        verified by the server.

    Returns:
      A `server_info_pb2.ServerInfoRequest` message.
    N)r   ZServerInfoRequestr   VERSIONZplugin_specificationupload_plugins)r	   request r   PD:\Projects\ConvertPro\env\Lib\site-packages\tensorboard/uploader/server_info.py_server_info_requestJ   s   
r   c              
   C   s   d|  }t |}| }td| ztj||tddtj id}W n tj	y5 } zt
d| d}~ww |jsEt
d|j|j|jf ztj|jW S  tjyc } z	t
d	||jf d}~ww )
a  Fetches server info from a remote server.

    Args:
      origin: The server with which to communicate. Should be a string
        like "https://tensorboard.dev", including protocol, host, and (if
        needed) port.
      upload_plugins: List of plugins names requested by the user and to be
        verified by the server.

    Returns:
      A `server_info_pb2.ServerInfoResponse` message.

    Raises:
      CommunicationError: Upon failure to connect to or successfully
        communicate with the remote server.
    z%s/api/uploaderzRequested server info: <%r>z
User-Agentztensorboard/%s)datatimeoutheadersz Failed to connect to backend: %sNz&Non-OK status from backend (%d %s): %rz&Corrupt response from backend (%s): %r)r   ZSerializeToStringr   inforequestspost_REQUEST_TIMEOUT_SECONDSr   r   RequestExceptionCommunicationErrorokstatus_codereasoncontentr   ServerInfoResponseZ
FromStringr   DecodeError)originr	   endpointZserver_info_requestZ	post_bodyresponseer   r   r   fetch_server_infoZ   s:   
r!   c                 C   sd   t  }t j|j_||j_|j}d}|| v rd| }|| v sd| |f |_||_	||j
jdd< |S )a  Manually creates server info given a frontend and backend.

    Args:
      frontend_origin: The origin of the TensorBoard.dev frontend, like
        "https://tensorboard.dev" or "http://localhost:8000".
      api_endpoint: As to `server_info_pb2.ApiServer.endpoint`.
      upload_plugins: List of plugin names requested by the user and to be
        verified by the server.

    Returns:
      A `server_info_pb2.ServerInfoResponse` message.
    z{{EID}}z{%s}z%s/experiment/%s/N)r   r   Z
VERDICT_OKcompatibilityZverdictZ
api_serverr   
url_formattemplateid_placeholderplugin_controlallowed_plugins)Zfrontend_originZapi_endpointr	   resultr#   placeholderr   r   r   create_server_info   s   
r*   c                 C   s   | j }|j|j|S )a  Formats a URL that will resolve to the provided experiment.

    Args:
      server_info: A `server_info_pb2.ServerInfoResponse` message.
      experiment_id: A string; the ID of the experiment to link to.

    Returns:
      A URL resolving to the given experiment, as a string.
    )r#   r$   replacer%   )server_infoZexperiment_idr#   r   r   r   experiment_url   s   
r-   c                 C   s"   |  drt| jjS ttjfS )a-  Determines which plugins may upload data.

    This pulls from the `plugin_control` on the `server_info` when that
    submessage is set, else falls back to a default.

    Args:
      server_info: A `server_info_pb2.ServerInfoResponse` message.

    Returns:
      A `frozenset` of plugin names.
    r&   )HasField	frozensetr&   r'   scalars_metadataZPLUGIN_NAME)r,   r   r   r   r'      s   
r'   c                 C   s~   |  dr	| j}nt }|jst|_|jst|_|jst	|_|j
s%t|_
|js+t|_|js1t|_|js7t|_|js=t|_|S )zReturns UploadLimits, from server_info if possible, otherwise from defaults.

    Args:
      server_info: A `server_info_pb2.ServerInfoResponse` message.

    Returns:
      An instance of UploadLimits.
    upload_limits)r.   r1   r   ZUploadLimitsZmax_scalar_request_size _DEFAULT_MAX_SCALAR_REQUEST_SIZEZmax_tensor_request_size _DEFAULT_MAX_TENSOR_REQUEST_SIZEZmax_blob_request_size_DEFAULT_MAX_BLOB_REQUEST_SIZEZmin_scalar_request_interval$_DEFAULT_MIN_SCALAR_REQUEST_INTERVALZmin_tensor_request_interval$_DEFAULT_MIN_TENSOR_REQUEST_INTERVALZmin_blob_request_interval"_DEFAULT_MIN_BLOB_REQUEST_INTERVALZmax_blob_size_DEFAULT_MAX_BLOB_SIZEZmax_tensor_point_size_DEFAULT_MAX_TENSOR_POINT_SIZE)r,   r1   r   r   r   r1      s.   
	r1   c                   @   s   e Zd ZdZdS )r   z3Raised upon failure to communicate with the server.N)__name__
__module____qualname____doc__r   r   r   r   r      s    r   )r=   Zgoogle.protobufr   r   Zabslr   Ztensorboardr   Ztensorboard.plugins.scalarr   r0   Ztensorboard.uploader.protor   r   r5   r6   r7   r2   r3   r4   r8   r9   r   r!   r*   r-   r'   r1   RuntimeErrorr   r   r   r   r   <module>   s.   +(