
    DhO3                     T   d Z ddlZddlZddlZddlZddlZddlZddlmZmZm	Z	  e	j                  d      Z e	j                  d      Z ej                  ddd      Zd	ej                  d
e	j                   fdZde	j$                  d
e	j                   fdZde	j(                  e	j$                     d
e	j(                  e	j                      fdZ ej                  d      fde	j,                  dej                  d
efdZe	j2                  de	j4                  e	j6                  g e	j8                  e   f   e	j8                  e   f   d
e	j8                  e   fd       Ze	j2                  de	j4                  e	j6                  g e	j<                  e   f   e	j<                  e   f   d
e	j<                  e   fd       Zde	j4                  e	j<                  e   e	j6                  g e	j<                  e   f   e	j8                  e   e	j6                  g e	j8                  e   f   f   d
e	j4                  e	j<                  e   e	j8                  e   f   fdZ ej                  d      ej>                  ddfde	j$                  de	j$                  de	j4                  e	j<                  e   e	j6                  g e	j<                  e   f   f   de de	j(                  e	j$                     d
e	j<                  e   fdZ! ej                  d      ejD                  ddfde	j$                  de	j$                  de	j4                  e	j8                  e   e	j6                  de	j8                  e   f   f   de de	j(                  e	j$                     d
e	jF                  edf   fdZ$ddejJ                  fde	jF                  edf   de	j(                  e	j$                     de	j(                  e	j$                     d e	j(                  e	j6                  e	jF                  edf   e	j(                  e	j$                     e	j(                  e	j$                     e&ge	jN                  f      d!e	jP                  e	jR                  e	jN                  f   d
e	jF                  edf   fd"Z*ddejJ                  fde	j(                  e	j$                     de	j(                  e	j$                     d e	j(                  e	j6                  e	jF                  e	jN                  df   e	j(                  e	j$                     e	j(                  e	j$                     e&ge	jN                  f      d!e	jP                  e	jR                  e	jN                  f   d
e	j6                  e	j6                  ee	jF                  edf   f   ge	j6                  ee	jF                  edf   f   f   f
d#Z+y)$a#  
This module provides utility functions for handling time-related operations.

Functions:
- timedelta_to_seconds: Convert a timedelta to seconds with microseconds as
  fraction.
- delta_to_seconds: Convert a timedelta or numeric interval to seconds.
- delta_to_seconds_or_none: Convert a timedelta to seconds or return None.
- format_time: Format a timestamp (timedelta, datetime, or seconds) to a
  string.
- timeout_generator: Generate items from an iterable until a timeout is
  reached.
- aio_timeout_generator: Asynchronously generate items from an iterable until a
  timeout is reached.
- aio_generator_timeout_detector: Detect if an async generator has not yielded
  an element for a set amount of time.
- aio_generator_timeout_detector_decorator: Decorator for
  aio_generator_timeout_detector.
    N)aio
exceptionstypes_T_Pi     )yearmonthdaydeltareturnc                     | j                   r| j                   dz  }nd}|| j                  z  }|| j                  dz  dz  dz  z  }|S )a  Convert a timedelta to seconds with the microseconds as fraction.

    Note that this method has become largely obsolete with the
    `timedelta.total_seconds()` method introduced in Python 2.7.

    >>> from datetime import timedelta
    >>> '%d' % timedelta_to_seconds(timedelta(days=1))
    '86400'
    >>> '%d' % timedelta_to_seconds(timedelta(seconds=1))
    '1'
    >>> '%.6f' % timedelta_to_seconds(timedelta(seconds=1, microseconds=1))
    '1.000001'
    >>> '%.6f' % timedelta_to_seconds(timedelta(microseconds=1))
    '0.000001'
    gư>r   <      )microsecondssecondsdays)r   totals     s/var/www/fastuser/data/www/generator.snapmosaic.io/flask_app/venv/lib/python3.12/site-packages/python_utils/time.pytimedelta_to_secondsr   (   sQ    " ""T)	U]]E	UZZ"_r!B&&EL    intervalc                     t        | t        j                        rt        |       S t        | t        t
        f      r| S t        dt        |        d|       )am  
    Convert a timedelta to seconds.

    >>> delta_to_seconds(datetime.timedelta(seconds=1))
    1
    >>> delta_to_seconds(datetime.timedelta(seconds=1, microseconds=1))
    1.000001
    >>> delta_to_seconds(1)
    1
    >>> delta_to_seconds('whatever')  # doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    TypeError: Unknown type ...
    Unknown type : )
