
    Kif                     .   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rd dlmZ  ed       G d de             Z G d d      Ze G d de             Ze G d de             Zy)    N)ABCabstractmethod)Iterable)suppress)chain)Path)TYPE_CHECKINGAny)definefield)ArgumentCollectionupdate_argument_collection)CycloptsError)to_tuple_converter)AppT)kw_onlyc                       e Zd ZU dZ ede      Zee   e	d<    ed      Z
ee	d<    ed      Zee	d	<    ed
d      Zed
z  e	d<   eedeeef   fd              Zeedefd              Zdddeedf   defdZy
)
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                     t         )z$Return the configuration dictionary.NotImplementedErrorselfs    c/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/cyclopts/config/_common.pyconfigzConfigBase.config!   
     "!    c                     t         HReturn a string identifying the configuration source for error messages.r    r"   s    r$   r   zConfigBase.source'   r&   r'   appr   commands.	argumentsc                 
   | j                   j                         }	 t        | j                  | j                  r|nd      D ]  }||   }	 	 | j                  r|}n't        d |j                  j                  D        |      }|j                         D ci c]  \  }}||vs|| }}}t        || j                  ||j                  j                  d   | j                  | j                         y # t
        $ r Y y w xY wc c}}w )Nr   c              3   :   K   | ]  }|j                   r|  y wN)_meta_parent).0as     r$   	<genexpr>z&ConfigBase.__call__.<locals>.<genexpr>>   s     \QQ^^q\s   )r   r   )r%   copyr   r   r   KeyErrornext	app_stackcurrent_frameitemsr   r   stackr   )	r#   r+   r,   r-   r%   key
filter_appkvs	            r$   __call__zConfigBase.__call__-   s     "&!1!1!3	T^^9R9RXXZ[ %% $$J\#--*E*E\^abJ#)<<>I41aQj5H!Q$II"KKMM#nn,,	
  		 Js   .C0 C?!C?0	C<;C<)__name__
__module____qualname____doc__r   r   r   r   str__annotations__r   boolr   r   propertyr   dictr
   r%   r   tupler   rA   r   r'   r$   r   r      s      %R;MNIx}N.M4.!&t!4$4H=GS4Z="S#X "  " " "  "

 S/
 &	
r'   r   c                   (    e Zd ZdZdeez  fdZd Zy)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                 
   t        |      j                         | _        | j                  j                         r=| j                  j	                         }|j
                  | _        |j                  | _        y d | _        d | _        y r0   )	r   absoluterN   existsstatst_mtime_mtimest_size_size)r#   rN   rR   s      r$   __init__zFileCacheKey.__init__R   sZ    J'')	9999>>#D--DKDJDKDJr'   c                     t        |t        |             sy| j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S )NF)
isinstancetyperT   rV   rN   )r#   others     r$   __eq__zFileCacheKey.__eq__\   sM    %d,{{ell*dtzzU[[/HdTYYZ_ZdZdMddr'   N)rB   rC   rD   rE   rF   r   rW   r\   r   r'   r$   rM   rM   K   s    S4Z er'   rM   c                   R   e Zd ZU dZ ee      Zeez  ed<    edd      Z	e
ed<    edd      Ze
ed<    ed	dd
      Zeeef   d	z  ed<   	  ed	dd
      Zed	z  ed<   	 ededeeef   fd       Zedeeef   fd       Zedefd       Zej,                  dedd	fd       Zy	)ConfigFromFilezlConfiguration source that loads from a file.

    Supports file caching and parent directory searching.
    )r   rN   FT)r   r   
must_existsearch_parentsN)r   initrepr_config_config_cache_keyr   c                     t         )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#   rN   s     r$   _load_configzConfigFromFile._load_configt   s
      "!r'   c                 l   t        | j                  t              sJ | j                  j                         j	                         j                         j                  D ]  }|| j                  j                  z  }|j                         rXt        |      }| j                  |k(  r| j                  xs i c S 	 | j                  |      | _        || _
        | j                  c S | j(                  r| j*                  st-        t.        j0                  t3        j4                  t.        j0                        t7        | j                               | j*                  rOt-        t.        j0                  t3        j4                  t.        j0                        t7        | j                              i | _        | j                  S # t        $ r  t        $ rh}t        t!        |      dd      }t#        t$              5  |j&                  d   }|r|dz  }||z  }d d d        n# 1 sw Y   nxY wt        |      |d }~ww xY w)NrB    r   z: )msg)rY   rN   r   
expanduserresolverP   parentsnamerQ   rM   rd   rc   rf   r   	ExceptiongetattrrZ   r   
IndexErrorargsr`   r_   FileNotFoundErrorerrnoENOENTosstrerrorrF   )r#   parent	candidate	cache_keyeri   exception_msgs          r$   r%   zConfigFromFile.config   s   $))T***ii**,446??AII 	aF/I!(3	))Y6<<-2-8#'#4#4Y#?DL-6D* ||#$$'bkk%,,6OQTUYU^U^Q_``3	a8 ??#ELL"++ell2KSQUQZQZ^\\||- %   8!$q':r:C!*- -()q	4KC},	- - -
 (C0a78s0   6F99H3%H.0H	H.HH..H3c                     | j                   | j                   S t        | j                  t              sJ t	        | j                  j                               S r)   )r   rY   rN   r   rF   rP   r"   s    r$   r   zConfigFromFile.source   sD     <<#<<$))T***499%%'((r'   valuec                     || _         y r0   r   r#   r}   s     r$   r   zConfigFromFile.source   	    r'   )rB   rC   rD   rE   r   r   rN   rF   rG   r_   rH   r`   rc   rJ   r
   rd   rM   r   rf   rI   r%   r   setterr   r'   r$   r^   r^   c   s   
 t,D#*,UD9J9 =ND=%*4e%%PGT#s(^d"PZ-24eRW-X|d*X4" "$sCx. " "" "S#X " "H ) ) ) ]]C D  r'   r^   c                       e Zd ZU dZeeef   ed<   edeeef   fd       Z	edefd       Z
e
j                  deddfd       Z
y)	DictzrConfiguration source from an in-memory dictionary.

    Useful for programmatically generated configurations.
    datar   c                     | j                   S r0   )r   r"   s    r$   r%   zDict.config   s    yyr'   c                 4    | j                   | j                   S y)r*   rJ   r   r"   s    r$   r   zDict.source   s     <<#<<r'   r}   Nc                     || _         y r0   r   r   s     r$   r   zDict.source   r   r'   )rB   rC   rD   rE   rJ   rF   r
   rG   rI   r%   r   r   r   r'   r$   r   r      sy    
 sCx.S#X      ]]C D  r'   r   ) rs   ru   abcr   r   collections.abcr   
contextlibr   	itertoolsr   pathlibr   typingr	   r
   attrsr   r   cyclopts.argumentr   r   cyclopts.exceptionsr   cyclopts.utilsr   cyclopts.corer   r   rM   r^   r   r   r'   r$   <module>r      s     	 # $    %  L - -! 3
 3
 3
le e0 QZ Q Qh :  r'   