a
    d=icJ  ã                   @   sT   d Z ddlZddlZddlmZ ddlmZ ddlmZ e ¡ Z	G dd„ de
ƒZdS )zCLoader for event file data for an entire TensorBoard log directory.é    N)Údirectory_watcher)Ú
io_wrapper)Ú
tb_loggingc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚLogdirLoadera  Loader for a root log directory, maintaining multiple DirectoryLoaders.

    This class takes a root log directory and a factory for DirectoryLoaders, and
    maintains one DirectoryLoader per "logdir subdirectory" of the root logdir.

    Note that this class is not thread-safe.
    c                 C   s6   |du rt dƒ‚|du r t dƒ‚|| _|| _i | _dS )aZ  Constructs a new LogdirLoader.

        Args:
          logdir: The root log directory to load from.
          directory_loader_factory: A factory for creating DirectoryLoaders. The
            factory should take a path and return a DirectoryLoader.

        Raises:
          ValueError: If logdir or directory_loader_factory are None.
        NzA logdir is requiredz&A directory loader factory is required)Ú
ValueErrorÚ_logdirÚ_directory_loader_factoryÚ_directory_loaders)ÚselfZlogdirZdirectory_loader_factory© r   ús/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/tensorboard/uploader/logdir_loader.pyÚ__init__&   s    zLogdirLoader.__init__c                 C   s¨   t  d| j¡ tƒ }t | j¡D ]D}tj || j¡}| 	|¡ || j
vr t  d|¡ |  |¡| j
|< q t| j
ƒ| }|r–|D ]}t  d|¡ | j
|= q|t  d| j¡ dS )zÉFinds new runs within `logdir` and makes `DirectoryLoaders` for
        them.

        In addition, any existing `DirectoryLoader` whose run directory
        no longer exists will be deleted.
        zStarting logdir traversal of %sz&- Adding run for relative directory %sz(- Removing run for relative directory %szEnding logdir traversal of %sN)ÚloggerÚinfor   Úsetr   ZGetLogdirSubdirectoriesÚosÚpathÚrelpathÚaddr	   r   )r
   Z	runs_seenÚsubdirÚrunZ
stale_runsr   r   r   Úsynchronize_runs:   s     

ÿ
zLogdirLoader.synchronize_runsc                 C   sP   t | jƒ}t dt|ƒ¡ t ¡ }t|ƒD ] }| j| }|  | 	¡ ¡||< q*|S )aé  Returns tf.Event generators for each run's `DirectoryLoader`.

        Warning: the generators are stateful and consuming them will affect the
        results of any other existing generators for that run; calling code should
        ensure it takes events from only a single generator per run at a time.

        Returns:
          Dictionary containing an entry for each run, mapping the run name to a
          generator yielding tf.Event protobuf objects loaded from that run.
        z-Creating event loading generators for %d runs)
Úlistr	   r   r   ÚlenÚcollectionsÚOrderedDictÚsortedÚ_wrap_loader_generatorÚLoad)r
   ÚrunsZrun_to_loaderÚrun_nameÚloaderr   r   r   Úget_run_eventsR   s    

zLogdirLoader.get_run_eventsc                 c   s0   z|D ]
}|V  qW n t jy*   Y dS 0 dS )zMWraps `DirectoryLoader` generator to swallow
        `DirectoryDeletedError`.N)r   ZDirectoryDeletedError)r
   Zloader_generatorÚitemr   r   r   r   e   s
    z#LogdirLoader._wrap_loader_generatorN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r   r   r   r   r   r      s
   r   )r'   r   r   Z$tensorboard.backend.event_processingr   r   Ztensorboard.utilr   Z
get_loggerr   Úobjectr   r   r   r   r   Ú<module>   s   