U
    Vc                     @   sH   d Z ddlZddlmZ dZdd Zdd Zd	d
 Zdd Z	dd Z
dS )zLoad plugin assets from disk.    N)tfZpluginsc                 C   s   t jjtj| |S )z2Helper that returns if parent/item is a directory.)r   iogfileisdirospathjoin)parentitem r   Z/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/backend/event_processing/plugin_asset_util.py_IsDirectory   s    r   c                 C   s   t j| t|S )z-Returns the plugin directory for plugin_name.)r   r   r   _PLUGINS_DIR)logdirplugin_namer   r   r   PluginDirectory   s    r   c                    sP   t j| t ztjj }W n tjj	k
r<   g  Y S X  fdd|D S )ab  List all the plugins that have registered assets in logdir.

    If the plugins_dir does not exist, it returns an empty list. This maintains
    compatibility with old directories that have no plugins written.

    Args:
      logdir: A directory that was created by a TensorFlow events writer.

    Returns:
      a list of plugin names, as strings
    c                    s*   g | ]"}| d st |r|d qS /)endswithr   rstrip.0xZplugins_dirr   r   
<listcomp>6   s   
 
zListPlugins.<locals>.<listcomp>)
r   r   r   r   r   r   r   listdirerrorsNotFoundError)r   entriesr   r   r   ListPlugins#   s    

r   c                 C   sF   t | |}zdd tjj|D W S  tjjk
r@   g  Y S X dS )a  List all the assets that are available for given plugin in a logdir.

    Args:
      logdir: A directory that was created by a TensorFlow summary.FileWriter.
      plugin_name: A string name of a plugin to list assets for.

    Returns:
      A string list of available plugin assets. If the plugin subdirectory does
      not exist (either because the logdir doesn't exist, or because the plugin
      didn't register) an empty list is returned.
    c                 S   s   g | ]}| d qS r   )r   r   r   r   r   r   L   s     zListAssets.<locals>.<listcomp>N)r   r   r   r   r   r   r   )r   r   Z
plugin_dirr   r   r   
ListAssets=   s
    
r    c              
   C   s   t jt| ||}z2tjj|d}| W  5 Q R  W S Q R X W nX tj	j
k
rj   td| Y n6 tj	jk
r } ztd||f W 5 d}~X Y nX dS )ak  Retrieve a particular plugin asset from a logdir.

    Args:
      logdir: A directory that was created by a TensorFlow summary.FileWriter.
      plugin_name: The plugin we want an asset from.
      asset_name: The name of the requested asset.

    Returns:
      string contents of the plugin asset.

    Raises:
      KeyError: if the asset does not exist.
    rzAsset path %s not foundz(Couldn't read asset path: %s, OpError %sN)r   r   r   r   r   r   r   ZGFilereadr   r   KeyErrorZOpError)r   r   Z
asset_nameZ
asset_pathfer   r   r   RetrieveAssetQ   s     
r&   )__doc__os.pathr   Ztensorboard.compatr   r   r   r   r   r    r&   r   r   r   r   <module>   s   