
    ce)                        d Z ddlZddlmZ ddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlmZmZ ddl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 dd
lmZ ddlmZmZ ddlm Z m!Z!m"Z"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. ddl/m0Z0 ddl1m2Z2 ddl3m4Z5 ej6        rddlm7Z7  G d de          Z8da9d Z:dS )z=API for using the api (anaconda-client, downloads and conda).    N)OrderedDict)QObjectSignal)external_apps)	ClientAPI)CondaAPI)DownloadAPI)WorkerManager)TeamEditionAPI)is_internet_availablesplit_canonical_name)CONFLAUNCH_SCRIPTS_PATHMETADATA_PATHWINAnacondaBrand)contentimages)	constantsget_domain_from_api_url)	url_utils)logger)path_is_writable)is_binary_string   )types)ProcessWorkerc            
       ~    e Zd ZdZ ee          Z ee          Z eee          Z ee          Z	 e            Z
 e            Z fdZd Zed             Zd<dZd Zd Zd	 Zed
             Zd<dZd<dZd<dZd<dZd<dZdedej        ddfdZed             Zed             Zd=dZ 	 d<dej!        e"j#        df         dej$        e         dej%        edf         fdZ&d Z'd<dZ(	 	 d>d!Z)d" Z*d# Z+d?ded$ed%e,ddfd&Z-d' Z.d@d(Z/	 	 	 	 dAd)Z0d?d*Z1ed+             Z2edBd,            Z3	 	 	 dBd-ej$        e         d.ej$        e         d/ej$        e         dej$        e         fd0Z4	 	 dCd1ej$        e         d2ej$        e         ddfd3Z5d<d1ej$        e         ddfd4Z6dCd5Z7d6 Z8d7 Z9d8 Z:d9 Z;d: Z<dDd;Z= xZ>S )E_AnacondaAPIz
    Anaconda Manager API.

    This class contains all methods from the different apis and acts as a controller for the main actions Navigator
    needs to execute.
    c                 d   t                                                       t                      | _        t	                      | _        t                      | _        t                      | _	        | j        j
        | _
        | j        j        | _        i | _        d| _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | j        j        | j        j        | _         fd| _!        | j        j"        | _#        | j        j$        | _%        | j        j&        | _'        | j        j(        | _)        | j        j*        | _+        | j        j,        | _-        | j        j.        | _/        | j        j0        | _1        | j        j2        | _3        | j        j4        | _5        | j        j6        | _7        | j        j8        | _9        | j        j:        | _;        | j        j<        | _=        | j        j        | _>        | j        j?        | _@        | j        jA        | _B        | j        jC        | _D        | j        jE        | _F        | j        jG        | _H        dS )z$Anaconda Manager API process worker.Nc                  *                             S N )_get_api_info_get_api_urls   Clib/python3.11/site-packages/anaconda_navigator/api/anaconda_api.py<lambda>z'_AnacondaAPI.__init__.<locals>.<lambda>Z   s    ]]<<>>-J-J     )Isuper__init__r   
_conda_apir   _client_apir	   _download_apir
   _process_apiROOT_PREFIXCONDA_PREFIX	_metadata_data_directorydependenciesconda_dependenciesremoveconda_removeterminate_all_processesconda_terminate
config_addconda_config_add
config_setconda_config_setconfig_removeconda_config_removedownloadis_valid_urldownload_is_valid_urlget_api_infoget_api_urlis_valid_api_urldownload_is_valid_api_urldownload_get_api_infois_valid_channeldownload_is_valid_channel	terminatedownload_terminate
clear_lockconda_clear_lockenvironment_existsconda_environment_existsget_envsconda_get_envslinkedconda_linkedlinked_apps_infoconda_linked_apps_infoget_prefix_envnameconda_get_prefix_envnamepackage_versionconda_package_versionget_platformconda_platformload_proxy_configconda_load_proxy_configloginclient_loginlogoutclient_logoutuserclient_userclient_get_api_urlset_api_urlclient_set_api_urlget_sslclient_get_sslset_sslclient_set_ssldomainclient_domainreload_clientclient_reload)selfr$   r%   	__class__s    @@r&   r*   z_AnacondaAPI.__init__?   s1    #**$;;(]])OO?6 O8  $"&/"> O2#F $ : $ :#'?#@ *3%)%7%D"*7'3)-);)L&%J%J%J%J%J")-);)L&"&"4"> !% :(,(J%"o6 O2&*o&F#(,(J%%)_%D""o:'+'H$ !,2!-4+0"&"2">"&"2">".6".6!-4!-;r(   c                     || _         dS )z+Set the directory where metadata is stored.N)r2   )rn   data_directorys     r&   set_data_directoryz_AnacondaAPI.set_data_directoryv   s    -r(   c                       t                       S r"   )r   r#   r(   r&   
is_offlinez_AnacondaAPI.is_offline|   s    (****r(   Nc                 j    t          j        d|           | j                            ||dd|          S )zq
        Login to anaconda cloud via the anaconda-client API.

        This method does not use workers.
        z!Login attempt with username `%s`.zAnaconda Navigator )
