
    Zi                     4   d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	mZm
Z
 d dlmZ d dlmZmZ d dlmZ d dlZd dlmZmZ  e j        d	          Zd
 Zd Zd Zd Zd Zd Zd Zd Zdedee          fdZ!de"fdZ# G d de          Z$d Z%d Z&d Z'dS )    N)defaultdict)asdictis_dataclass)datedatetimetimezone)Decimal)AnyOptional)UUID)tzlocaltzutcposthogc                 L    | j         du p| j                             |           du S )z1Determines if a given datetime.datetime is naive.N)tzinfo	utcoffset)dts    H/root/projects/butler/venv/lib/python3.11/site-packages/posthog/utils.pyis_naiver      s)    9?	 3 3B 7 74 ??    c                 H    | j         | j        | j        dz  dz  z   dz  z   dz  S )z2Determines total seconds with python < 2.7 compat.   i  g    .A)microsecondssecondsdays)deltas    r   total_secondsr      s/     %-%*r/D2H"HC!OOSVVVr   c                     t          |           rmt          j                    | z
  }t          |          dk     r"|                     t                                S |                     t                                S | S )z:Attempts to convert a naive datetime to an aware datetime.   r   )r   r   nowr   replacer   r   )r   r   s     r   guess_timezoner#      sn    || 
. #!## ::WYY:/// ::UWW:---Ir   c                 D    |                      d          r
| d d         S | S )N/)endswith)hosts    r   remove_trailing_slashr)   -   s)    }}S CRCyKr   c                    t          | t                    rt          |           S t          | t                    rt	          |           S t          | t
          j        t          t          j	        t          t          t          d           f          r| S t          | t          t          t          f          rt!          |           S 	 t#          | d          r)t%          | j                  r|                                 } n8t#          | d          r(t%          | j                  r|                                 } n4# t*          $ r'}t,                              d|            Y d }~nd }~ww xY wt          | t(                    rt1          |           S t3          |           r$t          | t                    st5          |           S t7          |           S )N
model_dumpdictz)Could not serialize Pydantic-like model: )
isinstancer	   floatr   strsixstring_typesboolnumbersNumberr   r   typesetlisttuple_clean_listhasattrcallabler+   r,   	TypeErrorlogdebug_clean_dictr   _clean_dataclass_coerce_unicode)itemes     r   cleanrD   3   s   $   T{{$ 4yysw~xtDzzR   $dE*++ !4   	4&& 	8DO+D+D 	??$$DDT6"" 	x	':': 	99;;D   		AaAABBB $ !4   D &*T4"8"8 &%%%4   s   A1D5 5
E&?E!!E&c                     d | D             S )Nc                 ,    g | ]}t          |          S  )rD   ).0rB   s     r   
<listcomp>z_clean_list.<locals>.<listcomp>Q   s    ***DE$KK***r   rG   )list_s    r   r9   r9   P   s    **E****r   c           
          i }t          j        |           D ]R\  }}	 t          |          ||<   # t          $ r- t                              d||t          |                     Y Ow xY w|S )NzXDictionary values must be serializeable to JSON "%s" value %s of type %s is unsupported.)r0   	iteritemsrD   r<   r=   warningr5   )dict_datakvs       r   r?   r?   T   s    De$$ 	 	1	AhhDGG 	 	 	KKjQ	    	 Ks   04A'&A'c                 B    t          |           }t          |          }|S N)r   r?   )
dataclass_rO   s     r   r@   r@   c   s!    *DtDKr   cmplxreturnc                 T   d}	 t          | t                    r|                     dd          }nt          | t                    r| }n`# t          $ rS}d                    t          t          |j                            }t          	                    d|           Y d}~dS d}~ww xY w|S )a  
    In theory, this method is only called
    after many isinstance checks are carried out in `utils.clean`.
    When we supported Python 2 it was safe to call `decode` on a `str`
    but in Python 3 that will throw.
    So, we check if the input is bytes and only call `decode` in that case.

    Previously we would always call `decode` on the input
    That would throw an error.
    Then we would call `decode` on the stringified error
    That would throw an error.
    And then we would return `None`

    To avoid a breaking change, we can maintain the behavior
    that anything which did not have `decode` in Python 2
    returns None.
    Nzutf-8strict:zError decoding: %s)
r-   bytesdecoder/   	Exceptionjoinmapargsr=   rM   )rU   rB   	exceptions      r   rA   rA   i   s    $ DeU## 	<<22DDs## 	D   xxC0011($///ttttt
 Ks   AA 
B%AB  B%c                 \    	 t          j        |            dS # t           j        $ r Y dS w xY w)NTF)recompileerror)values    r   is_valid_regexrf      s@    

5t8   uus    ++c                   (     e Zd Z fdZ fdZ xZS )SizeLimitedDictc                 H     t                      j        |i | || _        d S rS   )super__init__max_size)selfrl   r_   kwargs	__class__s       r   rk   zSizeLimitedDict.__init__   s*    $)&))) r   c                     t          |           | j        k    r|                                  t                                          ||           d S rS   )lenrl   clearrj   __setitem__)rm   keyre   ro   s      r   rs   zSizeLimitedDict.__setitem__   sC    t99%%JJLLLC'''''r   )__name__
__module____qualname__rk   rs   __classcell__)ro   s   @r   rh   rh      sQ        ! ! ! ! !( ( ( ( ( ( ( ( (r   rh   c                 T    | j          |                     t          j                  } | S )Nr    )r   r"   r   utc)date_objs    r   convert_to_datetime_awarer|      s'    ##8<#88Or   c                     t          |                                          t          |                                           v S )a  
    Check if a string contains another string, ignoring case.

    Args:
        source: The string to search within
        search: The substring to search for

    Returns:
        bool: True if search is a substring of source (case-insensitive), False otherwise

    Examples:
        >>> str_icontains("Hello World", "WORLD")
        True
        >>> str_icontains("Hello World", "python")
        False
    r/   casefold)sourcesearchs     r   str_icontainsr      s3    " v;;!!S[[%9%9%;%;;;r   c                     t          |                                           t          |                                          k    S )a  
    Check if a string equals another string, ignoring case.

    Args:
        value: The string to compare
        comparand: The string to compare with

    Returns:
        bool: True if value and comparand are equal (case-insensitive), False otherwise

    Examples:
        >>> str_iequals("Hello World", "hello world")
        True
        >>> str_iequals("Hello World", "hello")
        False
    r~   )re   	comparands     r   str_iequalsr      s3    " u::  C	NN$;$;$=$===r   )(loggingr3   rb   collectionsr   dataclassesr   r   r   r   r   decimalr	   typingr
   r   uuidr   r0   dateutil.tzr   r   	getLoggerr=   r   r   r#   r)   rD   r9   r?   r@   r/   rA   r2   rf   rh   r|   r   r   rG   r   r   <module>r      s     				 # # # # # # , , , , , , , , - - - - - - - - - -                             



 & & & & & & & &g	""@ @ @
W W W  "  ! ! !:+ + +    3 8C=    @T    	( 	( 	( 	( 	(k 	( 	( 	(  < < <(> > > > >r   