
    9iV                       S r SSKJr  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  SSKJr  SSKJr  S	S
KJrJrJr  SSKJrJr  SSKJr  SSKJr  SSKJrJrJrJ r J!r!  \" \"5      r# " S S\5      r$ " S S\5      r%g)     )annotationsN)	getLogger)Path)CompletedProcess)NamedTemporaryFile)Any   )WinLex
logged_rununlink   )MenuMenuItem)folder_path)windows_terminal_settings_files)notify_shell_changesregister_file_extensionregister_url_protocolunregister_file_extensionunregister_url_protocolc                     ^  \ rS rSrSS jrSS jr\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
\SU 4S jj5       rSS	 jrSSU 4S
 jjjrSSS jjrSS jrSrU =r$ )WindowsMenu   c                <   [         R                  SU R                  5        U R                  R                  SSS9  U R                  (       a  U R                  R                  SSS9  U R
                  (       a  U R
                  R                  SSS9  U R                  4$ )NzCreating %sTparentsexist_ok)logdebugstart_menu_locationmkdirquick_launch_locationdesktop_locationselfs    6lib/python3.13/site-packages/menuinst/platforms/win.pycreateWindowsMenu.create   s    		-!9!9:  &&td&C%%&&,,TD,I  !!''t'D((**    c                j   [        U R                  5      nUR                  5       (       a$   [        UR	                  5       5        [        5       $ [        5       $ ! [
         aO    [        R                  SU R                  5        [        R                  " U R                  SS9  U R                  4s $ f = f)NRemoving %sT)ignore_errors)r   r!   existsnextiterdirStopIterationr   r    shutilrmtreetuple)r&   menu_locations     r'   removeWindowsMenu.remove(   s    T556!!3 ]**,-
 wuw	 ! 3		-)A)ABd66dK00223s   A AB21B2c                z    [        [        U R                  SS5      5      U R                  U R                  5      -  $ )z
On Windows we can create shortcuts in several places:

- Start Menu
- Desktop
- Quick launch (only for user installs)

In this property we only report the path to the Start menu.
For other menus, check their respective properties.
Fstart)r   windows_folder_pathmoderendernamer%   s    r'   r!   WindowsMenu.start_menu_location6   s2     '		5'BCdkkRVR[R[F\\\r*   c                    U R                   S:X  a  [        R                  " S5        g [        [	        U R                   SS5      5      $ )Nsystemz>Quick launch menus are not available for system level installsFquicklaunch)r;   warningswarnr   r:   r%   s    r'   r#   !WindowsMenu.quick_launch_locationD   s7    99 MMZ['		5-HIIr*   c                B    [        [        U R                  SS5      5      $ )NFdesktop)r   r:   r;   r%   s    r'   r$   WindowsMenu.desktop_locationL   s    '		5)DEEr*   c                z    U R                   S:X  a  [        R                  S5        / $ [        U R                   5      $ )zLocation of the Windows terminal profiles.

The parent directory is used to check if Terminal is installed
because the settings file is generated when Terminal is opened,
not when it is installed.
r@   z=Terminal profiles are not available for system level installs)r;   r   warningr   r%   s    r'   terminal_profile_locations&WindowsMenu.terminal_profile_locationsP   s1     99 KKWXI.tyy99r*   c                  > [         TU ]  nUR                  [        U R                  S-  5      [        U R                  S-  5      [        U R                  S-  5      [        U R
                  S-  5      [        U R
                  S-  5      [        U R                  S-  S-  5      [        U R                  5       5      SS.5        U$ )NScriptsz
python.exezpythonw.exeLibrarybinico)SCRIPTS_DIRPYTHONPYTHONWBASE_PYTHONBASE_PYTHONWBIN_DIRSP_DIRICON_EXT)superplaceholdersupdatestrprefixbase_prefix_site_packages)r&   rZ   	__class__s     r'   rZ   WindowsMenu.placeholders]   s    w+"4;;#:;dkkL89t{{]:;"4#3#3l#BC #D$4$4}$D Et{{Y6>?d1134!		
 r*   c           
     J   U R                   S-  [        [        R                  R	                  SS5      5      U R                   S-  S-  U R                   S-  S-  [        [        R                  R	                  SS5      5      U R                   S-  S-  U R                   S-  S-  4$ )	Nz
_conda.exe	CONDA_EXEz"C:\oops\a_file_that_does_not_existcondabinz	conda.batrO   	MAMBA_EXEzmicromamba.exe)r^   r   osenvirongetr%   s    r'   _conda_exe_path_candidates&WindowsMenu._conda_exe_path_candidatesn   s    |+-RSTz)K7u${2-RSTz),<<u$'77
 	
r*   c                   > [         TU ]  XUS9n[        US5      (       a!  SU;   a  US   S:w  a  UR                  SS5      nU$ )a  
We extend the render method here to replace forward slashes with backslashes.
We ONLY do it if the string does not start with /, because it might
be just a windows-style command-line flag (e.g. cmd /K something).

This will not escape strings such as `/flag:something`, common
in compiler stuff but we can assume such windows specific
constructs will have their platform override, which is always an option.

This is just a convenience for things like icon paths or Unix-like paths
in the command key.
)slugextrareplace/r   \)rY   r<   hasattrrn   )r&   valuerl   rm   r`   s       r'   r<   WindowsMenu.rendery   sI     uu=5)$$%(c/MM#t,Er*   c                D    Uc  U R                   nU R                   S-  S-  $ )NLibzsite-packages)r]   )r&   r]   s     r'   r_   WindowsMenu._site_packages   s$    >[[F{{U"_44r*   c                    U R                   4$ N)r!   r%   s    r'   _pathsWindowsMenu._paths   s    ((**r*    )returnztuple[Path]r|   r   )r|   z
list[Path])r|   zdict[str, str])FN)rr   r	   rl   boolrm   zdict[str, str] | Noner|   r	   rx   )r]   zPath | Noner|   r   )__name__
__module____qualname____firstlineno__r(   r6   propertyr!   r#   r$   rJ   rZ   ri   r<   r_   ry   __static_attributes____classcell__)r`   s   @r'   r   r      s    + ] ] J J F F 
: 
:   	
 $5
+ +r*   r   c                  ,   \ rS rSr\SS j5       rSS jrSS jrSS jrSSS jjr	SS jr
S rSS	 jrSSS jjrS S!S jjrS S"S jjrS rS rS r\ S#         S$S jj5       r\ S#       S%S jj5       rS&S jrS&S jrS&S jrS&S jrS rSrg
)'WindowsMenuItem   c                P    U R                   R                  U R                  5       -  $ )z
Path to the .lnk file placed in the Start Menu
On Windows, menuinst can create up to three shortcuts (start menu, desktop, quick launch)
This property only lists the one for start menu
)menur!   _shortcut_filenamer%   s    r'   locationWindowsMenuItem.location   s"     yy,,t/F/F/HHHr*   c                h   SSK Jn  U R                  5         U R                  5       nU GH  nUR                  S:X  d  M  U R                  5       tpEU R                  S5      nU(       a7  [        [        R                  R                  U5      5      R                  SSS9  OS[        R                  ;   a  SnOS	nU R                  S
5      =(       d    Sn[        U5      R                  5       (       a  [        R                  SU5        U" UU R!                  SS9[#        U5      SR%                  U5      UUSU R'                  5       5        GM"     U R(                  R*                   H  nU R-                  USS9  M     U R/                  5       n	U R1                  5       n
U	(       d  U
(       a
  [3        5         U$ )Nr   )create_shortcutz.lnkworking_dirTr   USERPROFILEz%USERPROFILE%z%HOMEDRIVE%%HOMEPATH%iconr   z Overwriting existing link at %s.)ext r   Fr6   )win_utils.winshortcutr   
_precreatery   suffix_process_command
render_keyr   rf   path
expandvarsr"   rg   r.   r   rI   r   r\   join_app_user_model_idr   rJ   $_add_remove_windows_terminal_profile_register_file_extensions_register_url_protocolsr   )r&   r   pathsr   target_path	argumentsr   r   r   changed_extensionschanged_protocolss              r'   r(   WindowsMenuItem.create   si   :D;;&(&*&;&;&=#K//-8KRWW''45;;DSW;X
 "**,-5??6*0bD Dz  "">E''B'/D	#'')	3 H 		<<H55hu5M =!;;= 88:!2 "r*   c                d   U R                  5       nU R                  5       nU(       d  U(       a
  [        5         U R                  R                   H  nU R                  USS9  M     [        S U R                  5        5       5      nU H$  n[        R                  SU5        [        U5        M&     U$ )NTr   c              3  f   #    U  H'  n[        U5      R                  5       (       d  M#  Uv   M)     g 7frx   )r   is_file).0r   s     r'   	<genexpr>)WindowsMenuItem.remove.<locals>.<genexpr>   s!     M}tT
8J8J8Ldd}s   "1	1r,   )_unregister_file_extensions_unregister_url_protocolsr   r   rJ   r   r4   ry   r   r    r   )r&   r   r   r   r   r   s         r'   r6   WindowsMenuItem.remove   s    !==? ::<!2 "		<<H55ht5L = Mt{{}MMDIImT*4L  r*   c                D   U R                   /n/ nU R                  S   (       a%  UR                  U R                  R                  5        U R                  S   (       aV  U R                  R
                  (       a;  [        R                  " S5        UR                  U R                  R
                  5        U(       a%  X Vs/ s H  o3U R                  5       -  PM     sn-  nU R                  S   (       a  UR                  U R                  5       5        [        U5      $ s  snf )NrF   rA   z"Quick launch menus are deprecated.activate)r   metadataappendr   r$   r#   rB   rC   r   _path_for_scriptr4   )r&   r   
extra_dirs	directorys       r'   ry   WindowsMenuItem._paths   s    
==#dii889=='DII,K,KMM>?dii==>ZXZ	$"9"9";;ZXXE==$LL..01U| Ys   >Dc                F    U(       a  SU 3OSnU R                  S0 S9 U 3$ )N.r   r=   )rm   r   )r&   r   s     r'   r   "WindowsMenuItem._shortcut_filename   s.    #iB//&/34SE::r*   c                j    [        U R                  R                  S   5      U R                  S5      -  $ )NMENU_DIRbat)r   r   rZ   r   r%   s    r'   r    WindowsMenuItem._path_for_script   s-    DII**:67$:Q:QRW:XXXr*   c                   U R                  S5      nU(       d  g [        SSS9 nUR                  U5        S S S 5        [        [        R
                  R                  SS5      5      S-  n[        US-  S	-  S
-  5      SWR                   S3/n[        USS9  [        R                  " UR                  5        g ! , (       d  f       N= f)N	precreateFw)deleter;   
SystemRoot
C:\Windowssystem32WindowsPowerShellv1.0powershell.exe"start 'z' -WindowStyle hidden"Tcheck)r   r   writer   rf   rg   rh   r\   r=   r   r   )r&   precreate_codetmpr   cmds        r'   r   WindowsMenuItem._precreate   s    5u373IIn% 8

|]CDzQ..7:JJKz!89
 	3d#
		#(( 87s   B99
Cc           	        SS/nU R                  S5      nU(       a  UR                  U5        U R                  S   (       Ga  U R                  R	                  U R                  R
                  5      (       a:  U R                  R
                   SU R                  R                   S3nSU S3S	/nGO_[        U R                  R
                  5      S
S[        U R                  R                  5      /n[        USSS9n[        UR                  R                  5       5      nUR                  nUS:X  a9  U R                  R
                   SU R                  R                   S3nSU S3S	/nOUS:X  a~  / nUR                  5       R                  5        HY  n	U	R                  5       R                  SS5      u  pU
S:X  a  UR                  SU 35        MA  UR                  SU
 SU S35        M[     O[!        SU SU S35      eUR#                  5         US/UQSP-  nUR                  SR%                  [&        R(                  " U R                  S5      5      5      5        SR%                  U5      $ )Nz	@ECHO OFFz%:: Script generated by conda/menuinst
precommandr   z shell activate ""z%@FOR /F "usebackq tokens=*" %%i IN (`z`) do set "ACTIVATOR=%%i"z@CALL %ACTIVATOR%zshell.cmd.exeTF)r   r   z.batz shell.cmd.exe activate "z.env=r   _CONDA_SCRIPTz@CALL z@SET "z2Menuinst cannot parse activation scripts of type 'z': ''z @SETLOCAL ENABLEDELAYEDEXPANSIONz!:: This below is the user commandr   commandz
)r   r   r   r   _is_micromamba	conda_exer]   r\   r   r   stdoutstripr   	read_text
splitlinessplitNotImplementedErrorr   r   r   
quote_args)r&   linesr   	activatoractivation_linesactivator_cmdactivator_runactivation_filefiletypelinekeywordrr   s               r'   _commandWindowsMenuItem._command  sg   3
 __\2
LL$==$$yy''		(;(;<<#yy2233DTYYEUEUDVVWX	;I;F_`'$  		++,#		(()	! !+=% P"&}';';'A'A'C"D*11v%99..//HIYIYHZZ[\  @	{Jcd+($ '')$ / 9 9 ; F F H)-););C)C"o5,33fUG4DE,33fWIQugQ4OP !I .LXJVZ[jZkklm   &&(2! 4 E 	SXXf//	0JKLM{{5!!r*   Nc                   Uc  U R                  5       nO[        U5      nUR                  R                  SSS9  [	        US5       nUR                  U R                  5       5        SSS5        U$ ! , (       d  f       U$ = f)zL
This method generates the batch script that will be called by the shortcut
NTr   r   )r   r   parentr"   openr   r   )r&   script_pathfs      r'   _write_scriptWindowsMenuItem._write_scriptE  sw     //1K{+K   =+s#qGGDMMO$ $  $# s    A00
A?c                &   U R                   S   (       a  U R                  5       nU R                   S   (       a$  SSSSU S3/nU(       a  UR                  S5        U$ [        [        R
                  R                  SS	5      5      S
-  nU(       a  SOSnSUS-   S3SSSSSSUS-  S-  S-   S3SSSU SU S3/
nU$ U R                  S5      nU(       a(  [        S U 5       5      (       a  UR                  S5        [        R                  " U5      $ )Nr   terminalr   /Dz/Kr   %1r   r   r   z%1 r   zcmd.exe/CSTARTz/MINz""r   r   r   z-WindowStylehiddenr   z' z-WindowStyle hidden"r   c              3  ,   #    U  H
  nS U;  v   M     g7f)r   Nr{   )r   args     r'   r   3WindowsMenuItem._process_command.<locals>.<genexpr>p  s     @T_s   )r   r   r   r   rf   rg   rh   r   allr   r   )r&   	with_arg1scriptr   r   arg1s         r'   r    WindowsMenuItem._process_commandT  s$   ==$'')F}}Z( $&m<NN4(& N#  

|] KLzY )ur
 9,-Q/#66?BRRSSTU"xr$/DE N//),@@@@NN4   ))r*   c                *   U R                   R                  S5      (       a  UR                  R                  5       (       d  gU R	                  S5      nUR                  5       (       a$  [
        R                  " UR                  5       5      O0 nSn[        UR                  S0 5      R                  S/ 5      5       H  u  pgUR                  S5      U:X  d  M  Un  O   U(       a  US:  a  gUS   S   U	 OSR                  [        R                  " U R	                  S	5      5      5      US
.nU R                   R                  S5      (       a  U R	                  S5      US'   U R                   R                  S5      (       a  U R	                  S5      US'   US:  a4  SU;  a  0 US'   SUS   ;  a  / US   S'   US   S   R                  U5        O#[        R                  SU S35        XS   S   U'   UR                  [
        R                   " USS95        g)a  Add/remove the Windows Terminal profile.

Windows Terminal is using the name of the profile to create a GUID,
so the name will be used as the unique identifier to find existing profiles.

If the Terminal app has never been opened, the settings file may not exist yet.
Writing a minimal profile file will not break the application - Terminal will
automatically generate the missing options and profiles without overwriting
the profiles menuinst has created.
terminal_profileNprofileslistr=   r   r   r   )commandliner=   r   r   startingDirectoryz!Overwriting terminal profile for r      )indent)r   rh   r   r.   r   jsonloadsr   	enumerater   r   r   r   r   rI   
write_textdumps)	r&   r   r6   r=   settingsindexpprofileprofile_datas	            r'   r   4WindowsMenuItem._add_remove_windows_terminal_profilet  s    }}  !344HOO<R<R<T<T127?7H7H4::h0023b#HLLR$@$D$DVR$PQJA{{6"d* R
 qy$V,U3  #xx(9(9$//):T(UVL }}  (('+v'>V$}}  //48OOM4R01qyX-+-HZ(*!5535HZ(0$V,33LA?vQGH6B$V,U3DJJx:;r*   c                0    U R                  SSS9nU SU 3$ )Nr=   Trl   z
.AssocFiler   )r&   	extension
identifiers      r'   _ftype_identifier!WindowsMenuItem._ftype_identifier  s%    __V$_7
Z	{33r*   c           	     f   U R                   S   nU(       d  gSR                  U R                  5       5      n[        [        R                  U Vs/ s H  o3R                  5       PM     sn5      5      nU H4  nU R                  U5      nU R                  XR5        U R                  X5S9  M6     gs  snf )8
This function uses CMD's `assoc` and `ftype` commands.
file_extensionsNr   )associate_to)
r   r   r   r  dictfromkeyslowerr  
_cmd_ftype
_cmd_assoc)r&   
extensionsr   r   extsr  s         r'   _register_file_extensions_cmd-WindowsMenuItem._register_file_extensions_cmd  s     ]]#45
((40023DMM*"E*399;*"EFGC//4JOOJ0OOCO9  #Fs   B.c           	     
   U R                   S   nU(       d  g[        [        R                  U Vs/ s H  o"R	                  5       PM     sn5      5      nU H%  nU R                  U5      nU R                  U5        M'     gs  snf )r  r   N)r   r  r"  r#  r$  r  r%  r&   r'  r   r(  r  s        r'   _unregister_file_extensions_cmd/WindowsMenuItem._unregister_file_extensions_cmd  sk     ]]#45
DMM*"E*399;*"EFGC//4JOOJ'  #Fs   B c                    [        XU/5      S:w  a  [        S5      eU R                  S5      (       d  [        S5      eU(       a  U  SU 3nOU(       a  U nOU(       a  U  S3n[        SSSS	W 3/S
S9$ )zVhttps://learn.microsoft.com/en-us/windows-server/administration/windows-commands/assocr   z6Only one of {associate_to, query, remove} must be set.r   zextension must startwith '.'r   r   r   r   assoc Tr   )sum
ValueError
startswithr   )r  r!  queryr6   r   s        r'   r&  WindowsMenuItem._cmd_assoc  s    
 V,-2UVV##C((;<<Kq/CCKq/C5$sen=TJJr*   c                    [        XU/5      S:w  a  [        S5      eU(       a  U  SU 3nOU(       a  U nOU(       a  U  S3n[        SSSSW 3/SS	9$ )
zVhttps://learn.microsoft.com/en-us/windows-server/administration/windows-commands/ftyper   z1Only one of {command, query, remove} must be set.r   r   r   r   r0  Tr   )r1  r2  r   )r  r   r4  r6   r   s        r'   r%  WindowsMenuItem._cmd_ftype  sh    
 '(A-PQQL'+CCL"C5$sen=TJJr*   c                   U R                   S   nU(       d  gSR                  U R                  SS95      nU R                  S5      n[	        [
        R                  U Vs/ s H  oDR                  5       PM     sn5      5      nU HT  nU R                  U5      n[        UUUUU R                  S5      U R                  5       U R                  R                  S9  MV     gs  snf )	WIPr   Fr   Tr   r   r=   r   app_nameapp_user_model_idr;   )r   r   r   r   r  r"  r#  r$  r  r   r   r   r;   )r&   r'  r   r   r   r(  r  s          r'   r   )WindowsMenuItem._register_file_extensions  s    ]]#45
((40040@Av&DMM*"E*399;*"EFGC//4J#0"&"9"9";YY^^   #Fs   Cc           	     $   U R                   S   nU(       d  g[        [        R                  U Vs/ s H  o"R	                  5       PM     sn5      5      nU H2  nU R                  U5      n[        X$U R                  R                  S9  M4     gs  snf )Nr   Fr;   T)	r   r  r"  r#  r$  r  r   r   r;   r,  s        r'   r   +WindowsMenuItem._unregister_file_extensions  st    ]]#45
DMM*"E*399;*"EFGC//4J%cDIINNK  	 #Fs   Bc                D   U R                   S   nU(       d  gSR                  U R                  SS95      nU R                  S5      nU HT  nU R	                  U5      n[        UUUUU R                  S5      U R                  5       U R                  R                  S9  MV     g)	z~See https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85)url_protocolsFr   Tr:  r   r=   r;  )	r   r   r   r   r  r   r   r   r;   )r&   	protocolsr   r   protocolr  s         r'   r   'WindowsMenuItem._register_url_protocols  s    MM/2	((40040@Av&!H//9J!0"&"9"9";YY^^ " r*   c                    U R                   S   nU(       d  gU H2  nU R                  U5      n[        X#U R                  R                  S9  M4     g)NrC  Fr@  T)r   r  r   r   r;   )r&   rD  rE  r  s       r'   r   )WindowsMenuItem._unregister_url_protocols  sG    MM/2	!H//9J#Htyy~~N " r*   c                    U R                  S5      nU(       d&  SU R                  SSS9R                  SS5       3S S $ U$ )	Nr=  z	Menuinst.r=   Tr  r   r      )r   rn   )r&   aumis     r'   r   "WindowsMenuItem._app_user_model_id   sJ    23tvDAII#rRSTUYVYZZr*   r{   r}   )r|   ztuple[Path, ...])lnk)r   r\   )r|   r\   rx   )r   zos.PathLike | Noner|   r   )F)r   r~   r|   z
tuple[str])r   r   r6   r~   )NFF)
r  r\   r!  
str | Noner4  r~   r6   r~   r|   r   )r   rN  r4  r~   r6   r~   r|   r   )r|   r~   )r   r   r   r   r   r   r(   r6   ry   r   r   r   r   r   r   r   r  r)  r-  staticmethodr&  r%  r   r   r   r   r   r   r{   r*   r'   r   r      s    I I1f $;Y;"z*@-<^4:
( ]bKK&0K@DKVZK	K K  TYK'K7;KMQK	K K,	(r*   r   )&__doc__
__future__r   r  rf   r2   rB   loggingr   pathlibr   
subprocessr   tempfiler   typingr	   utilsr   r   r   baser   r   win_utils.knownfoldersr   r:   r   win_utils.registryr   r   r   r   r   r   r   r   r   r{   r*   r'   <module>r[     sh     "  	     ' '  . .   F C  s+$ s+lPh Pr*   