o
    if                     @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZmZ d d	lmZmZ d d
lmZ d dlmZ erTd dlmZ eddG dd deZG dd dZeG dd deZeG dd deZdS )    N)ABCabstractmethod)Iterable)suppress)chain)Path)TYPE_CHECKINGAny)definefield)ArgumentCollectionupdate_argument_collection)CycloptsError)to_tuple_converter)AppT)kw_onlyc                   @   s   e Zd ZU dZededZee e	d< eddZ
ee	d< eddZee	d	< ed
ddZed
B e	d< eedeeef fddZeedefddZdddeedf defddZd
S )
ConfigBasezBase class for configuration sources.

    Handles the common logic of processing configuration dictionaries
    and updating ArgumentCollections.
     )default	converter	root_keysF)r   allow_unknownTuse_commands_as_keysNsource)r   alias_sourcereturnc                 C      t )z$Return the configuration dictionary.NotImplementedErrorselfr   r   ]/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/cyclopts/config/_common.pyconfig!      zConfigBase.configc                 C   r   )HReturn a string identifying the configuration source for error messages.r   r    r   r   r"   r   '   r$   zConfigBase.sourceappr   commands.	argumentsc                    s   | j  }zt| j| jr|ndD ]}|| }qW n
 ty#   Y d S w | jr*| ntdd |jjD |  fdd|	 D }t
|| j||jjd | j| jd d S )Nr   c                 s   s    | ]}|j s|V  qd S N)_meta_parent).0ar   r   r"   	<genexpr>>   s    z&ConfigBase.__call__.<locals>.<genexpr>c                    s   i | ]\}}| vr||qS r   r   )r+   kv
filter_appr   r"   
<dictcomp>?   s    z'ConfigBase.__call__.<locals>.<dictcomp>)r   r   )r#   copyr   r   r   KeyErrornext	app_stackcurrent_frameitemsr   r   stackr   )r!   r&   r'   r(   r#   keyr   r0   r"   __call__-   s(   



zConfigBase.__call__)__name__
__module____qualname____doc__r   r   r   r   str__annotations__r   boolr   r   propertyr   dictr	   r#   r   tupler   r<   r   r   r   r"   r      s&   
 
r   c                   @   s*   e Zd ZdZdeeB fddZdd ZdS )FileCacheKeyzAbstraction to quickly check if a file needs to be read again.

    If a newly instantiated ``CacheKey`` doesn't equal a previously instantiated ``CacheKey``,
    then the file needs to be re-read.
    pathc                 C   sF   t | | _| j r| j }|j| _|j| _d S d | _d | _d S r)   )	r   absoluterH   existsstatst_mtime_mtimest_size_size)r!   rH   rK   r   r   r"   __init__R   s   


zFileCacheKey.__init__c                 C   s6   t |t| s	dS | j|jko| j|jko| j|jkS )NF)
isinstancetyperM   rO   rH   )r!   otherr   r   r"   __eq__\   s   $zFileCacheKey.__eq__N)r=   r>   r?   r@   rA   r   rP   rT   r   r   r   r"   rG   K   s    
rG   c                   @   s   e Zd ZU dZeedZeeB ed< edddZ	e
ed< edddZe
ed< ed	ddd
Zeeef d	B ed< 	 ed	ddd
Zed	B ed< 	 ededeeef fddZedeeef fddZedefddZejdedd	fddZd	S )ConfigFromFilezlConfiguration source that loads from a file.

    Supports file caching and parent directory searching.
    )r   rH   FT)r   r   
must_existsearch_parentsN)r   initrepr_config_config_cache_keyr   c                 C   r   )a>  Load the config dictionary from path.

        Do **not** do any downstream caching; ``ConfigFromFile`` handles caching.

        Parameters
        ----------
        path: Path
            Path to the file. Guaranteed to exist.

        Returns
        -------
        dict
            Loaded configuration.
        r   )r!   rH   r   r   r"   _load_configt   s   zConfigFromFile._load_configc                 C   s`  t | jtsJ | j   jD ]}|| jj }| rt	|}| j
|kr.| jp+i   S z| || _|| _
W nD tyA     ty} } z1tt|dd}tt |jd }|r`|d7 }||7 }W d    n1 snw   Y  t|d|d }~ww | j  S | jrq| jrttjttjt| jq| jrttjttjt| ji | _| jS )Nr=    r   z: )msg)rQ   rH   r   
expanduserresolverI   parentsnamerJ   rG   r[   rZ   r\   r   	ExceptiongetattrrR   r   
IndexErrorargsrW   rV   FileNotFoundErrorerrnoENOENTosstrerrorrA   )r!   parent	candidate	cache_keyer^   exception_msgr   r   r"   r#      sB   





zConfigFromFile.configc                 C   s.   | j dur| j S t| jtsJ t| j S )r%   N)r   rQ   rH   r   rA   rI   r    r   r   r"   r      s   
zConfigFromFile.sourcevaluec                 C   
   || _ d S r)   r   r!   rq   r   r   r"   r         
)r=   r>   r?   r@   r   r   rH   rA   rB   rV   rC   rW   rZ   rE   r	   r[   rG   r   r\   rD   r#   r   setterr   r   r   r"   rU   c   s"   
 "$rU   c                   @   sf   e Zd ZU dZeeef ed< edeeef fddZ	edefddZ
e
jdedd	fd
dZ
d	S )DictzrConfiguration source from an in-memory dictionary.

    Useful for programmatically generated configurations.
    datar   c                 C   s   | j S r)   )rx   r    r   r   r"   r#      s   zDict.configc                 C   s   | j dur| j S dS )r%   NrE   rs   r    r   r   r"   r      s   
zDict.sourcerq   Nc                 C   rr   r)   rs   rt   r   r   r"   r      ru   )r=   r>   r?   r@   rE   rA   r	   rB   rD   r#   r   rv   r   r   r   r"   rw      s   
 rw   ) rh   rj   abcr   r   collections.abcr   
contextlibr   	itertoolsr   pathlibr   typingr   r	   attrsr
   r   cyclopts.argumentr   r   cyclopts.exceptionsr   cyclopts.utilsr   cyclopts.corer   r   rG   rU   rw   r   r   r   r"   <module>   s*    6T