a
    d=icQ*                     @   s   d Z ddlZddlZddlZddlZddlZddlZddl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 e ZdZG dd	 d	ejZG d
d dejZdd Zdd ZG dd deZG dd deZG dd dZdd ZdS )z6Provides data ingestion logic backed by a gRPC server.    N)grpc_provider)ingester)data_provider_pb2)
tb_loggingZTENSORBOARD_DATA_SERVER_BINARYc                   @   s,   e Zd ZdZdd Zedd Zdd ZdS )	ExistingServerDataIngesterz*Connect to an already running gRPC server.c                C   s   t ||}t||| _dS )zInitializes an ingester with the given configuration.

        Args:
          address: String, as passed to `--grpc_data_provider`.
          channel_creds_type: `grpc_util.ChannelCredsType`, as passed to
            `--grpc_creds_type`.
        N)
_make_stubr   GrpcDataProvider_data_provider)selfaddresschannel_creds_typestub r   q/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/tensorboard/data/server_ingester.py__init__+   s    
z#ExistingServerDataIngester.__init__c                 C   s   | j S N)r	   r
   r   r   r   data_provider6   s    z(ExistingServerDataIngester.data_providerc                 C   s   d S r   r   r   r   r   r   start:   s    z ExistingServerDataIngester.startN__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r   r   (   s
   
r   c                   @   s4   e Zd ZdZdddddZedd Zdd	 ZdS )
SubprocessServerDataIngesterz(Start a new data server as a subprocess.N)samples_per_pluginextra_flagsc                C   s:   || _ d| _|| _|| _|| _|p$i | _t|p0g | _dS )aD  Initializes an ingester with the given configuration.

        Args:
          server_binary: `ServerBinary` to launch.
          logdir: String, as passed to `--logdir`.
          reload_interval: Number, as passed to `--reload_interval`.
          channel_creds_type: `grpc_util.ChannelCredsType`, as passed to
            `--grpc_creds_type`.
          samples_per_plugin: Dict[String, Int], as parsed from
            `--samples_per_plugin`.
          extra_flags: List of extra string flags to be passed to the
            data server without further interpretation.
        N)_server_binaryr	   _logdir_reload_interval_channel_creds_type_samples_per_pluginlist_extra_flags)r
   server_binaryZlogdirZreload_intervalr   r   r   r   r   r   r   A   s    
z%SubprocessServerDataIngester.__init__c                 C   s   | j d u rtd| j S )NzMust call `start` first)r	   RuntimeErrorr   r   r   r   r   `   s    
z*SubprocessServerDataIngester.data_providerc              
   C   s|  | j r
d S tjdd}tj|jd}tj|jd}| jdkrFd}ntt	| j}dd | j
 D }d	|}| jjd
tj| j d| d| dd|f dg}| jdr|d|f  ttjr|d ttjr|d || j td| tj|tjd}|j| _d }	td t dD ]}
|! d urpt"|pPd# }|shd|!  }t$|td|
 t"|}td| |pd%drt	|}	 qtd q2|	d u rt$d|j& d|	 }t'|| j(}td|j&| t)* }z|j+|d d! W nJ t,j-y^ } z.d"||f }t.d#| t$||W Y d }~n
d }~0 0 td$ t/0||| _ d S )%NZtensorboard_data_server_)prefixportZstartup_errorr   oncec                 S   s(   g | ] \}}d ||dkrdn|f qS )z%s=%sr   allr   ).0kvr   r   r   
<listcomp>s   s   z6SubprocessServerDataIngester.start.<locals>.<listcomp>,z--logdir=%sz--reload=%sz--samples-per-plugin=%sz--port=0z--port-file=%sz--die-after-stdinz0.5.0a0z--error-file=%sz	--verbosezSpawning data server: %r)stding{Gz?    z(exited with %d; check stderr for detailsz)Polling for data server port (attempt %d)zPort file contents: %r
g      ?zTTimed out while waiting for data server to start. It may still be running as pid %d.zlocalhost:%dz.Opened channel to data server at pid %d via %s   )timeoutz0Failed to communicate with data server at %s: %sz%sz#Got valid response from data server)1r	   tempfileTemporaryDirectoryospathjoinnamer    strintr"   itemsr   
expanduserr   at_least_versionappendloggerisEnabledForloggingINFODEBUGextendr$   info
subprocessPopenPIPEr0   Z_stdin_handletimesleeprangepoll_maybe_read_filestripDataServerStartupErrorendswithpidr   r!   r   ZGetExperimentRequestZGetExperimentgrpcZRpcErrorwarningr   r   )r
   ZtmpdirZport_file_pathZerror_file_pathreloadZsample_hint_pairsr   argspopenr(   imsgZport_file_contentsaddrr   reqer   r   r   r   f   s    

	



 
z"SubprocessServerDataIngester.startr   r   r   r   r   r   >   s   	
r   c              
   C   s|   z6t | }| W  d   W S 1 s*0    Y  W n@ tyv } z(|jtjkr`W Y d}~dS  W Y d}~n