isinstancedatetime	timedeltar   intfloat	TypeErrortyper   s    r   delta_to_secondsr$   B   sO     (H../#H--	HsEl	+-X'7r(FGGr   c                     | yt        |       S )z.Convert a timedelta to seconds or return None.N)r$   r#   s    r   delta_to_seconds_or_noner&   Y   s     ))r   r   	timestamp	precisionc                 f   |j                         }t        | t              rt        |       } t        | t        t        f      r	 t        j                  |       } t        | t
        j                        r7| j                         }|||z  z
  }t        t        j                  |            S t        | t
        j
                        rbt        | d      r| j                         }nt        | t        z
        }|||z  z
  }	 t
        j
                  j                  |      }t        |      S t        | t
        j                         rt        |       S | yt#        dt%        |        d|       # t        $ r d} Y w xY w# t        t        f$ r' t
        j
                  j                  }Y t        |      S w xY w)a/  Formats timedelta/datetime/seconds.

    >>> format_time('1')
    '0:00:01'
    >>> format_time(1.234)
    '0:00:01'
    >>> format_time(1)
    '0:00:01'
    >>> format_time(datetime.datetime(2000, 1, 2, 3, 4, 5, 6))
    '2000-01-02 03:04:05'
    >>> format_time(datetime.date(2000, 1, 2))
    '2000-01-02'
    >>> format_time(datetime.timedelta(seconds=3661))
    '1:01:01'
    >>> format_time(None)
    '--:--:--'
    >>> format_time(format_time)  # doctest: +ELLIPSIS
    Traceback (most recent call last):
        ...
    TypeError: Unknown type ...

    r'   Nr(   z--:--:--r   r   )total_secondsr   strr    r   r   r   OverflowErrorhasattrr(   r   epochfromtimestamp
ValueErrorOSErrormaxdater!   r"   )r(   r)   precision_secondsr   dts        r   format_timer7   c   s   4 "//1)S!)$	)c5\*	 **9=I )X//0))+W'8898%%g677	Ix00	19k*))+G*9u+<=G W'889	'""009B 2w	Ix}}	-9~		-Y'89-HII;  	I	* G$ 	'""&&B2w	's$   E( <E: (E76E7:)F0/F0iterablec                      y N r8   s    r   _to_iterabler=      s     "r   c                      y r:   r;   r<   s    r   r=   r=      s    
 r   c                 *    t        |       r |        S | S r:   )callabler<   s    r   r=   r=      s     zr   g      ?timeoutinterval_multipliermaximum_intervalc              #   (  K   t        |      }t        |      }t        |      }t        |       t        j                         z   }|D ]H  }	|	 t        j                         |k\  r yt        j
                  |       ||z  }|s=t        ||      }J yw)a  
    Generator that walks through the given iterable (a counter by default)
    until the float_timeout is reached with a configurable float_interval
    between items.

    This can be used to limit the time spent on a slow operation. This can be
    useful for testing slow APIs so you get a small sample of the data in a
    reasonable amount of time.

    >>> for i in timeout_generator(0.1, 0.06):
    ...     # Put your slow code here
    ...     print(i)
    0
    1
    2
    >>> timeout = datetime.timedelta(seconds=0.1)
    >>> interval = datetime.timedelta(seconds=0.06)
    >>> for i in timeout_generator(timeout, interval, itertools.count()):
    ...     print(i)
    0
    1
    2
    >>> for i in timeout_generator(1, interval=0.1, iterable='ab'):
    ...     print(i)
    a
    b

    >>> timeout = datetime.timedelta(seconds=0.1)
    >>> interval = datetime.timedelta(seconds=0.06)
    >>> for i in timeout_generator(timeout, interval, interval_multiplier=2):
    ...     print(i)
    0
    1
    2
    N)r$   r&   r=   timeperf_countersleepmin
rA   r   r8   rB   rC   float_intervalfloat_maximum_interval	iterable_enditems
             r   timeout_generatorrO      s     X -X6N4L5 X&I
7
#d&7&7&9
9C 
I
#%

>"--! 1GHN
Is   B BB.c                N  K   t        |      }t        |      }t        |      }t        |       t        j                         z   }|2 3 d{   }	|	 t        j                         |k\  r yt        j                  |       d{    ||z  }|sLt        ||      }Y7 T7 6 yw)as  
    Async generator that walks through the given async iterable (a counter by
    default) until the float_timeout is reached with a configurable
    float_interval between items.

    The interval_exponent automatically increases the float_timeout with each
    run. Note that if the float_interval is less than 1, 1/interval_exponent
    will be used so the float_interval is always growing. To double the
    float_interval with each run, specify 2.

    Doctests and asyncio are not friends, so no examples. But this function is
    effectively the same as the `timeout_generator` but it uses `async for`
    instead.
    N)r$   r&   r=   rE   rF   asynciorG   rH   rI   s
             r   aio_timeout_generatorrR     s     . -X6N4L5 X&I