verify_ssl)r   debugr,   r]   )rn   usernamepasswordrw   s       r&   r]   z_AnacondaAPI.login   s<     	8(CCC%%h:NPR_i%jjjr(   c                 \    t          j        d           | j                                        S )zt
        Logout from anaconda cloud via the anaconda-client API.

        This method does not use workers.
        zLogout was requested!)r   rx   r,   r_   rn   s    r&   r_   z_AnacondaAPI.logout   s*     	,---&&(((r(   c                 N    t          | j                                                  S )zCheck if an user is logged in.)boolr,   ra   r|   s    r&   is_logged_inz_AnacondaAPI.is_logged_in   s    D$))++,,,r(   c                       j                                          fd}d  j                                        }|j                            |           |S )z-Get all the api urls for the current api url.c                 
   | }|                     di           }|                     dd          }t          j        d           j                            ||          } || _        | j                                       d S )Nproxy_servers
ssl_verifyTzRequesting api info for `%s`.)r   verify)getr   rx   r,   rB   base_workersig_finishedconnect)	workeroutputerrorr   r   r   	_api_infoapi_urlrn   s	         r&   _configz&_AnacondaAPI.api_urls.<locals>._config   s     K"JJ;;MZZd33FL8'BBB%22+ 3  F
 "-F''	22222r(   c                 L    | j         }|j                            |||           d S r"   r   sig_chain_finishedemitr   r   r   r   s       r&   r   z(_AnacondaAPI.api_urls.<locals>._api_info   s+     ,K*//VUKKKKKr(   )r,   get_api_info_urlr+   config_showr   r   )rn   r   r   r   r   s   `  @@r&   api_urlsz_AnacondaAPI.api_urls   s    "3355	3 	3 	3 	3 	3 	3 	3	L 	L 	L ,,..##G,,,r(   c                    d}d}d}t                      }|D ]}|D ]l}||v rf|                    |          \  }}	||z   }|	                    d          }
|
d         }d                    dg|
dd         z             }||||z   |z   <   m|D ]~}||v rx|                    |          \  }}	|	                    d          }
|
d         }d                    d	|
d         f          }d                    |d
f          }| d| d| }|||<   g }| D ]T}|D ]:}||v r4|                                D ]\  }}||v r|                    ||          } ;|                    |           U|S )a  
        Fix channels with tokens so that we can correctly process conda cache.

        From this:
            - 'https://conda.anaconda.org/t/<TOKEN>/repo/goanpeca/<SUBDIR>'
        to this:
            - 'https://conda.anaconda.org/t/<ACTUAL-VALUE>/repo/goanpeca/<SUBDIR>'

        And from this:
            - 'https://conda.anaconda.org/repo/t/<TOKEN>/goanpeca/<SUBDIR>'
        to this:
            - 'https://conda.anaconda.org/t/<ACTUAL-VALUE>/repo/goanpeca/<SUBDIR>'
        )zt/z/t/)z/repo/t/z<TOKEN>/r   rv   r   Nrepot)r   splitjoinitemsreplaceappend)channelsunsafe_channelsTOKEN_START_MARKSTOKEN_START_MARKS_REPOTOKEN_VALUE_MARKtoken_channelschtoken_start_markstarttoken_plus_user_and_systempartstokenendconcat_channelnew_channelsuchs                   r&   _process_unsafe_channelsz%_AnacondaAPI._process_unsafe_channels   s    *!/$$! 	; 	;B$5 K K #r))8:AQ8R8R5E5!$44E6<<SAAE!!HE((B4%)#344CEJN5+;#;c#AB$: ; ; #r))8:AQ8R8R5E56<<SAAE!!HE((FE!H#566CHHeS\22E(-%H%H0@%H%H3%H%HN5:N>2;  	$ 	$B$5 E E #r))&4&:&:&<&< E E
U"99!#,<e!D!DB####r(   c                       j         s                                   fd} fd fdd  j                            d          }|j                            |           |S )z
        Return all the conda data needed to make the application work.

        If prefix is None, the root prefix is used.
        c                     j                                       }|j                                       |d         |_        | |_        dS )rv   prefixr   N)r+   infor   r   r   r   )r   r   r   
new_worker_conda_info_processedr   rn   s       r&   _load_unsafe_channelsz6_AnacondaAPI.conda_data.<locals>._load_unsafe_channels   sN    --V-<<J#++,ABBB)-j)9J&%0J"""r(   c                    | j         }                    |          }||_        ||_        j                                        }|r0|                    dg           }| j                            |           	                    |d         | j                  }|d         }|d         }	t          j        d||	           j                            ||	          }
|
rXt          j        d           j                            |
j                  }||_         |j                                       d S j                            d	|
          }||_         ||_        |	|_        |j                                       d S )Ndefault_channelsr   default_prefix	pkgs_dirs8Loading repodata for channels `%s` and package dirs `%s`r   r   +Extracting packages and apps from repodata.repodatametadatacondar   )r   _process_conda_infor   processed_infor+   load_rcr   r   extendr   r   rx   get_repodatar,   load_repodatar1   r   r   searchr   r   )r   r   r   r   r   condarcrc_default_channelsr   r   r   r   r   _get_repodata_load_repo_datarn   s               r&   r   z6_AnacondaAPI.conda_data.<locals>._conda_info_processed   s    ,K!55d;;N#K)7K&o--//G C&-kk2Db&I&I#&--.ABBB44T*5EvG]^^H*+F[)ILSU]_hiii33XQZ3[[H ?JKKK!-;;XX\Xf;gg
)4
&'//@@@@@ "_33GF3KK
)4
&&.
#'0
$'//>>>>>r(   c                 J   t          j        d| j        | j                   j                            | j        | j                  }t          j        d           j                            |j                  }| j	        |_	        |j
                                       d S )Nr   r   r   r   )r   rx   r   r   r+   r   r,   r   r1   r   r   r   )r   r   r   r   r   r   rn   s        r&   r   z._AnacondaAPI.conda_data.<locals>._get_repodata  s    LSU[Udflfvwww33V_X^Xh3iiHLFGGG)77TXTb7ccJ%+%7J"#++O<<<<<r(   c                 x    | j         }|\  }}|j        |j        ||d}|j                            |||           d S )N)r   r   packagesapplications)r   r   r   r   r   )r   r   r   r   r   r   
