a
    d=ic                     @   st   d Z ddlZddlZddlZddlZddlZG dd deZdd Zddd	Z	d
d Z
dddZdd Zdd ZdS )z4Utilities for use by the uploader command line tool.    Nc                   @   s    e Zd ZdZdd Zdd ZdS )RateLimiterz>Helper class for rate-limiting using a fixed minimum interval.c                 C   s   t | _|| _d| _dS )zMConstructs a RateLimiter that permits a tick() every
        `interval_secs`.r   N)time_time_interval_secs_last_called_secs)selfZinterval_secs r   j/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/tensorboard/uploader/util.py__init__   s    zRateLimiter.__init__c                 C   s:   | j | j | j  }|dkr*| j| | j | _ dS )zLBlocks until it has been at least `interval_secs` since last
        tick().r   N)r   r   r   r   sleep)r   Z	wait_secsr   r   r	   tick#   s
    zRateLimiter.tickN)__name__
__module____qualname____doc__r
   r   r   r   r   r	   r      s   r   c                  C   sZ   t jdkr2t d} | r| S t d} | r.| S dS t d}|rD|S t jt jddS )zDReturns a platform-specific root directory for user config settings.ntLOCALAPPDATAAPPDATANXDG_CONFIG_HOME~z.config)osnamegetenvpathjoin
expanduser)appdataZxdg_config_homer   r   r	   get_user_config_directory.   s    



r   Fc              
   C   s   |rt jdkrtdzt j| } t j| }zt | W n2 typ } z|jtj	kr\ W Y d}~n
d}~0 0 |rt 
|d t| d  |rt 
| d W n4 ty } ztd| |f W Y d}~n
d}~0 0 dS )a  Creates a file and its containing directories, if they don't already
    exist.

    If `private` is True, the file will be made private (readable only by the
    current user) and so will the leaf directory. Pre-existing contents of the
    file are not modified.

    Passing `private=True` is not supported on Windows because it doesn't support
    the relevant parts of `os.chmod()`.

    Args:
      path: str, The path of the file to create.
      private: boolean, Whether to make the file and leaf directory readable only
        by the current user.

    Raises:
      RuntimeError: If called on Windows with `private` set to True.
    r   z.Creating private file not supported on WindowsNi  ai  zFailed to create file %s: %s)r   r   RuntimeErrorr   realpathdirnamemakedirsOSErrorerrnoEEXISTchmodopencloseEnvironmentError)r   privateZleaf_direr   r   r	   make_file_with_directoriesC   s"    r,   c                 C   s$   t || _t t|d d | _dS )aE  Sets a `Timestamp` proto message to a floating point UNIX time.

    This is like `pb.FromNanoseconds(int(seconds_since_epoch * 1e9))` but
    without introducing floating-point error.

    Args:
      pb: A `google.protobuf.Timestamp` message to mutate.
      seconds_since_epoch: A `float`, as returned by `time.time`.
       i ʚ;N)intsecondsroundZnanos)ZpbZseconds_since_epochr   r   r	   set_timestampi   s    

r1   c                 C   s   t j | jd }|t jdd }|du r4t j  }|jdd| }dd }d}|t jddk rfd	}nx|t jd
dk r|t| dd}nR|t jd
dk r|t| d dd}n(|t jd
dk r|t| d dd}|durd| nd}t|| S )a  Converts a `timestamp_pb2.Timestamp` to human-readable string.

    This always includes the absolute date and time, and for recent dates
    may include a relative time like "(just now)" or "(2 hours ago)". It
    should thus be used for ephemeral values. Use `format_time_absolute`
    if the output will be persisted.

    Args:
      timestamp_pb: A `google.protobuf.timestamp_pb2.Timestamp` value to
        convert to string. The input will not be modified.
      now: A `datetime.datetime` object representing the current time,
        used for determining relative times like "just now". Optional;
        defaults to `datetime.datetime.now()`.

    Returns:
      A string suitable for human consumption.
    iQ )r/   Nr   )microsecondc                 S   s   d| | dkr|n|f S Nz	%d %s agor-   r   nZsingularpluralr   r   r	   ago_text   s    zformat_time.<locals>.ago_text   zjust nowr-   )minutessecondr/   )hours<   minuter9   )daysi  hourr;   z (%s) )	datetimefromtimestampr/   	timedeltanowreplacer.   total_secondsstr)timestamp_pbrD   dtZagor7   relativeZrelative_partr   r   r	   format_timew   s"    
rK   c                 C   s   t j | j}|dS )a;  Converts a `timestamp_pb2.Timestamp` to UTC time string.

    This will always be of the form "2001-02-03T04:05:06Z".

    Args:
      timestamp_pb: A `google.protobuf.timestamp_pb2.Timestamp` value to
        convert to string. The input will not be modified.

    Returns:
      An RFC 3339 date-time string.
    z%Y-%m-%dT%H:%M:%SZ)rA   utcfromtimestampr/   strftime)rH   rI   r   r   r	   format_time_absolute   s    rN   c                 C   s   d| | dkr|n|f S r3   r   r4   r   r   r	   	_ngettext   s    rO   )F)N)r   rA   r$   r   os.pathr   objectr   r   r,   r1   rK   rN   rO   r   r   r   r	   <module>   s   
&
.