
    i7                        S SK r S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	J
r
  S SKJr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKrS SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SK J!r!J"r"  \RF                  S:  a  S SK$r$OS SK%r$S\\   4S jr&S\4S jr' " S S\5      r( " S S\5      r)g)    N)deque)deepcopy)cached_propertyreduce)Path)copy)TOMLDocument)Any)ClassVar)Dict)Optional)Tuple)Type)Union)ValidationError)BaseSettings)PydanticBaseSettingsSource)!PyprojectTomlConfigSettingsSource)SettingsConfigDict)AnacondaConfigTomlSyntaxErrorAnacondaConfigValidationError)      returnc            
          [        [        R                  R                  [        R                  R	                  [        R
                  " SS5      5      5      5      n U R                  5       (       a  U $ S $ )NANACONDA_SECRETS_DIRz/run/secrets)r   ospath
expandvars
expandusergetenvis_dir)r   s    8lib/python3.13/site-packages/anaconda_cli_base/config.pyanaconda_secrets_dirr$   &   sS    
GGryy)?PQ	
D
 ;;==4*d*    c            
          [        [        R                  R                  [        R                  R	                  [        R
                  " SS5      5      5      5      $ )NANACONDA_CONFIG_TOMLz~/.anaconda/config.toml)r   r   r   r   r    r!    r%   r#   anaconda_config_pathr)   /   sC    
GG		02KL	
 r%   c                   h   ^  \ rS rSr% 0 r\\\\\\	4   4      \
S'   S\S\\\	4   4U 4S jjrSrU =r$ ) AnacondaConfigTomlSettingsSource9   _cache	file_pathr   c                   >  U R                   R                  U5      nUc  [        TU ]  U5      nX R                   U'   U$ ! [        R
                   a,  n[        5        SUR                  S    3n[        U5      eS nAff = f)N: r   )	r-   getsuper
_read_filetomllibTOMLDecodeErrorr)   argsr   )selfr.   resultearg	__class__s        r#   r3   +AnacondaConfigTomlSettingsSource._read_file<   s|    	5[[__Y/F~+I6)/I&M&& 	5)+,Bqvvayk:C/44	5s   <A   B 'A;;B r(   )__name__
__module____qualname____firstlineno__r-   r   r   r   strr
   __annotations__r3   __static_attributes____classcell__r;   s   @r#   r+   r+   9   sD    35FHT$S#X./05	5D 	5T#s(^ 	5 	5r%   r+   c                      ^  \ rS rSr SS\\\\4      S\SS4U 4S jjjr	S\SS4U 4S jjr
\S\\   S	\S
\S\S\S\\S4   4S j5       r  SS\S\SS4S jjrSrU =r$ )AnacondaBaseSettingsH   Nplugin_namekwargsr   c                   > SnUc  SnUnO[        U[        5      (       aR  [        S U 5       5      (       d  [        SU S35      eS/UQ7nUSR	                  U5      R                  5       -   S-   nO>[        U[        5      (       a  SU4nX1R                  5        S3-   nO[        SU S35      e[        S	UUS
S[        4[        5       SS9U l
        [        TU ]0  " S0 UD6$ )N	ANACONDA_r(   c              3   B   #    U  H  n[        U[        5      v   M     g 7fN)
isinstancerA   ).0entrys     r#   	<genexpr>9AnacondaBaseSettings.__init_subclass__.<locals>.<genexpr>S   s     G;%z%--;s   zplugin_name=z# error: All values must be strings.plugin_z4 is not supported. It must be either a str or tuple.z.env__ignoreT)env_filepyproject_toml_table_header
env_prefixenv_nested_delimiterextraignored_typessecrets_dirvalidate_assignment)rO   tupleall
ValueErrorjoinupperrA   r   r   r$   model_configr2   __init_subclass__)clsrI   rJ   base_env_prefixrY   rZ   r;   s         r#   rf   &AnacondaBaseSettings.__init_subclass__I   s     + *,'(JU++G;GGG ";-/RS  ,4*Bk*B'(388K+@+F+F+HH3NJS))+3[*A'(.?.?.A-B!+DDJ{m+_`  .(C!!%*,,. $	
 w(2622r%   c                 &  >  [         TU ]  " S0 UD6  g ! [         Gaq  n/ nUR                  5        GH6  nUS   nUS   nU R                  R                  SS5      nU R                  R                  SS5      =(       d    SnXxR                  S US    5       5      -   n	US   S   n
X;   a-  U[        U
5         nS	UR                   S
US   S    SU SU 3nOU	[        R                  ;   a  SU	 SU SU 3nO_SR                  U R                  R                  S/ 5      5      nSR                  S US    5       5      nS[        5        SU SU SU SU 3
nUR                  U5        GM9     SSR                  U5      -   n[        U5      eS nAff = f)NinputmsgrZ    r[   c              3   T   #    U  H  n[        U5      R                  5       v   M      g 7frN   )rA   rd   rP   locs     r#   rR   0AnacondaBaseSettings.__init__.<locals>.<genexpr>y   s!      60<CHNN$$s   &(rp   r   z- Error in init kwarg (=z)
    z - Error in environment variable z
    .rY   c              3   8   #    U  H  n[        U5      v   M     g 7frN   )rA   ro   s     r#   rR   rq      s     "D|3s88|s   z- Error in z in [z] for z = 
r(   )r2   __init__r   errorsre   r1   rc   rA   titler   environr)   appendr   )r7   rJ   r9   rx   errorinput_valuerl   rZ   	delimiterenv_varkwargvaluetable_headerkeymessager;   s                  r#   rw   AnacondaBaseSettings.__init__n   s   	9G&v& 	9F#GnEl!..22<D
 --112H"MSQS	$~~ 605e6 (  eQ?"3u:.E2177)1U5\!_<MQugU\]`\abC

*<WIQ{mSYZ]Y^_C#&88))--.KRP$L (("DuU|"DDC'(<(>'?u\NRXY\X]]`al`mmstwsxyCc"/ $2 TYYv..G/88;	9s    FE,FFsettings_clsinit_settingsenv_settingsdotenv_settingsfile_secret_settings.c           	      4    UUUU[        U[        5       5      4$ rN   )r+   r)   )rg   r   r   r   r   r   s         r#   settings_customise_sources/AnacondaBaseSettings.settings_customise_sources   s(      ,\;O;QR
 	
r%   preserve_existing_keysdry_runc                 >
   U R                  SSSSS9n[        5       nUR                  5       (       a  SSKJn  UR	                  5       R                  S5      nUR                  SU S35      n [        XG5         [        UR                  R                  S5      S SS9n	U	SS  H  n
U
R                  5         M      [        US5       n[        R                   " U5      nSSS5        O0 UR                  R'                  SSS9  [        R(                  " 5       n[+        W5      nU R,                  R/                  S[1        5       5      nU(       a.  S[        R(                  S[2        S[2        4S jn[5        XU5      nOUn S2S[        R(                  S[6        [8        [2        4   S[6        [8        [2        4   S[:        SS4
S  jjnU R                  5       nU" UUUUS!9  U(       GaY  SSKnSSKnSS"KJ n  SS#K!J"n  URG                  5       nURG                  5       nS$n[        5       nUR                  5       (       aC  UR                  RI                  URK                  5       RL                  5      R                  U5      nOS%nUR                  R	                  5       R                  U5      nURO                  URQ                  S5      URQ                  S5      [9        U5      U[9        U5      US%S&9nS'RS                  U5      nU(       d  URU                  S([        5        S)35        gU" US*SSS+9nURU                  U5        g[V        RX                  " UR                  S,S-SS.9u  nn  [        RZ                  " U5      n![\        R^                  " S/S'U![\        R`                  S09n![b        Rd                  " US15       nURg                  U!5        SSS5        [b        Rh                  " U U5        [j        Rl                  Ro                  5         g! [        [        4 a  n[        S	U S
U SU 35      UeSnAff = f! [        [        4 a     GNf = f! , (       d  f       GN@= f! [        [        4 a  n[        SU SU 35      UeSnAf["         a  n[%        SU SU 35      UeSnAff = f! [        [        4 a!  n[        SUR                   SU 35      UeSnAff = f! , (       d  f       GN= f! ["         a)     [b        R                  " U 5        e ! [         a     e f = ff = f)3a|  
Write the current configuration to the Anaconda config.toml file.

This method writes the configuration instance to the config.toml file,
preserving existing comments and formatting. Only non-default and non-None
values are written. If a value is set to its default, the corresponding
entry is removed from the config file.

The write operation is atomic - the config file is only updated if the
entire write succeeds, preventing corruption from interrupted writes.

Args:
    preserve_existing_keys: If True (default) updates to existing keys in the
        config.toml file, will not remove the key if set to the default
        value. If False fields set to default value are removed from the file
    dry_run: If True, displays a diff of proposed changes without writing
        to the file. If False (default), writes changes to config.toml.

Raises:
    ValidationError: If any attribute has been manually set to an invalid
        value that fails pydantic validation.
    OSError: If backup creation fails, config file cannot be read, or
        config directory cannot be created due to permissions or I/O errors.
    ValueError: If the existing config.toml contains invalid TOML syntax.

Behavior:
    - Creates ~/.anaconda/config.toml if it doesn't exist
    - Creates timestamped backup (e.g., config.backup.20231218_143022.toml)
    - Keeps last 5 backups, automatically deletes older ones
    - Preserves comments and formatting in existing config
    - Only writes non-default, non-None values
    - Removes keys when values are set to their defaults
    - Validates all values before writing
    - Uses atomic write to prevent file corruption
FT)exclude_unsetexclude_defaultsexclude_noneexclude_computed_fieldsr   )datetimez%Y%m%d_%H%M%S_%fzconfig.backup.z.tomlzFailed to create backup of z at r0   Nzconfig.backup.*.tomlc                 6    U R                  5       R                  $ rN   )statst_mtime)ps    r#   <lambda>3AnacondaBaseSettings.write_config.<locals>.<lambda>   s    !&&("3"3r%   )r   reverse   rtzFailed to read zFailed to parse zD as TOML. The file may be corrupted or contain invalid TOML syntax: )parentsexist_okzFailed to create directory rY   abr   c                 ^    X;  a%  U R                  U[        R                  " 5       5        X   $ rN   )addtomlkittable)r   r   s     r#   nestitem3AnacondaBaseSettings.write_config.<locals>.nestitem  s"    :EE!W]]_-tr%   orignew
full_modelr   c                    [         [        [        [        [        [
        4   [        [        [
        4   4      " XU4/5      nU(       Ga   UR                  5       u  pVnUR                  5       UR                  5       -
  S1-
  nU H'  n	UR                  U	S 5      n
U
b  U(       a  XU	'   M%  XY	 M)     UR                  5        H}  u  p[        U[        5      (       a_  X;  a%  UR                  U	[        R                  " 5       5        UR                  U	5      XR                  U	5      4nUR                  U5        My  XU	'   M     U(       a  M  g g )NrT   )r   r   r	   r   rA   r
   popleftkeysr1   itemsrO   dictr   r   r   r{   )r   r   r   r   stackcurrent_originalcurrent_updatecurrent_fullremoved_keyskr   v	to_appends                r#   	deepmerge4AnacondaBaseSettings.write_config.<locals>.deepmerge  s+    %d38nd38n LMNZ()E AF> , %))+n.A.A.CCxjP  &A ),,Q5E)/E.3+,/ & +002DA!!T**4,00GMMOD%5%9%9!%<aAQAQRSAT$U	Y/./+ 3# %r%   )r   )Syntax)consolez%m-%d-%y %H:%Mrm   )fromfilefromfiledatetofile
tofiledatelinetermrv   z[bold green]No change to z[/bold green]diff)codelexerline_numbers	word_wrapz.config_z	.toml.tmp)dirprefixsuffixtextz\n+$)flagswt)T)8
model_dumpr)   existsr   nowstrftime	with_namer   OSErrorIOErrorsortedparentglobunlinkopenr   load	Exceptionrb   mkdirr	   r   re   r1   r`   r
   r   r   rA   booldifflibrich.syntaxr   anaconda_cli_base.consoler   	as_stringfromtimestampr   r   unified_diff
splitlinesrc   printtempfilemkstempdumpsresubDOTALLr   fdopenwritereplacer+   r-   clear)"r7   r   r   valuesconfig_tomlr   	timestampbackup_pathr9   backups
old_backupfconfig	to_updater   r   r   r   	full_dumpr   dtr   r   originalupdated	dt_formatmodifiedr   diffsr   syntaxtmp_fdtmp_pathconfig_dumps"                                     r#   write_config!AnacondaBaseSettings.write_config   s   P !$(	 ! 
 +,) //0BCI%//.50QRK[. &&++,BC3  #*!"+J%%' #.	+t,$\\!_F -,""(((E
 ))+FV$	((,,-JEGTG00 S S 
 HI>FF ,0	!	0&&!	0c3h!	0 S#X!	0 %)	!	0
 !	0F OO%	FI>T	
 !*9'')H))+G(I.0K!!##;;44$$&//(9%  ++//#,,Y7C((##E*""5)[)%;' ) E 99U#D/0D0F/G}U V%SWXFMM&! $++""	
	!--	2K&&$299MK64(A$ ) JJx- -3399;o W% 1+d;-rRSQTU W% 
 -,W% K}BqcBCJ  &{m 4QQRPSU  W% 1+2D2D1ERsK` )(  			(#   		s   -P 9AP1 ;Q QQ (R# "AS) 4S=S) P.P))P.1QQ
QQ Q R *Q<<R 	RR #S3SS
S&!S) )
T4T
T
TTTTr(   rN   )TF)r=   r>   r?   r@   r   r   rA   r`   r
   rf   rw   classmethodr   r   r   r   r   r   r  rC   rD   rE   s   @r#   rG   rG   H   s    8<#3"5e#45#3HK#3	#3 #3J 9  9  9D 
<(
 2
 1	

 4
 9
 
)3.	/
 
$ (,V $V V 
	V Vr%   rG   )*r   r   sysr   collectionsr   r   r   	functoolsr   r   pathlibr   shutiltomlkit.toml_documentr	   typingr
   r   r   r   r   r   r   r   pydanticr   pydantic_settingsr   r   r   r   anaconda_cli_base.exceptionsr   r   version_infor4   tomlir$   r)   r+   rG   r(   r%   r#   <module>r     s    	 	 
    -   .         $ * 8 ? 0
 w+htn +d 5'H 5o< or%   