new_outputs          r&   r   z0_AnacondaAPI.conda_data.<locals>._load_repo_data  sU     ,K%+"Hl#("-"<$ ,	 J *//ZOOOOOr(   T)r   r   )r1   load_bundled_metadatar+   r   r   r   )rn   r   r   r   r   r   r   s   ``  @@@r&   
conda_dataz_AnacondaAPI.conda_data   s     ~ 	)&&(((	1 	1 	1 	1 	1 	1 	1	? 	? 	? 	? 	? 	? 	?>	= 	= 	= 	= 	= 	=
	P 
	P 
	P %%VT%JJ##$9:::r(   c                 |      fd} j                             |          }|j                            |           |S )zz
        Return the processed conda info for a given prefix.

        If prefix is None, the root prefix is used.
        c                 j                         |          }| j                            | ||           d S r"   )r   r   r   )r   r   r   r   rn   s       r&   r   z6_AnacondaAPI.conda_info.<locals>._conda_info_processed4  s7    !55d;;N%**6>5IIIIIr(   r   )r+   r   r   r   )rn   r   r   r   s   `   r&   
conda_infoz_AnacondaAPI.conda_info-  sX    	J 	J 	J 	J 	J %%V%44##$9:::r(   c                 6    d }|                      ||          S )zShow config for a given prefix.c                 B    | j                             | d|i|           d S )Nconfigr   r   r   r   r   s      r&   r   z*_AnacondaAPI.conda_config.<locals>._config?  s(    %**6Hf3EuMMMMMr(   r   connected_method_create_worker)rn   r   r   s      r&   conda_configz_AnacondaAPI.conda_config<  s-    	N 	N 	N""&7"KKKr(   c                 6    d }|                      ||          S )z'Show config sources for a given prefix.c                 B    | j                             | d|i|           d S )Nconfig_sourcesr   r   s      r&   _config_sourcesz:_AnacondaAPI.conda_config_sources.<locals>._config_sourcesF  s)    %**64Df3MuUUUUUr(   r   r   )rn   r   r   s      r&   conda_config_sourcesz!_AnacondaAPI.conda_config_sourcesC  s-    	V 	V 	V""&?"SSSr(   c                 J      fd}d                       |          S )z2Show config and config sources for a given prefix.c                     | }j                                       } || _        || _        | j                                       d S Nr   )r+   r   r   r   r   r   )r   r   r   r   r   r   rn   s       r&   r   z>_AnacondaAPI.conda_config_and_sources.<locals>._config_sourcesM  sM     K_000??F$*F!!,F''00000r(   c                 `    | j         }|| j        d}|j                            |||           d S )N)r   r   )r   r   r   r   )r   r   r   r   r   s        r&   r   z6_AnacondaAPI.conda_config_and_sources.<locals>._configT  sB     ,K "("7 J *//ZOOOOOr(   r   r   )rn   r   r   r   s   `` @r&   conda_config_and_sourcesz%_AnacondaAPI.conda_config_and_sourcesJ  sU    	1 	1 	1 	1 	1 	1 	1	P 	P 	P ""&?"SSSr(   r   r   returnr   c                 p    | j                             |          }|j                            |           |S r   )r+   config_show_sourcesr   r   )rn   r   r   r   s       r&   r   z_AnacondaAPI._create_worker^  s7    44F4CC##$4555r(   c                    |                                  }g }| d         D ]&}t          |          r|                    |           '||d<   g }|                     d          D ]&}t          |          r|                    |           '||d<   |                     d          }t	                      }d||<   |                     d          }d |D             }	t          t          |	|                    D ]6\  }
}t          r%|d	                                         |d
d         z   }|
||<   7d||<   t          
                    |          |d<   |S )z2Process conda info output and add some extra keys.	envs_dirs__envs_dirs_writabler   __pkgs_dirs_writableroot_prefixzbase (root)envsc                 L    g | ]!}t           j                            |          "S r#   )ospathbasename).0envs     r&   
<listcomp>z4_AnacondaAPI._process_conda_info.<locals>.<listcomp>{  s(    <<<bg&&s++<<<r(   r   r   N__environments)copyr   r   r   r   sortedzipr   upperr   filter_environments)r   r   envs_dirs_writableenv_dirpkgs_dirs_writablepkg_dirr   environmentsr   
envs_namesenv_name
env_prefixs               r&   r   z _AnacondaAPI._process_conda_infoc  s      K( 	3 	3G(( 3"))'2221C-.  xx,, 	3 	3G(( 3"))'2221C-. hh}--"}}$1[!xx<<t<<<
$*3z4+@+@$A$A 	0 	0 Hj D']0022Z^C
'/L$$ %2[!+7+K+KL+Y+Y'(r(   c                     t                      }|                                 D ]\  }}|                    d          s|||<    |S )z
        Removes all environments which names starts with underscore.

        :param OrderedDict envs: List of environments which will be filtered.

        :return OrderedDict: Filtered environments.
        _)r   r   
