U
    Vºc  ã                   @   sV   d Z ddlmZ ddlmZ ddlmZ ddlmZ e ¡ Z	e
ƒ ZG dd„ de
ƒZdS )	z1Implementation for a multi-file directory loader.é    )Údirectory_watcher)Ú
io_wrapper)Útf)Ú
tb_loggingc                   @   s>   e Zd ZdZdd„ dd„ fdd„Zdd„ Zd	d
„ Zdd„ ZdS )ÚDirectoryLoaderaŒ  Loader for an entire directory, maintaining multiple active file
    loaders.

    This class takes a directory, a factory for loaders, and optionally a
    path filter and watches all the paths inside that directory for new data.
    Each file loader created by the factory must read a path and produce an
    iterator of (timestamp, value) pairs.

    Unlike DirectoryWatcher, this class does not assume that only one file
    receives new data at a time; there can be arbitrarily many active files.
    However, any file whose maximum load timestamp fails an "active" predicate
    will be marked as inactive and no longer checked for new data.
    c                 C   s   dS ©NT© )Úxr   r   úY/tmp/pip-unpacked-wheel-g8kmtpbc/tensorboard/backend/event_processing/directory_loader.pyÚ<lambda>3   ó    zDirectoryLoader.<lambda>c                 C   s   dS r   r   )Ú	timestampr   r   r
   r   4   r   c                 C   sH   |dkrt dƒ‚|dkr t dƒ‚|| _|| _|| _|| _i | _i | _dS )a²  Constructs a new MultiFileDirectoryLoader.

        Args:
          directory: The directory to load files from.
          loader_factory: A factory for creating loaders. The factory should take a
            path and return an object that has a Load method returning an iterator
            yielding (unix timestamp as float, value) pairs for any new data
          path_filter: If specified, only paths matching this filter are loaded.
          active_filter: If specified, any loader whose maximum load timestamp does
            not pass this filter will be marked as inactive and no longer read.

        Raises:
          ValueError: If directory or loader_factory are None.
        NzA directory is requiredzA loader factory is required)Ú
ValueErrorÚ
_directoryÚ_loader_factoryÚ_path_filterÚ_active_filterÚ_loadersÚ_max_timestamps)ÚselfÚ	directoryZloader_factoryZpath_filterZactive_filterr   r   r
   Ú__init__/   s    zDirectoryLoader.__init__c              
   #   s¢   zFt  ˆ j¡}t‡ fdd„|D ƒƒ}|D ]}ˆ  |¡D ]
}|V  q6q(W nV tjjk
rœ } z4tjj	 
ˆ j¡s~t dˆ j ¡‚nt d| ¡ W 5 d}~X Y nX dS )a  Loads new values from all active files.

        Yields:
          All values that have not been yielded yet.

        Raises:
          DirectoryDeletedError: If the directory has been permanently deleted
            (as opposed to being temporarily unavailable).
        c                 3   s   | ]}ˆ   |¡r|V  qd S )N)r   )Ú.0Úp©r   r   r
   Ú	<genexpr>[   s     
 z'DirectoryLoader.Load.<locals>.<genexpr>z)Directory %s has been permanently deletedz&Ignoring error during file loading: %sN)r   ZListDirectoryAbsoluter   ÚsortedÚ	_LoadPathr   ÚerrorsZOpErrorÚioZgfileÚexistsr   ZDirectoryDeletedErrorÚloggerÚinfo)r   Z	all_pathsÚpathsÚpathÚvalueÚer   r   r
   ÚLoadO   s    
ÿÿzDirectoryLoader.Loadc                 c   sÚ   | j  |d¡}|tks"|  ||¡r2t d|¡ dS | j |d¡}|dkrˆz|  |¡}W n& tj	j
k
r|   t d|¡ Y dS X || j|< t d|¡ | ¡ D ]"\}}|dks´||kr¸|}|V  qœ|  ||¡sÖ|| j |< dS )zÍGenerator for values from a single path's loader.

        Args:
          path: the path to load from

        Yields:
          All values from this path's loader that have not been yielded yet.
        NzSkipping inactive path %szSkipping nonexistent path %szLoading data from path %s)r   ÚgetÚ	_INACTIVEÚ_MarkIfInactiver!   Údebugr   r   r   r   ZNotFoundErrorr"   r'   )r   r$   Úmax_timestampÚloaderr   r%   r   r   r
   r   h   s,    	 ÿ
zDirectoryLoader._LoadPathc                 C   s:   t  d||¡ |dk	r6|  |¡s6t| j|< | j|= dS dS )zNIf max_timestamp is inactive, returns True and marks the path as
        such.z"Checking active status of %s at %sNTF)r!   r+   r   r)   r   r   )r   r$   r,   r   r   r
   r*   ˆ   s    
zDirectoryLoader._MarkIfInactiveN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r   r*   r   r   r   r
   r       s   û
  r   N)r1   Z$tensorboard.backend.event_processingr   r   Ztensorboard.compatr   Ztensorboard.utilr   Z
get_loggerr!   Úobjectr)   r   r   r   r   r
   Ú<module>   s   