7
#d&7&7&9
9C 
I 
Id
#%mmN+++--! 1GHN
I 	,  sB   AB%B#
BB#7B%B!B%B%B#!B%#B%	generatortotal_timeout
on_timeouton_timeout_kwargsc                  K   |d}n t        j                         t        |      z   }t        j                  |      }	 	 |r,t        j                         |k\  rt        j                  d      |r0t        j                  | j                         |       d{    n| j                          d{    }7 $7 
# t
        j                  $ r!}| || |||fi | d{  7   Y d}~yd}~wt        $ r Y yw xY ww)a  
    This function is used to detect if an asyncio generator has not yielded
    an element for a set amount of time.

    The `on_timeout` argument is called with the `generator`, `timeout`,
    `total_timeout`, `exception` and the extra `**kwargs` to this function as
    arguments.
    If `on_timeout` is not specified, the exception is reraised.
    If `on_timeout` is `None`, the exception is silently ignored and the
    generator will finish as normal.
    NzTotal timeout reached)
rE   rF   r$   python_utilsr&   rQ   TimeoutErrorwait_for	__anext__StopAsyncIteration)rS   rA   rT   rU   rV   total_timeout_end	timeout_s	exceptions           r   aio_generator_timeout_detectorr`   /  s    8   --/2B3
 
 55g>I
	 T%6%6%8<M%M**+  #,,Y-@-@-BINNN%//111  O1## 		% !	
 (   ! 		sk   <D AB? B;B? 2B=3B? :D ;B? =B? ?C=C.#C&$C.)D .C=:D <C==D c                      dt         j                  t        t         j                  t        df   f   dt         j                  t        t         j                  t        df   f   f fd}|S )z7A decorator wrapper for aio_generator_timeout_detector.rS   Nr   c           	           t        j                         dt        j                  dt        j                  dt
        j                  t        df   f fd       }|S )zThe decorator itself.argskwargsr   Nc                  .    t         | i |fi S r:   )r`   )rc   rd   rS   rU   rV   rA   rT   s     r   wrapperz^aio_generator_timeout_detector_decorator.<locals>._timeout_detector_decorator.<locals>.wrapper  s2    
 24*6*	
 $ r   )	functoolswrapsr   rc   rd   r   AsyncGeneratorr   )rS   rf   rU   rV   rA   rT   s   ` r   _timeout_detector_decoratorzMaio_generator_timeout_detector_decorator.<locals>._timeout_detector_decorator  s]    
 
	#
	77
	ii
	 !!"d(+
	 
	 
$
	 r   )r   Callabler   ri   r   )rA   rT   rU   rV   rj   s   ```` r   (aio_generator_timeout_detector_decoratorrl   o  s_    *>>"e&:&:2t8&D"DE	E00T::	; ( '&r   ),__doc__rQ   r   rg   	itertoolsrE   rX   r   r   r   TypeVarr   	ParamSpecr   r/   r   Numberr   
delta_typer$   Optionalr&   timestamp_typer,   r7   overloadUnionrk   AsyncIterabler=   Iterablecountr    rO   acountri   rR   reraiseBaseExceptionAnyMappingTextr`   rl   r;   r   r   <module>r      s(  *       / /U]]4U__T
 	t1!4 2 2 u|| 4Hu// HELL H.*nnU--.*
^^ELL!* %7H$6$6q$A?J##?J!!?J 	?JD "kkr5..r223B	!"
 " " kkr5>>"--.r0BB ^^B	 kkrr5>>"--.Br5..r223	5 [[#U%8%8%<<=  "4!3!3A!> 	!$9==I=I=I kkrENN2u~~b/A+ABB=I =I nnU%5%56=I ^^B=ID "4!3!3A!> 	

!$9=(I(I(I kkBU5H5H5L0L!MM(I (I nnU%5%56(I "d(#(IZ 156: 	=##BH-=^^E,,-= >>%"2"23= $$RX.u//0u//0	 II	

	= uzz599'<==  "d(#!=B 156: 	)'^^E,,-)'>>%"2"23)' $$UYY_5u//0u//0	 II	

)' uzz599'<=)' ^^
^^B,,RX6678	NN2u++BH5568)'r   