startswith)r   filtered_envskeyr   s       r&   r  z _AnacondaAPI.filter_environments  sM     $

 	) 	)HC>>#&& )%(c"r(   r#   c                    	 	 fd} fd	fd|                                 }D ]}||v r|                    |            j                                      }||_        |j                            |           |S )zAProcess packages data and metadata to row format for table model.c                     j                                       }||_        | |_        |j                                       d S r   )r+   listbase_outputr   r   r   )r   r   r   r   _pip_data_readyr   rn   s       r&   _call_list_prefixz8_AnacondaAPI.process_packages.<locals>._call_list_prefix  sH    _)))88F!'F!,F''88888r(   c                 `   | j         }| j        }|j        }|rt          j        |           |pg }j                                      }t          d |D                       }j                                        dft          fd|D                       }	j        
                    |	          }
j                            |
j                  \  }}|                    |           |}j                            ||||          } || _        | j                                       d S )Nr   c              3   >   K   | ]}|d          dk    |d         V  dS )platformpypibase_urlNr#   )r   packages     r&   	<genexpr>zI_AnacondaAPI.process_packages.<locals>._pip_data_ready.<locals>.<genexpr>  s8      jjwGT^L_ciLiLiwz2LiLiLiLijjr(   noarchc                 D    h | ]}D ]}t          j        ||          S r#   )r   r   )r   r  r  	platformss      r&   	<setcomp>zI_AnacondaAPI.process_packages.<locals>._pip_data_ready.<locals>.<setcomp>  sL     8 8 8 )8 8  x228 8 8 8r(   )r  r   r   r   r   r+   rQ   setrY   r  r   r,   _load_repodatar1   updateprepare_model_datar   r   )r   r   r   pip_list_datar   clean_packagespip_packagesrQ   channel_urlsmetadata_channelsmeta_repodatar   appsr   r  _model_data_readyr   rn   s                 @r&   r  z6_AnacondaAPI.process_packages.<locals>._pip_data_ready  s[   ".M ,K(1N $U###(.BL _++6+::Fjj&jjjjjL151M1M1O1OQY0ZI26 8 8 8 8 ,8 8 8 3 3 !O889JKKM!-<<t~ NHd OOD!!!H%88Q]_ghhF!,F''(9:::::r(   c                 j   | j         }|j        }|d d          }D ]x}||v r|                    |           t          t	          t          |                              D ]3}||         t          j                 |k    r|                    |           4y|j        	                    |||f|           d S r"   )
r   r   popreversedrangelenCCOL_NAMEr   r   )	r   r   r   r   r&  datapackage_namerow	blacklists	           r&   r,  z8_AnacondaAPI.process_packages.<locals>._model_data_ready  s     ,K(1N!!!9D !* & &>11"&&|444 $E#d))$4$455 & &CCy,<<&
 *//nd=SUZ[[[[[r(   r   )r   r.  r+   pip_listr   r   r   )
rn   r   r   r7  r  r&  r5  r   r,  r  s
   ` ``    @@r&   process_packagesz_AnacondaAPI.process_packages  s    	9 	9 	9 	9 	9 	9 	9	; 	; 	; 	; 	; 	; 	;B	\ 	\ 	\ 	\ 	\( "% 	1 	1L~--""<000 )))88(##$5666r(   r+  zapi_types.RawApplicationzapi_types.Applicationc                 6   || j         }i }t          j        t          | j                  }|                     |d          }| j                                        }fd|                     |                                          D             }t          j
                                        |                                          D ]\  }}	|	                    dg           }
|
s|j                            |t          j                  }|j        sL|                    |i           }|                    |i           }|	                    dd          p-|                    dd          p|                    dd          pd}|                    d	d          p!|                    d	d          pt          j        }|	                    d
          p|
d         }|                     ||d          }|	                    di           }|                    |d          }|r|                    ||          }|sqt%          j        dd|d          }t(          j        j        ||||t/          |          |dd|p||
d||<   |                    ||                    |j                                        D ]}|j        s
|j        ||j        <   |j                                        D ]/}|j        s
|j        r|j        s|j         s |j        ||j        <   0|S )zProcess app information.N)configurationprocess_apiT)
envs_cachec                 $    i | ]\  }}|v	||S r#   r#   )r   r  valuer+  s      r&   
<dictcomp>z-_AnacondaAPI.process_apps.<locals>.<dictcomp>  s3     _
 _
 _
U$ r(   versionsdescriptionrv   
image_pathlatest_versionF)r   pkgbuild	app_entryz%(?i:ipython\s+(?=notebook|qtconsole))zjupyter-r   r   )app_typecommandrB  display_namerC  	installedname	non_condarankversionrA  )!r/   r   get_applicationsr   r.   rT   r+   local_apps_infor   	itertoolschainr   app_patchesEMPTY_PATCHis_availabler   ANACONDA_ICON_256_PATHrX   resubr2  AppTypeCONDAr~   apply_toweb_appsvaluestile_definitionapp_nameinstallable_appsrN  
executableis_installation_enabled)rn   r+  r   r   collected_applicationsrS   rR  missing_appsra  app_datarA  application_patchlinked_app_infolocal_app_inforB  rC  rD  installed_versionapp_entriesrJ  web_appapps    `                    r&   process_appsz_AnacondaAPI.process_apps  s    >%FBDFSFd)G
 G
 G
  66v$6OOEI_EdEdEfEf_
 _
 _
 _