d}~0 0 dS )z5Read a file, or return `None` on ENOENT specifically.N)openreadOSErrorerrnoENOENT)r9   infiler^   r   r   r   rP      s    
,rP   c                 C   s0   |  \}}|d tj| ||d}t|S )N)zgrpc.max_receive_message_lengthi   )options)Zchannel_configrA   rU   Zsecure_channelr   Z	make_stub)r\   r   credsre   Zchannelr   r   r   r      s    
r   c                   @   s   e Zd ZdS )NoDataServerErrorNr   r   r   r   r   r   r   rg      s   rg   c                   @   s   e Zd ZdS )rR   Nrh   r   r   r   r   rR      s   rR   c                   @   s,   e Zd ZdZdd Zedd Zdd ZdS )	ServerBinaryz'Information about a data server binary.c                 C   s"   || _ |durt|n|| _dS )at  Initializes a `ServerBinary`.

        Args:
          path: String path to executable on disk.
          version: PEP 396-compliant version string, or `None` if
            unknown or not applicable. Binaries at unknown versions are
            assumed to be bleeding-edge: if you bring your own binary,
            it's on you to make sure that it's up to date.
        N)_pathpkg_resourcesparse_version_version)r
   r9   versionr   r   r   r      s
    
zServerBinary.__init__c                 C   s   | j S r   )rj   r   r   r   r   r9      s    zServerBinary.pathc                 C   s   | j du rdS | j t|kS )a  Test whether the binary's version is at least the given one.

        Useful for gating features that are available in the latest data
        server builds from head, but not yet released to PyPI. For
        example, if v0.4.0 is the latest published version, you can
        check `at_least_version("0.5.0a0")` to include both prereleases
        at head and the eventual final release of v0.5.0.

        If this binary's version was set to `None` at construction time,
        this method always returns `True`.

        Args:
          required_version: PEP 396-compliant version string.

        Returns:
          Boolean.
        NT)rm   rk   rl   )r
   required_versionr   r   r   r@      s    
zServerBinary.at_least_versionN)r   r   r   r   r   r   r9   r@   r   r   r   r   ri      s
   
ri   c                  C   s   t jt} | rDtd|  t j| s8tdt| f t	| ddS t j
t jtdd}t j|rtd| t	|ddS zddl}W n ty   Y n80 | }|j}td|| |du rtd	t	||S td
dS )z5Get `ServerBinary` info or raise `NoDataServerError`.zServer binary (from env): %sz:Found environment variable %s=%s, but no such file exists.N)rn   serverzServer binary (from bundle): %sr   z+Server binary (from Python package v%s): %sz7TensorBoard data server not supported on this platform.z{TensorBoard data server not found. This mode is experimental. If building from source, pass --define=link_data_server=true.)r8   environget_ENV_DATA_SERVER_BINARYrD   rH   r9   isfilerg   ri   r:   dirname__file__existstensorboard_data_serverImportErrorr%   __version__)Z
env_resultZbundle_resultrx   Z
pkg_resultrn   r   r   r   get_server_binary
  s>    
r{   )r   rb   rD   r8   rI   r6   rL   rU   rk   Ztensorboard.datar   r   Ztensorboard.data.protor   Ztensorboard.utilr   Z
get_loggerrB   rs   ZDataIngesterr   r   rP   r   r&   rg   rR   ri   r{   r   r   r   r   <module>   s.    /