o
    i                     @   s   d dl Z d dlmZmZ d dl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mZmZ edd	G d
d dZdeeef defddZdedeeef fddZdS )    N)	dataclassfield)datetime)AnySupportsFloatcast)Self)DeserializationErrorSerializationError)nownow_plustry_parse_datetime_strT)kw_onlyc                
   @   s   e Zd ZU dZeeef ed< eddZ	e
dB ed< eddZedB ed< eddZe
dB ed< eddZedB ed< dd
dZed	efddZdddZddededed	efddZeddedededB d	efddZdS )ManagedEntryaH  A managed cache entry containing value data and TTL metadata.

    The entry supports either TTL seconds or absolute expiration datetime. On init:
    - If `ttl` is provided but `expires_at` is not, an `expires_at` will be computed.
    - If `expires_at` is provided but `ttl` is not, a live TTL will be computed on access.
    valueN)default
created_atttl
expires_at_dumped_jsonreturnc                 C   sN   | j d ur| jd u rt| j d| _d S | jd ur#| j d u r%|   d S d S d S )N)seconds)r   r   r   recalculate_ttlself r   j/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/key_value/shared/utils/managed_entry.py__post_init__   s
   zManagedEntry.__post_init__c                 C   s   | j d u rdS | j t kS )NF)r   r   r   r   r   r   
is_expired$   s   
zManagedEntry.is_expiredc                 C   s2   | j d ur| jd u r| j t   | _d S d S d S N)r   r   r   total_secondsr   r   r   r   r   *   s   zManagedEntry.recalculate_ttlTinclude_metadatainclude_expirationinclude_creationc                 C   sT   i }|r"| j |d< |r| jr| j |d< |r!| jr!| j |d< n| j }t|dS )Nr   r   r   )obj)r   r   	isoformatr   dump_to_json)r   r!   r"   r#   datar   r   r   to_json.   s   



zManagedEntry.to_jsonjson_strincludes_metadatac           	      C   sx   t |d}|s| |dS t|dd}t|dd}|d}|d u r+d}t|| |||r7t||dS d |dS )N)r)   )r   r   r   r   zValue is None)r   r   r   r   )load_from_jsonr   getr	   float)	clsr)   r*   r   r'   r   r   r   msgr   r   r   	from_json<   s(   


zManagedEntry.from_json)r   N)TTT)TN)__name__
__module____qualname____doc__dictstrr   __annotations__r   r   r   r   r-   r   r   r   propertyboolr   r   r(   classmethodr   r   r0   r   r   r   r   r      s   
 

&r   r$   r   c              
   C   s@   zt | W S  t jtfy } z
d| }t||d }~ww )Nz$Failed to serialize object to JSON: )jsondumpsJSONDecodeError	TypeErrorr
   )r$   er/   r   r   r   r&   V   s   

r&   r)   c              
   C   s   zt | }W n t jtfy  } z
d| }t||d }~ww t|ts,d}t|tdd |D s;d}t|td|dS )Nz#Failed to deserialize JSON string: z'Deserialized object is not a dictionaryc                 s   s    | ]}t |tV  qd S r   )
isinstancer6   ).0keyr   r   r   	<genexpr>j   s    z!load_from_json.<locals>.<genexpr>z,Deserialized object contains non-string keyszdict[str, Any])typval)	r;   loadsr=   r>   r	   r@   r5   allr   )r)   deserialized_objr?   r/   r   r   r   r+   ^   s   


r+   )r;   dataclassesr   r   r   typingr   r   r   typing_extensionsr   key_value.shared.errorsr	   r
   #key_value.shared.utils.time_to_liver   r   r   r   r5   r6   r&   r+   r   r   r   r   <module>   s    I