"99&AAGGII_
 _
 _
 #,/$**,,@R@R@T@T"U"U 5	? 5	?Hh-5\\*b-I-IH  !7 B F FxQ^Qj k k$1  8H7K7KHVX7Y7YO6E6I6I(TV6W6WN ]D11 ##M488""=$77 	   ##L$77 .""<66.- 
 19=M0N0N0^RZ[]R^N !% : :&h^c : d dBJ,,{\^B_B_K,7OONB,O,OG  F%//*;WEE fEzSZ\]^^G IO"* ((!"344 ",>$& &L" &&|H'=>>>> .6==?? 	E 	EG' -4-DL)** *:AACC 	= 	=C# } cn s?Z ),)<L&&r(   c                    t           j        }t           j        }t          }t          j                            |          r	 t          |d          5 }t          j	        |          
                    di           | _        ddd           n# 1 swxY w Y   t          j        d|           nF# t          $ r9}t          j        |           i | _        t          j        d           Y d}~nd}~ww xY wdS #  Y dS xY w	 t	          j        |d           n# t"          $ r Y nw xY wd}|r[t          j                            |          r<t          |d          5 }|                                }ddd           n# 1 swxY w Y   |r 	 t)          j        |          }t          |d	          5 }|                    |           ddd           n# 1 swxY w Y   t/          |          r|                                }t          j        |          
                    di           | _        t          j        d|           dS # t          $ r:}t          j        |           i | _        t          j        d           Y d}~dS d}~ww xY wdS )
zLoad bundled metadata.rr   NMetadata was loaded from `%s`Metadata is empty!T)exist_okrbwb)r   BUNDLE_METADATA_COMP_PATHCONF_METADATA_PATHr   r   r   existsopenjsonloadr   r1   r   rx   	Exception	exceptionmakedirsOSErrorisfilereadbz2
decompresswriter   decodeloadsr   )	rn   comp_meta_filepathconf_meta_filepathconf_meta_folder	json_fileebinary_datafr4  s	            r&   r   z"_AnacondaAPI.load_bundled_metadataK  sk   $>$7(7>>,-- 	
,c22 Ni%)Yy%9%9%=%=j"%M%MDNN N N N N N N N N N N N N N N<>PQQQQ 3 3 3 ###!#1222222223 	K(488888 	 	 	D	  	'"'..1C"D"D 	'($// '1ffhh' ' ' ' ' ' ' ' ' ' ' ' ' ' '  	33~k22,d33 "qGGDMMM" " " " " " " " " " " " " " " $D)) );;==D!%D!1!1!5!5j"!E!E<>PQQQQQ 3 3 3Q!#12222222223	3 	3s    B+ .B
>B+ 
BB+ BB+ *C3 +
C.5/C)$C3 )C..C3 3C8<D 
D D E77E;>E;$H> *G H> GH> GA(H> >
J/I==Jc                 2     fd}d  j         t          d          d}t          j        }                     ||          }t
          j        |_        |_        |_	        |j
                            |           t          j        d||           |S )z
        Update the metadata available for packages in repo.anaconda.com.

        Returns a download worker with chained finish signal.
        c                 T   | }|r[t           j                            |          r<t          |d          5 }|                                }ddd           n# 1 swxY w Y   	 t          j        |                              di           	_        t          j
        d|           nF# t          $ r9}t          j        |           i 	_        t          j
        d           Y d}~nd}~ww xY w	j                            d          } || _        | j                                       dS )zCallback for update_metadata.rq  Nr   rr  rs  r   r   )r   r   r  rz  r  r{  r  r   r1   r   rx   r}  r~  r+   r   r   r   r   )
r   r   r   r   r  r4  r  _index_updatedr   rn   s
          r&   _metadata_updatedzA_AnacondaAPI.update_index_and_metadata.<locals>._metadata_updated~  sf    K $t,, $$__ $6688D$ $ $ $ $ $ $ $ $ $ $ $ $ $ $3!%D!1!1!5!5j"!E!E<dCCCC 3 3 3 ###!#1222222223
 _++GF+CCF!,F''77777s*   AAA"AB% %
C(//C##C(c                 L    | j         }|j                            |d d            d S r"   r   r   s       r&   r  z>_AnacondaAPI.update_index_and_metadata.<locals>._index_updated  s+     ,K*//T4HHHHHr(   Nz,Need to call `api.set_data_directory` first.z4https://repo.anaconda.com/pkgs/main/channeldata.jsonz)Downloading metadata from `%s` into `%s`.)r2   r}  r   rx  r?   r2  ACTION_SEARCHactionr   
old_prefixr   r   r   rx   )rn   r   r  metadata_urlfilepathr   r  s   ``    @r&   update_index_and_metadataz&_AnacondaAPI.update_index_and_metadatax  s    	8 	8 	8 	8 	8 	8 	8$	I 	I 	I
 'JKKKM-|X66"##$5666@,PXYYYr(   pythonFc                    | j                             |||t                                 }t          j        |_        d| d|_        ||_        | j                             |          |_	        t          j        d||           |S )z*Create environment and install `packages`.)r   pkgsno_default_pythonofflinezCreating environment <b></b>z5Creating environment `%s` with following packages: %s)r+   creater   r2  ACTION_CREATEr  
