
    g                      `    d dl mZ  G d de      Z G d d      Z	 	 	 	 ddededed	ed
ef
dZy)    )Enumc                   $    e Zd ZdZdZdZdZdZdZy)	CacheModea@  
    Defines the caching behavior for web crawling operations.
    
    Modes:
    - ENABLED: Normal caching behavior (read and write)
    - DISABLED: No caching at all
    - READ_ONLY: Only read from cache, don't write
    - WRITE_ONLY: Only write to cache, don't read
    - BYPASS: Bypass cache for this operation
    enableddisabled	read_only
write_onlybypassN)	__name__
__module____qualname____doc__ENABLEDDISABLED	READ_ONLY
WRITE_ONLYBYPASS     K/var/www/openai/venv/lib/python3.12/site-packages/crawl4ai/cache_context.pyr   r      s"    	 GHIJFr   r   c                   T    e Zd ZdZddededefdZdefdZdefdZ	e
defd	       Zy
)CacheContexta2  
    Encapsulates cache-related decisions and URL handling.
    
    This class centralizes all cache-related logic and URL type checking,
    making the caching behavior more predictable and maintainable.
    
    Attributes:
        url (str): The URL being processed.
        cache_mode (CacheMode): The cache mode for the current operation.
        always_bypass (bool): If True, bypasses caching for this operation.
        is_cacheable (bool): True if the URL is cacheable, False otherwise.
        is_web_url (bool): True if the URL is a web URL, False otherwise.
        is_local_file (bool): True if the URL is a local file, False otherwise.
        is_raw_html (bool): True if the URL is raw HTML, False otherwise.
        _url_display (str): The display name for the URL (web, local file, or raw HTML).
    url
cache_modealways_bypassc                    || _         || _        || _        |j                  d      | _        |j                  d      | _        |j                  d      | _        |j                  d      | _        | j                  s|| _        yd| _        y)a9  
        Initializes the CacheContext with the provided URL and cache mode.
        
        Args:
            url (str): The URL being processed.
            cache_mode (CacheMode): The cache mode for the current operation.
            always_bypass (bool): If True, bypasses caching for this operation.
        )http://https://file://)r   r   r   zraw:zRaw HTMLN)	r   r   r   
startswithis_cacheable
is_web_urlis_local_fileis_raw_html_url_display)selfr   r   r   s       r   __init__zCacheContext.__init__'   sv     $*NN+MN..)@A ^^I6>>&1'+'7'7CZr   returnc                     | j                   s| j                  sy| j                  t        j                  t        j
                  fv S )aK  
        Determines if cache should be read based on context.
        
        How it works:
        1. If always_bypass is True or is_cacheable is False, return False.
        2. If cache_mode is ENABLED or READ_ONLY, return True.
        
        Returns:
            bool: True if cache should be read, False otherwise.
        F)r   r!   r   r   r   r   r&   s    r   should_readzCacheContext.should_read9   s8     T%6%69#4#4i6I6I"JJJr   c                     | j                   s| j                  sy| j                  t        j                  t        j
                  fv S )aR  
        Determines if cache should be written based on context.
        
        How it works:
        1. If always_bypass is True or is_cacheable is False, return False.
        2. If cache_mode is ENABLED or WRITE_ONLY, return True.
        
        Returns:
            bool: True if cache should be written, False otherwise.
        F)r   r!   r   r   r   r   r*   s    r   should_writezCacheContext.should_writeH   s8     T%6%69#4#4i6J6J"KKKr   c                     | j                   S )z"Returns the URL in display format.)r%   r*   s    r   display_urlzCacheContext.display_urlW   s        r   N)F)r   r   r   r   strr   boolr'   r+   r-   propertyr/   r   r   r   r   r      s\     HC HY Ht H$KT KLd L !S ! !r   r   disable_cachebypass_cacheno_cache_readno_cache_writer(   c                     | rt         j                  S |rt         j                  S |r|rt         j                  S |rt         j                  S |rt         j                  S t         j
                  S )z
    Converts legacy cache parameters to the new CacheMode enum.
    
    This is an internal function to help transition from the old boolean flags
    to the new CacheMode system.
    )r   r   r   r   r   r   )r3   r4   r5   r6   s       r   _legacy_to_cache_moder8   ]   s]     !!!!!!###"""r   N)FFFF)enumr   r   r   r1   r8   r   r   r   <module>r:      sb     $D! D!P   	  	
 r   