
    1Vh7.                     "   d 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 ddl	m
Z
 ddl	mZ  ej                         Zej                  d	        Z e       Zd
 Zd Z G d d      Z G d d      Z G d de      Z G d de      Z G d de      Zy)z4Functionality for loading events from a record file.    N)data_compat)dataclass_compat)tf)	event_pb2)platform_util)
tb_loggingc               #      K   d yw)z=Pre-Python-3.7-compatible standin for contextlib.nullcontext.N r
       f/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/backend/event_processing/event_file_loader.py_nullcontextr      s      
s   c                  f    	 ddl m}  | j                         S # t        t        f$ r	 t
        cY S w xY w)zBContext manager that best-effort silences TF deprecation warnings.r   deprecation)tensorflow.python.utilr   silenceImportErrorAttributeError_NULLCONTEXTr   s    r   _silence_deprecation_warningsr   )   s3    6""$$( s    00c                 
   t         j                  dk(  r:t        j                  d|        t	        t         j
                  j                  |       S 	 ddlm} |j                  }|r"t        j                  d|        t	        ||       S t        j                  d|        t               5  t         j                  j                  j                  j                  |       cddd       S # t        t        f$ r d}Y w xY w# 1 sw Y   yxY w)z@Returns an iterator over TF records for the given tfrecord file.stubz+Opening a stub record reader pointing at %sr   )pywrap_tensorflowNz'Opening a PyRecordReader pointing at %sz+Opening a tf_record_iterator pointing at %s)r   __version__loggerdebug_PyRecordReaderIteratorr   PyRecordReader_Newtensorflow.pythonr   r   r   compatv1iotf_record_iterator)	file_pathr   py_record_reader_news      r   _make_tf_record_iteratorr&   4   s     
~~ 	BIN&  33Y
 	
$70CC >	J&';YGGBIN*, 	A99<<??55i@	A 	A ( $#$	A 	As   C" %3C9"C65C69Dc                   &    e Zd ZdZd Zd Zd ZeZy)r   z7Python iterator for TF Records based on PyRecordReader.c                 Z   t         j                  j                  j                  j	                         5 } |t         j                  j                  |      dt         j                  j                  d      |      | _        ddd       | j                  st        d|z        y# 1 sw Y   $xY w)zConstructs a _PyRecordReaderIterator for the given file path.

        Args:
          py_record_reader_new: pywrap_tensorflow.PyRecordReader_New
          file_path: file path of the tfrecord file to read
        r    Nz-Failed to open a record reader pointing to %s)r   r    r!   errors raise_exception_on_not_ok_statusas_bytes_readerIOError)selfr%   r$   statuss       r   __init__z _PyRecordReaderIterator.__init__X   s     YY\\  AAC 	v/		""9-q"))2D2DR2H&DL	 ||?)K  		 	s   AB!!B*c                     | S Nr
   )r/   s    r   __iter__z _PyRecordReaderIterator.__iter__h   s    r   c                     	 | j                   j                          | j                   j                         S # t        j                  j                  $ r}t
        d }~ww xY wr3   )r-   GetNextr   r*   OutOfRangeErrorStopIterationrecord)r/   es     r   __next__z _PyRecordReaderIterator.__next__k   sN    	 LL  " ||""$$ yy(( 	 	 s   6 AAAN)__name__
__module____qualname____doc__r1   r4   r;   nextr
   r   r   r   r   U   s    A % Dr   r   c                   $    e Zd ZdZddZd Zd Zy)RawEventFileLoaderz?An iterator that yields Event protos as serialized bytestrings.c                 "   |t        d      t        j                  |      | _        || _        d| _        t        | j                        | _        | j                  r4t        | j                  d      st        j                  d       d| _        yyy)a	  Constructs a RawEventFileLoader for the given file path.

        Args:
          file_path: the event file path to read from
          detect_file_replacement: if True, when Load() is called, the loader
              will make a stat() call to check the size of the file. If it sees
              that the file has grown, it will reopen the file entirely (while
              preserving the current offset) before attempting to read from it.
              Otherwise, Load() will simply poll at EOF for new data.
        NzA file path is requiredreopenzFile replacement detection requested, but not enabled because TF record iterator impl does not support reopening. This functionality requires TensorFlow 2.9+F)
ValueErrorr   readahead_file_path
_file_path_detect_file_replacement
_file_sizer&   	_iteratorhasattrr   warning)r/   r$   detect_file_replacements      r   r1   zRawEventFileLoader.__init__x   s     677';;IF(?%1$//B((NNH2
 NN9
 -2D)2