action_msgr   get_name_envprefixrM  r   rx   )rn   r   r   r  r   s        r&   create_environmentz_AnacondaAPI.create_environment  s     ''/-///	 ( 
 
 CvCCCo88@@LfV^___r(   c                 R   | j                             ||t                                 }t          j        |_        | j                             |          }d| d| d|_        ||_        | j                             |          |_	        ||_
        t          j        d||           |S )z8Clone environment located at `clone` (prefix) into name.r   r  zCloning from environment <b>z</b> into <b>r  z(Cloning from environment `%s` into `%s`.)r+   clone_environmentr   r2  ACTION_CLONEr  r  r  r   rM  cloner   rx   )rn   clone_from_prefixr   r   clone_from_names        r&   r  z_AnacondaAPI.clone_environment  s    22-/// 3 
 

 /<<=NOOe?eeY_eeeo88@@(?ARTZ[[[r(   c                     | j                             ||          }t          j        |_        d| d|_        ||_        | j                             |          |_        ||_	        t          j        d||           |S )z@Export environment, that exists in `prefix`, to the yaml `file`.)filer   zBacking up environment <b>r  z&Backing up environment `%s` into `%s`.)r+   export_environmentr2  ACTION_EXPORTr  r  r   r  rM  r  r   rx   )rn   r   r  r   s       r&   r  z_AnacondaAPI.export_environment  sw    33f3MMEEEEo88@@=vtLLLr(   r  validate_onlyc                 |   | j                             ||t                       |          }t          j        |_        |rd|_        t          j        d|           n3dt          j
        |           d|_        t          j        d||           ||_        | j                             |          |_        ||_        |S )z9Import new environment on `prefix` with specified `file`.)r   r  r  dry_runzValidating environmentzValidating environment `%s`.zImporting environment <b>r  z%Importing environment `%s` from `%s`.)r+   r  r   r2  ACTION_IMPORTr  r  r   rx   htmlescaper   r  rM  r  )rn   r   r  r  r   s        r&   import_environmentz_AnacondaAPI.import_environment  s    ''-///!	 ( 
 
  	P 8FL7@@@@ UDK<O<O U U UFL@&$OOOo88@@r(   c                    | j                             |t                                 }t          j        |_        d| d|_        ||_        | j                             |          |_	        t          j        d|           t          }|| j        k    r%t          j                            ||j	                  }	 t#          j        |           t          j        d|           n# t&          $ r Y nw xY w|S )zRemove environment `name`.r  zRemoving environment <b>r  zRemoving environment `%s`zScripts path `%s` was removed.)r+   remove_environmentr   r2  ACTION_REMOVE_ENVr  r  r   r  rM  r   rx   r   r/   r   r   r   shutilrmtreer  )rn   r   r   scripts_paths       r&   r  z_AnacondaAPI.remove_environment  s    33-/// 4 
 
 +CvCCCo88@@0&999 +T%%%7<<fkBBL	M,'''L9<HHHH 	 	 	D	 s   9)C# #
C0/C0c                 ,   | j                             ||||t                                 }d| d|_        t          j        |_        ||_        ||_        | j         	                    |          |_
        ||_        t          j        d|||           |S )z'Install `pkgs` in environment `prefix`.)r   r  r  r  r  zInstalling packages on <b>r  zJInstalling packages on prefix `%s`. (dry_run=%s) 
 Packages to install: %s)r+   installr   r  r2  ACTION_INSTALLr  r  r   r  rM  r  r   rx   )rn   r   r  r  r  r   s         r&   install_packagesz_AnacondaAPI.install_packages  s    ((/-/// ) 
 
 FEEE( o88@@Y[acjlp	r 	r 	rr(   c           	      .   | j                             |||||t                                 }d| d|_        t          j        |_        ||_        ||_        | j         	                    |          |_
        ||_        t          j        d|||           |S )z&Update `pkgs` in environment `prefix`.)r   r  r  r  all_r  zUpdating packages on <b>r  zGUpdating packages on prefix `%s`. (dry_run=%s) 
 Packages to update: %s)r+   r#  r   r  r2  ACTION_UPDATEr  r  r   r  rM  r  r   rx   )rn   r   r  r  r  r  r   s          r&   update_packagesz_AnacondaAPI.update_packages  s     ''/-/// ( 
 
 DvCCC o88@@_agiprvwwwr(   c                    | j                             |||t                                 }d| d|_        t          j        |_        ||_        | j                             |          |_	        ||_
        t          j        d|||           |S )z(Remove `pkgs` from environment `prefix`.)r   r  r  r  zRemoving packages from <b>r  zIRemoving packages from prefix `%s`. (dry_run=%s) 
 Packages to remove: %s)r+   r5   r   r  r2  ACTION_REMOVEr  r   r  rM  r  r   rx   )rn   r   r  r  r   s        r&   remove_packagesz_AnacondaAPI.remove_packages,  s    ''-///	 ( 
 
 FEEEo88@@XZ`biko	q 	q 	qr(   c                    |t           j        k    rd}nrd}| rt          | t                    r| d         } | rO|                     dg           }	 d |D             }n# t
          $ r d |D             }Y nw xY w|D ]}|dk    rd} n|S )z?Check if navigator is affected by the operation on (base/root).Fr   UNLINKc                 T    i | ]%}t          |          d          t          |          &S )r   r   r   ps     r&   r@  z=_AnacondaAPI.check_navigator_dependencies.<locals>.<dictcomp>Q  s3    fffXY 4Q 7 7 :<PQR<S<Sfffr(   c                 l    i | ]1}t          |d                    d         t          |d                    2S )	dist_namer   r  r  s     r&   r@  z=_AnacondaAPI.check_navigator_dependencies.<locals>.<dictcomp>T  sH          EF -Q{^<<Q?0;@@     r(   zanaconda-navigatorT)sysr   
isinstancer  r   AttributeError)actionsr   	conflictsunlinkedrF  s        r&   check_navigator_dependenciesz)_AnacondaAPI.check_navigator_dependencies=  s     SZII I %:gt44 %!!* ";;x44ff]efffHH%      JR     HHH $  C222$(	 3 s   A A43A4c                    |r|| d<   t          j        d|           |                     dg            |r || d<   dg| d<   t          j        d|           |r0| d                             |           t          j        d|           | S )zf
            Replace `channels`, `default_channels`, `channel_alias` with a new or empty data
        channel_aliasz<.condarc file was updated with following `channel_alias`:
%sr   r   defaultsz?.condarc file was updated with following `default_channels`:
%sz7.condarc file was updated with following `channels`:
%s)r   rx   
setdefaultr   )rc_datar   r   r  s       r&   __modify_condarcz_AnacondaAPI.__modify_condarc`  s    
  	i'4GO$LXZghhh:r*** 	o*:G&'#-,GJL[]mnnn 	_J&&x000LSU]^^^r(   logged_brandlogged_api_urluser_idc                    |t          j        t           j        d          }|t          j        t           j        d          }|r|sd| j        j        _        dS |B	 |                                 }n,# t          j        j	        $ r d| j        j        _        Y dS w xY w| j        j        
                    |||          }|| j        j        _        t          j        d||||           |S )z0Generate unique identifier for current .condarc.Nr  r  zZA new key `%s` was generated from following items: 
brand: `%s` domain: `%s` user_id: `%s`)r   r   DEFAULT_SECTION_NAMEr+   rc_indexcurrentget_user_identifierrequests
exceptionsRequestExceptiongenerate_config_index_keyr   rx   )rn   r  r  r  results        r&   generate_rc_keyz_AnacondaAPI.generate_rc_keyu  s    8D$=~NNL!!Xd&?AQRRN 	n 	/3DO$,4?2244&7   
 48(0tt o.HHWegnoo+1 (i	
 	
 	
 s   A2 2%BBrc_keyr  c                 b   | j                                         }| j         j                            |           |s| j         j                            |          }|                     ||          }| j         j                            ||           | j                             |           dS dS )z
        Saves the copy of the .condarc data and replaces it with a modified snapshot.

        :param rc_key: Key to get snapshot for.
        :param channel_alias: Alias of the channel to integrate into restored snapshot.
        r4  N)r  r4  r  )r+   r   r  save_rc_copyload_rc_copy_AnacondaAPI__modify_condarcsave_rc)rn   r  r  r  s       r&   __replace_condarcz_AnacondaAPI.__replace_condarc  s     /))++ --7-;;;o.;;FCCG++G=+QQGO$11wv1NNNO##G,,,,, r(   c                    |:| j                                         }| j         j                            ||           | j         j                                        }| j                             |           d| j         j        _        dS )z.Load the data which was before user logged in.Nr  r  )r+   r   r  r  r  r  r  )rn   r  r  s      r&   restore_condarcz_AnacondaAPI.restore_condarc  s{    o--//GO$11wv1NNN/*7799W---+/ (((r(   c                     | j                                         }|                     |||          }| j                             |           d S r"   )r+   r   r  r  )rn   r   r   r  s       r&   update_channelsz_AnacondaAPI.update_channels  sI    /))++''1A8LL(((((r(   c                    t          j                    \  }}|                     ||          }|t          j        k    rt          j        |d          }|                     ||           t          j	        | j
        j                                                  }|rl| j
        j                            |          }t          j        dd|d                    t          j        dd|d                    t          j        d	|           |t          j        k    r;t          j        t%          |          d
          }|                     ||           dS dS )a  
        Creates the login data needed to interact with Anaconda Server instance.

        Updates .condarc data with channels which are accessed by authenticated user.
        Updates anaconda-navigator.ini file with Anaconda Server access token and token ID.
        r  r  zapi/repo)r  r  mainanaconda_server_tokenr   anaconda_server_token_ididz%s access token was generated.r   N)r   get_logged_datar  r   TEAM_EDITIONr   r   _AnacondaAPI__replace_condarcr{  r  r,   anaconda_client_api
load_tokencreate_access_tokenr!  r   rx   ENTERPRISE_EDITIONr   )rn   r  r  r  r  jwt_token_dataaccess_tokens          r&   create_login_dataz_AnacondaAPI.create_login_data  sC    (,';'='=$n**Uc*dd=555%N>:FFM""&"NNN!Z(8(L(W(W(Y(YZZN M#/CWWXfgg!8,w:OPPP!;\$=OPPP=|LLL=;;;%N+B>+R+RT[\\M""&"NNNNN <;r(   c                    t          j                    \  }}|                     ||          }|t          j        k    rt          j        dd          }|                     |           	 | j        j        	                    |           t          j        d|           n# t          j        j        $ r Y nw xY wt          j                     |t          j        k    r|                     |           dS dS )a  
        Removes the login data needed to interact with Anaconda Server instance.

        Updates .condarc data by removing channels which were accessed by authenticated user.
        Updates anaconda-navigator.ini file with removing Anaconda Server access token and token ID.
        r  r  r  )r  z%s access token was removedN)r   r  r  r   r   r   r  r,   r  remove_access_tokenr   rx   r  r  r  set_logged_datar  )rn   r  r  r  access_token_ids        r&   remove_login_dataz_AnacondaAPI.remove_login_data  s    (,';'='=$n**Uc*dd=555"hv/IJJO   /// 4HHYYY:LIIII&7     """=;;;   ///// <;s   *4B B65B6c                     g }t          | j        j        t                    r| j        j                                        }t          j        d|           |S )
        Returns the list of available channels.

        :return list[dict[str, mixed]]: The list with dictionaries with info about channels.
        z%Current available channels are 
 `%s`)r  r,   r  r   get_channelsr   rx   )rn   r   s     r&   r  z_AnacondaAPI.get_channels  sQ     d&:NKK 	K';HHJJH=xHHHr(   c                 2    t           j        j        t                    r\ j                             j        j        j                  }|j                             fd           |	                                 dS  j
                            d           dS )r  c                 8    j                             |          S r"   )sig_api_healthr   )r  healthyr   rn   s      r&   r'   z+_AnacondaAPI.health_check.<locals>.<lambda>  s    $BUBZBZ[bBcBc r(   NT)r  r,   r  r   r.   create_python_workerpingr   r   r   r  r   )rn   r   s   ` r&   health_checkz_AnacondaAPI.health_check  s     d&:NKK 	&;;D<L<`<effF''(c(c(c(cdddLLNNNF  &&&&&r(   c                     t          | j        j        t                    r| j        j                                        }n(|                                                     dd          }t          j        d|           |S )Nr]   rv   zCurrent user_id is `%s`)	r  r,   r  r   get_user_idrb   r   r   rx   )rn   r  s     r&   r  z _AnacondaAPI.get_user_identifier  sl    d&:NKK 	:&:FFHHGG&&((,,Wb99G.888r(   c                    t          j        ddd          }t          j        ddd          }|r.|r,|                     |           t          j        d|           dS t          j        d|           |                     |           dS )zCReset ssl preferences in clients to the current Navigator settings.r  ssl_verificationTssl_certificateNz$SSL certificate was changed to `%s`.z%SSL verification was changed to `%s`.)r   r   ri   r   rx   )rn   r  r  s      r&   client_reset_sslz_AnacondaAPI.client_reset_ssl  s    !%&2Dd!K!K04ARTX0Y0Y 	2 	2000L?QQQQQL@/RRR 011111r(   r"   )Nr#   )r  F)F)FF)NFFF)NNN)NN)r   N)?__name__
__module____qualname____doc__r   objectr  sig_metadata_updatedsig_repodata_loadedsig_repodata_updatedsig_repodata_errored	sig_errorr*   rr   staticmethodrt   r]   r_   r   r   r   r   r   r   r   r   strtypingCallabler   r   r  r9  Mapping	api_typesApplicationNameOptionalDictro  r   r  r  r  r  r~   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  __classcell__)ro   s   @r&   r   r   0   s         VF^^N!6&>> &00!6&>>!688I3< 3< 3< 3< 3<n. . . + + \+k k k k) ) )- - -  6 0 0 \0dG G G GR   L L L LT T T TT T T T(S FO P_    
 $ $ \$L   \ H H H HZ ,0b b.!:<V!VWb OC(b 
S11	2	b b b bL+3 +3 +3Z( ( ( (Z !#	   (   
 
 
  C  Yh    (  0   ,    6   "     \ D    \, 2637,0	$ $ /#.$ #OC0$ _S)	$
 
	$ $ $ $P ,026- -OC(- "?3/- 
	- - - -*0 0foc&: 0d 0 0 0 0) ) ) )
O O O80 0 08  ' ' '  	2 	2 	2 	2 	2 	2 	2 	2r(   r   c                  :    t           t                      a t           S )zManager API threaded worker.)ANACONDA_APIr   r#   r(   r&   AnacondaAPIr4  *  s     #~~r(   );r!  rS  collectionsr   r  r  r{  r   rY  r  r  r*  qtpy.QtCorer   r   r  anaconda_navigator.apir   !anaconda_navigator.api.client_apir    anaconda_navigator.api.conda_apir   #anaconda_navigator.api.download_apir	   anaconda_navigator.api.processr
   'anaconda_navigator.api.team_edition_apir   anaconda_navigator.api.utilsr   r   anaconda_navigator.configr   r   r   r   r   anaconda_navigator.staticr   r   anaconda_navigator.utilsr   r2  r   r   anaconda_navigator.utils.logsr   anaconda_navigator.utils.miscr   "anaconda_navigator.utils.py3compatr   rv   r   r-  TYPE_CHECKINGr   r   r3  r4  r#   r(   r&   <module>rE     s=   D C     # # # # # # 



   				 				  



  ' ' ' ' ' ' ' '  0 0 0 0 0 0 7 7 7 7 7 7 5 5 5 5 5 5 ; ; ; ; ; ; 8 8 8 8 8 8 B B B B B B T T T T T T T T b b b b b b b b b b b b b b 5 5 5 5 5 5 5 5 L L L L L L L L . . . . . . 0 0 0 0 0 0 : : : : : : ? ? ? ? ? ?            	 ?>>>>>>t2 t2 t2 t2 t27 t2 t2 t2n     r(   