o
    i                     @  s   U d dl mZ d dlZd dlZd dlZd dlmZmZ d dl	m	Z	 d dl
Z
ddlmZ dZedZd	Zdad
ed< ddddZddddZeG dd dZdS )    )annotationsN)	dataclassfield)time   )data_snapshot)DEFAULT_UPDATE_URLUpdatePriceswait_prices_updated_syncwait_prices_updated_asynczgenai-priceszXhttps://raw.githubusercontent.com/pydantic/genai-prices/refs/heads/main/prices/data.jsonzUpdatePrices | None_global_update_pricestimeoutfloat | Nonereturnboolc                 C  s   t rt | S dS )zSynchronously wait for prices to be updated.

    Args:
        timeout: The maximum time to wait for prices to be updated. Defaults to None which waits indefinitely.

    Returns:
        True if prices were updated, False otherwise.
    F)r   waitr    r   `/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/genai_prices/update_prices.pyr
      s   	
r
   c                   s   t t| I dH S )zAsynchronously wait for prices to be updated.

    Args:
        timeout: The maximum time to wait for prices to be updated. Defaults to None which waits indefinitely.

    Returns:
        True if prices were updated, False otherwise.
    N)asyncio	to_threadr
   r   r   r   r   r   '   s   	r   c                   @  s   e Zd ZU dZdZded< 	 eZded< 	 edd d	Z	d
ed< 	 ee
jd	Zded< ee
jd	Zded< edddZded< edddZded< ddd1ddZd2d3dd Zd!d" Zd#d$ Zd4d'd(Zd5d*d+Zd,d- Zd6d/d0ZdS )7r	   zUpdate prices in the background using a daemon thread.

    Can be used either as a context manager or as a simple class, where you'll need to call start() and stop() manually.
    i  floatupdate_intervalstrurlc                   C  s   t jdddS )N
      )r   connect)httpxTimeoutr   r   r   r   <lambda>>   s    zUpdatePrices.<lambda>)default_factoryzhttpx.Timeoutrequest_timeoutzthreading.Event_stop_event_prices_updatedNF)defaultinitzthreading.Thread | None_threadzException | None_background_exc)r   r   bool | floatc                C  s   | j dur	tdtdurtd| a| j  | j  d| _tj| j	ddd| _ | j 
  |r?| j|du r9dn|d dS dS )	zStart the background task.

        Args:
            wait: Whether to wait for the prices to be updated before returning, if an int is passed
                wait for that many seconds, if `True` wait for 30 seconds.
        Nz,UpdatePrices background task already startedzWUpdatePrices global task already started, only one UpdatePrices can be active at a timeTzgenai_prices:update)targetdaemonname   r   )r'   RuntimeErrorr   r$   clearr#   r(   	threadingThread_background_taskstartr   )selfr   r   r   r   r3   E   s   
	


zUpdatePrices.startr   r   r   r   c                 C  s(   | j j|d}| jr| j}d| _||S )zWait for the prices to be updated in the background task.

        Args:
            timeout: The maximum time to wait for the prices to be updated in seconds.
        r   N)r$   r   r(   )r4   r   prices_updatedexcr   r   r   r   _   s   zUpdatePrices.waitc                 C  sL   da td | jdur| j  | j  d| _| jr$| j}d| _|dS )zStop the background task.N)r   r   set_custom_snapshotr'   r#   setjoinr(   )r4   r6   r   r   r   stopl   s   



zUpdatePrices.stopc                 C  s   |    | S N)r3   )r4   r   r   r   	__enter__{   s   zUpdatePrices.__enter___argsobjectc                 G  s   |    d S r;   )r:   )r4   r=   r   r   r   __exit__   s   zUpdatePrices.__exit__Nonec              
   C  s   t d z	 |   | j  | j| jrnqW n% ty? } z|| _	| j  t 
dt|j| W Y d }~d S d }~ww t d d S )Nz%Starting genai-prices background taskTz6Error updating genai-prices in the background (%s): %sz$genai-prices background task stopped)loggerinfo_update_pricesr$   r8   r#   r   r   	Exceptionr(   errortype__name__)r4   er   r   r   r2      s    


"zUpdatePrices._background_taskc                 C  sL   t  }|  }t  | }|rtdt|j| ntd| t| d S )Nz1Successfully fetched %d providers in %.2f secondsz2Successfully fetched null snapshot in %.2f seconds)r   fetchrA   rB   len	providersr   r7   )r4   r3   snapshotintervalr   r   r   rC      s   
zUpdatePrices._update_prices!data_snapshot.DataSnapshot | Nonec                 C  s>   ddl m} tj| j| jd}|  tj|j	
|jddS )z9Fetches the latest provider data from the configured URL.r   )datar   T)from_auto_update) rO   r   getr   r"   raise_for_statusr   DataSnapshotproviders_schemavalidate_jsoncontent)r4   rO   rr   r   r   rI      s   zUpdatePrices.fetch)r   r)   r;   r   r   r   r   )r=   r>   )r   r@   )r   rN   )rG   
__module____qualname____doc__r   __annotations__r   r   r   r"   r0   Eventr#   r$   r'   r(   r3   r   r:   r<   r?   r2   rC   rI   r   r   r   r   r	   3   s(   
 

r	   r;   rY   )
__future__r   _annotationsr   loggingr0   dataclassesr   r   r   r   rQ   r   __all__	getLoggerrA   r   r   r]   r
   r   r	   r   r   r   r   <module>   s     