(r   c              #     K   t         j                  d| j                         | j                  r| j	                         }||rUt         j                  d| j                         | j
                  j                          | j
                  j                          n,t         j                  d| j                  | j                         y	 	 t        | j
                         # t        $ r# t         j                  d| j                         Y nJt        j                  j                  $ r+}t         j                  d| j                  |       Y d}~nd}~ww xY wt         j                  d| j                         yw)a.  Loads all new events from disk as raw serialized proto bytestrings.

        Calling Load multiple times in a row will not 'drop' events as long as the
        return value is not iterated over.

        Yields:
          All event proto bytestrings in the file that have not been yielded yet.
        zLoading events from %sNz(Reopening %s since file size has changedzFSkipping attempt to poll %s since file size has not changed (still %d)zEnd of file in %szTruncated record in %s (%s)zNo more events in %s)r   r   rG   rH   CheckForIncreasedFileSizerJ   closerD   rI   r@   r8   r   r*   DataLossError)r/   has_increasedr:   s      r   LoadzRawEventFileLoader.Load   s     	-t?(( ::<M( LLB NN((*NN))+LL-	 
4>>**  ! 0$//B99**  :DOOQO 	+T__=s<   CE4C E4)EE4	E%!EE4E$E4c                 :   | j                   }	 t        j                  j                  j	                  | j
                        j                  | _         t        j                  d| j
                  | j                   |       |y| j                   |kD  ry| j                   |k  r3t        j                  d| j
                  || j                          || _         y# t        j                  j                  $ r+}t        j                  d| j
                  |       Y d}~yd}~ww xY w)a  Stats the file to get its updated size, returning True if it grew.

        If the stat call fails or reports a smaller size than was previously
        seen, then any previously cached size is left unchanged.

        Returns:
            boolean or None: True if the file size increased; False if it was
                the same or decreased; or None if neither case could be detected
                (either because the previous size had not been recorded yet, or
                because the stat call for the current size failed).
        zFailed to stat %s: %sNz(Stat on %s got size %d, previous size %sTz/File %s shrank from previous size %d to size %dF)rI   r   r"   gfilestatrG   lengthr*   OpErrorr   errorr   rL   )r/   previous_sizer:   s      r   rO   z,RawEventFileLoader.CheckForIncreasedFileSize   s     	 eekk..t?FFDO 	6OOOO		
  ??]*??]*NNA	 ,DO1 yy   	LL0$//1E	s   AC D/!DDN)F)r<   r=   r>   r?   r1   rS   rO   r
   r   r   rB   rB   u   s    I26)>V'r   rB   c                   "     e Zd ZdZ fdZ xZS )LegacyEventFileLoaderz,An iterator that yields parsed Event protos.c              #   v   K   t         |          D ]#  }t        j                  j	                  |       % yw)zLoads all new events from disk.

        Calling Load multiple times in a row will not 'drop' events as long as the
        return value is not iterated over.

        Yields:
          All events in the file that have not been yielded yet.
        N)superrS   r   Event
FromString)r/   r9   	__class__s     r   rS   zLegacyEventFileLoader.Load   s3      gln 	5F//,,V44	5s   69r<   r=   r>   r?   rS   __classcell__ra   s   @r   r\   r\      s    6
5 
5r   r\   c                   ,     e Zd ZdZ fdZ fdZ xZS )EventFileLoaderzAn iterator that passes events through read-time compat layers.

    Specifically, this includes `data_compat` and `dataclass_compat`.
    c                 2    t        |   |i | i | _        y r3   )r^   r1   _initial_metadata)r/   argskwargsra   s      r   r1   zEventFileLoader.__init__   s    $)&) "$r   c              #      K   t         |          D ]B  }t        j                  |      }t	        j                  || j
                        }|D ]  }|  D y wr3   )r^   rS   r   migrate_eventr   rh   )r/   eventeventsra   s      r   rS   zEventFileLoader.Load  s]     W\^ 	E--e4E%33t--F   	s   AA)r<   r=   r>   r?   r1   rS   rc   rd   s   @r   rf   rf      s    
$ r   rf   c                   "     e Zd ZdZ fdZ xZS )TimestampedEventFileLoaderzBAn iterator that yields (UNIX timestamp float, Event proto) pairs.c              #   T   K   t         |          D ]  }|j                  |f  yw)aO  Loads all new events and their wall time values from disk.

        Calling Load multiple times in a row will not 'drop' events as long as the
        return value is not iterated over.

        Yields:
          Pairs of (UNIX timestamp float, Event proto) for all events in the file
          that have not been yielded yet.
        N)r^   rS   	wall_time)r/   rm   ra   s     r   rS   zTimestampedEventFileLoader.Load  s-      W\^ 	+E??E**	+s   %(rb   rd   s   @r   rp   rp     s    L+ +r   rp   )r?   
contextlibtensorboardr   r   tensorboard.compatr   tensorboard.compat.protor   tensorboard.utilr   r   
get_loggerr   contextmanagerr   r   r   r&   r   rB   r\   rf   rp   r
   r   r   <module>rz      s     ;  # ( ! . * ' 
			  
 
 ~AB @p pf5. 5 + >+ +r   