
    1Vh                     |    d Z ddlmZ ddlmZ ddlmZ ddlmZ  ej                         Z	 e
       Z G d d      Zy)	z1Implementation for a multi-file directory loader.    )directory_watcher)
io_wrapper)tf)
tb_loggingc                   2    e Zd ZdZd d fdZd Zd Zd Zy)	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                      yNT )xs    e/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/backend/event_processing/directory_loader.py<lambda>zDirectoryLoader.<lambda>3           c                      yr
   r   )	timestamps    r   r   zDirectoryLoader.<lambda>4   r   r   c                     |t        d      |t        d      || _        || _        || _        || _        i | _        i | _        y)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	directoryloader_factorypath_filteractive_filters        r   __init__zDirectoryLoader.__init__/   sV    * 677!;<<#-'+!r   c              #      K   	 t        j                   j                        }t         fd|D              }|D ]  } j	                  |      D ]  }|   y# t
        j                  j                  $ rw}t
        j                  j                  j                   j                        s"t        j                  d j                  z        t        j                  d|z         Y d}~yd}~ww xY ww)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   F   K   | ]  }j                  |      s|  y w)N)r   ).0pr   s     r   	<genexpr>z'DirectoryLoader.Load.<locals>.<genexpr>[   s     H43D3DQ3G1Hs   !!z)Directory %s has been permanently deletedz&Ignoring error during file loading: %sN)r   ListDirectoryAbsoluter   sorted	_LoadPathr   errorsOpErroriogfileexistsr   DirectoryDeletedErrorloggerinfo)r   	all_pathspathspathvaluees   `     r   LoadzDirectoryLoader.LoadO   s     	J"88IIHiHHE  !^^D1  EK   yy   	J55;;%%doo6'==?oo& 
 DqHII	Js0   C1AA C1C.7A-C)$C1)C..C1c              #   P  K   | j                   j                  |d      }|t        u s| j                  ||      rt        j                  d|       y| j                  j                  |d      }|!	 | j                  |      }|| j                  |<   t        j                  d|       |j                         D ]  \  }}|||kD  r|}|  | j                  ||      s|| j                   |<   yy# t        j                  j                  $ r t        j                  d|       Y yw xY ww)zGenerator 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)   NotFoundErrorr0   r6   )r   r3   max_timestamploaderr   r4   s         r   r(   zDirectoryLoader._LoadPathh   s!     ,,00t<I%)=)=-*
 LL4d;""4.>--d3
 #)DMM$/6 & 	Iu$	M(A )K	 ##D-8)6D  & 9 99** ;TBs+   A,D&/C-  A-D&-3D# D&"D##D&c                     t         j                  d||       |2| j                  |      s!t        | j                  |<   | j
                  |= yy)zNIf max_timestamp is inactive, returns True and marks the path as
        such.z"Checking active status of %s at %sTF)r/   r;   r   r9   r   r   )r   r3   r=   s      r   r:   zDirectoryLoader._MarkIfInactive   sJ     	94O$T-@-@-O)2D  &d#r   N)__name__
__module____qualname____doc__r    r6   r(   r:   r   r   r   r   r       s&    $ #,"@J27@r   r   N)rC   $tensorboard.backend.event_processingr   r   tensorboard.compatr   tensorboard.utilr   
get_loggerr/   objectr9   r   r   r   r   <module>rI      s@     8 C ; ! ' 
			  H	p pr   