o
    Dg                     @   s  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 d dlZd dlmZmZ e dZd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZdefddZG dd deZ d d! Z!d"d# Z"d$d% Z#dS )&    N)defaultdict)asdictis_dataclass)datedatetimetimezone)Decimal)UUID)tzlocaltzutcposthogc                 C   s   | j du p| j | du S )z1Determines if a given datetime.datetime is naive.N)tzinfo	utcoffset)dt r   g/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/posthog/utils.pyis_naive   s   r   c                 C   s"   | j | j| jd d  d  d S )z2Determines total seconds with python < 2.7 compat.   i  g    .A)microsecondssecondsdays)deltar   r   r   total_seconds   s   "r   c                 C   s@   t | rt |  }t|dk r| jt dS | jt dS | S )z:Attempts to convert a naive datetime to an aware datetime.   r   )r   r   nowr   replacer
   r   )r   r   r   r   r   guess_timezone   s   r   c                 C   s   |  dr| d d S | S )N/)endswith)hostr   r   r   remove_trailing_slash,   s   
r"   c              
   C   s  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 zt| dr?t| jr?|  } nt| drMt| jrM|  } W n tyh } ztd|  W Y d }~nd }~ww t | trrt| S t| rt | tst| S t| 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er   r   r   clean2   s0   



r<   c                 C   s   dd | D S )Nc                 S   s   g | ]}t |qS r   )r<   ).0r:   r   r   r   
<listcomp>N   s    z_clean_list.<locals>.<listcomp>r   )list_r   r   r   r1   M   s   r1   c                 C   sR   i }t | D ]\}}zt|||< W q ty&   td||t| Y qw |S )NzXDictionary values must be serializeable to JSON "%s" value %s of type %s is unsupported.)r(   	iteritemsr<   r4   r5   warningr-   )dict_datakvr   r   r   r7   Q   s   r7   c                 C   s   t | }t|}|S N)r   r7   )
dataclass_rC   r   r   r   r8   `   s   r8   c              
   C   s\   z	|  dd}W |S  ty- } zd|}| dd td| W Y d }~d S d }~ww )Nzutf-8strict:zError decoding: %s)decodeAttributeErrorjoinr5   rA   )cmplxr:   	exceptionr   r   r   r9   f   s   
r9   returnc                 C   s(   zt |  W dS  t jy   Y dS w )NTF)recompileerror)valuer   r   r   is_valid_regexq   s   
rT   c                       s(   e Zd Z fddZ fddZ  ZS )SizeLimitedDictc                    s   t  j|i | || _d S rF   )super__init__max_size)selfrX   argskwargs	__class__r   r   rW   z   s   
zSizeLimitedDict.__init__c                    s(   t | | jkr|   t || d S rF   )lenrX   clearrV   __setitem__)rY   keyrS   r\   r   r   r`   ~   s   zSizeLimitedDict.__setitem__)__name__
__module____qualname__rW   r`   __classcell__r   r   r\   r   rU   y   s    rU   c                 C   s   | j d u r| jtjd} | S )Nr   )r   r   r   utc)date_objr   r   r   convert_to_datetime_aware   s   
rh   c                 C   s   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searchr   r   r   str_icontains      rm   c                 C   s   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
    ri   )rS   	comparandr   r   r   str_iequals   rn   rp   )$loggingr+   rP   collectionsr   dataclassesr   r   r   r   r   decimalr   uuidr	   r(   dateutil.tzr
   r   	getLoggerr5   r   r   r   r"   r<   r1   r7   r8   r9   r*   rT   rU   rh   rm   rp   r   r   r   r   <module>   s2    
