
    biט                       U d 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
mZ ddlmZ ddlmZ ddlmZmZ ddlZej(                  j*                  Zej.                  dk(  s*ej.                  d	k(  rej0                  j3                  d
      sdZndZ eedd      Zed=d>d       Zed?d       Zd=d@dZdAdZdBdZi Z de!d<   dCdZ"dAdZ#dDdZ$dBdZ%dBdZ&dBdZ' ed      Z(de!d<   dZ)d e!d!<   ejT                  d"k(  re(rejV                  jY                  d#d      Z) e       r\ejT                  d"k(  re(s e- eej0                  d$d            gZ.nk ej^                  edd%&      ja                  ejb                        Z.n<ejT                  d"k(  r)e)r ee)d      gZ.n  e- eej0                  d$d            gZ.nd'd(gZ. e- eej0                  d$d            gZ2d)e!d*<   dEd+Z3 e- eej0                  d,d            gZ4d)e!d-<    e       rFejT                  d"k(  re(se4dd Z5n[ ejl                  edd%&      ja                  ejb                        Z5n,ejT                  d"k(  re)r e- ee)d            gZ5n
e4dd Z5nd.d/gZ5dFd0Z7dCd1Z8dGdHd2Z9dGdHd3Z:ej.                  dk(  re9Z;ne:Z;dIdJd4Z<dKd5Z=dKd6Z>dLd7Z? ej                  d8d9      j                         d:v ZBed=dMd;       ZCdNd<ZDy)OzPath utility functions.    )annotationsN)Iterator)contextmanager)Path)Anyoverloadwin32darwinz/opt/homebrewJupyterjupyter	UF_HIDDENi   Fboolc                     y N namedefaults     M/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jupyter_core/paths.pyenvsetr   (   s    69    c                     y r   r   r   s     r   r   r   ,   s    58r   c                p    | t         j                  vr|S t         j                  |    j                         dvS )a%  Return the boolean value of a given environment variable.

    An environment variable is considered set if it is assigned to a value
    other than 'no', 'n', 'false', 'off', '0', or '0.0' (case insensitive)

    If the environment variable is not defined, the default value is returned.
    )nonfalseoff0z0.0)osenvironlowerr   s     r   r   r   0   s3     2::::d!!#+RRRr   c                     t        dd      S )zcDetermine if platformdirs should be used for system-specific paths.

    The default is False.
    JUPYTER_PLATFORM_DIRSF)r   r   r   r   use_platform_dirsr$   >   s    
 )511r   c                 x    t        d      j                         } t        t        |       j                               S )z'Get the real path of the home directory~)r   
expanduserstrresolve)homedirs    r   get_home_dirr+   F   s.    3i""$G tG}$$&''r   zdict[str, str]_dtempsc                *   t        |       j                         }|j                         s;||j                  k7  r,|j                  }|j                         s||j                  k7  r,	 |j	                         t        j                         k(  S # t        $ r Y nw xY wt        t
        d      rG	 |j                         }|j                  t        j                         k(  S # t        t        f$ r Y nw xY wt        j                  |t
        j                        S )z3Return whether the current user owns the given pathgeteuid)r   r)   existsparentownerr   getlogin	Exceptionhasattrstatst_uidr.   NotImplementedErrorOSErroraccessW_OK)pathpsts      r   	_do_i_ownr>   Q   s    T
A hhjQ!((]HH hhjQ!((]
wwyBKKM))  r9	B99

,,#W- 		
 99Q  s$   &$B 	BB+0C C.-C.c                    dt         j                  v rt        d      S t        j                  t        j
                  k7  rt        t        j                        rydt         j                  v rmt        j                  j                  t         j                  d         r=t         j                  j                  dd      dk7  rt        t        j                        ryy)zRDetermine if environment-level paths should take precedence over user-level paths.JUPYTER_PREFER_ENV_PATHTCONDA_PREFIXCONDA_DEFAULT_ENVbaseF)	r   r    r   sysprefixbase_prefixr>   
startswithgetr   r   r   prefer_environment_over_userrI   l   s     !BJJ./00 zzS__$3::)> 	"**$JJ!!"**^"<=JJNN.76Acjj!r   c                |    	 t         |    S # t        $ r' t        j                  | dz         x}t         | <   |cY S w xY w)zMake or reuse a temporary directory.

    If this is called with the same name in the same process, it will return
    the same directory.
    -)rE   )r,   KeyErrortempfilemkdtemp)r   ds     r   _mkdtemp_oncerP      sB    t} $,,D3J??GDMs    -;;c                     t         j                  } | j                  d      rt        d      S | j                  d      r| d   S t	               rt        j                  t        d      S t               }t        |d      S )zGet the Jupyter config directory for this platform and user.

    Returns JUPYTER_CONFIG_DIR if defined, otherwise the appropriate
    directory for the platform.
    JUPYTER_NO_CONFIGzjupyter-clean-cfgJUPYTER_CONFIG_DIRF	appauthorz.jupyter)
r   r    rH   rP   r$   platformdirsuser_config_dirAPPNAMEr+   pjoin)envhome_dirs     r   jupyter_config_dirr\      sl     **C
ww"#011
ww#$'((++GuEE~H:&&r   c                 0   t         j                  } | j                  d      r| d   S t               rt	        j
                  t        d      S t               }t        j                  dk(  rt        t        |dd            S t        j                  dk(  rYt         j                  j                  dd	      }|r#t        t        |d
      j                               S t        t               d      S | j                  dd	      }|st        |dd      }t        |d
      S )zGet the config directory for Jupyter data files for this platform and user.

    These are non-transient, non-configuration files.

    Returns JUPYTER_DATA_DIR if defined, else a platform-appropriate path.
    JUPYTER_DATA_DIRFrT   r
   Libraryr   r	   APPDATANr   dataXDG_DATA_HOMEz.localshare)r   r    rH   r$   rV   user_data_dirrX   r+   rD   platformr(   r   r)   rY   r\   )rZ   homeappdataxdgs       r   jupyter_data_dirri      s     **C
ww!"%&&))'UCC>D
||x4i344
||w**..D1tGY/779::')622 ''/4
(CD(G,i  r   c                 v    t         j                  } | j                  d      r| d   S t        t	               d      S )zReturn the runtime dir for transient jupyter files.

    Returns JUPYTER_RUNTIME_DIR if defined.

    The default is now (data_dir)/runtime on all platforms;
    we no longer use XDG_RUNTIME_DIR after various problems.
    JUPYTER_RUNTIME_DIRruntime)r   r    rH   rY   ri   )rZ   s    r   jupyter_runtime_dirrm      s7     **C
ww$%())!#Y//r   JUPYTER_USE_PROGRAMDATA_use_programdataz
str | None_win_programdatantPROGRAMDATArc   T)rU   	multipathz/usr/local/share/jupyterz/usr/share/jupyter	list[str]ENV_JUPYTER_PATHc                 V   g }t         j                  j                  d      rF|j                  d t         j                  d   j	                  t         j
                        D               t               g}t        j                  rat        t        d      rt        j                         nt        j                  }|r+t        t        |dd            }||vr|j                  |       t        t         k(  rt        }nt        D cg c]  }|t         vs| }}t#               r#|j                  |       |j                  |       n"|j                  |       |j                  |       t         D ]  }||vs|j                  |        | r|D cg c]  }t%        |g|   }}|S c c}w c c}w )ay  Return a list of directories to search for data files.

    There are four sources of paths to search:

    - $JUPYTER_PATH environment variable (always highest priority)
    - user directories (e.g. ~/.local/share/jupyter)
    - environment directories (e.g. {sys.prefix}/share/jupyter)
    - system-wide paths (e.g. /usr/local/share/jupyter)

    JUPYTER_PATH environment variable has highest priority, if defined,
    and is purely additive.

    If the JUPYTER_PREFER_ENV_PATH environment variable is set, the environment-level
    directories will have priority over user-level directories.
    You can also set JUPYTER_PREFER_ENV_PATH=0 to explicitly prefer user directories.
    If Jupyter detects that you are in a virtualenv or conda environment,
    environment paths are also preferred to user paths,
    otherwise user paths are preferred to environment paths.

    If the Python site.ENABLE_USER_SITE variable is True, we also add the
    appropriate Python user site subdirectory to the user-level directories.

    Finally, system-wide directories, such as `/usr/local/share/jupyter` are searched.

    If ``*subdirs`` are given, that subdirectory will be added to each element.


    .. versionchanged:: 5.8

        On Windows, %PROGRAMDATA% will be used as a system-wide path only if
        the JUPYTER_USE_PROGRAMDATA env is set.
        By default, there is no default system-wide path on Windows and the env path
        is used instead.

    Examples:

    >>> jupyter_path()
    ['~/.local/jupyter', '/usr/local/share/jupyter']
    >>> jupyter_path('kernels')
    ['~/.local/jupyter/kernels', '/usr/local/share/jupyter/kernels']
    JUPYTER_PATHc              3  Z   K   | ]#  }|j                  t        j                         % y wr   rstripr   sep.0r<   s     r   	<genexpr>zjupyter_path.<locals>.<genexpr>%  s     \!QXXbff%\   )+getuserbaserc   r   )r   r    rH   extendsplitpathsepri   siteENABLE_USER_SITEr4   r   	USER_BASEr(   r   appendru   SYSTEM_JUPYTER_PATHrI   rY   )subdirspathsuseruserbaseuserdirrZ   r<   _paths           r   jupyter_pathr      s`   V E 
zz~~n%\rzz./I/O/OPRPZPZ/[\\ D *1})E4##%4>>$x)<=Gd"G$
 ..*KQa7J.JqKK#%STTS %  LL 
 -23q#7#33L# L  4s    F!F!
F&etcENV_CONFIG_PATHz/usr/local/etc/jupyterz/etc/jupyterc                 l   t         j                  j                  d      rt               gS g } t         j                  j                  d      rF| j	                  d t         j                  d   j                  t         j                        D               t               g}t        j                  rat        t        d      rt        j                         nt        j                  }|r+t        t        |dd            }||vr|j                  |       t        t         k(  rt        }nt        D cg c]  }|t         vs| }}t#               r#| j	                  |       | j	                  |       n"| j	                  |       | j	                  |       t        t         k7  r| j	                  t                | S c c}w )a+  Return the search path for Jupyter config files as a list.

    If the JUPYTER_PREFER_ENV_PATH environment variable is set, the
    environment-level directories will have priority over user-level
    directories.

    If the Python site.ENABLE_USER_SITE variable is True, we also add the
    appropriate Python user site subdirectory to the user-level directories.

    Finally, system-wide directories such as `/usr/local/etc/jupyter` are searched.


    .. versionchanged:: 5.8

        On Windows, %PROGRAMDATA% will be used as a system-wide path only if
        the JUPYTER_USE_PROGRAMDATA env is set.
        By default, there is no system-wide config path on Windows.

    Examples:

    >>> jupyter_config_path()
    ['~/.jupyter', '~/.local/etc/jupyter', '/usr/local/etc/jupyter', '/etc/jupyter']

    rR   JUPYTER_CONFIG_PATHc              3  Z   K   | ]#  }|j                  t        j                         % y wr   ry   r|   s     r   r~   z&jupyter_config_path.<locals>.<genexpr>  s     c!QXXbff%cr   r   r   r   )r   r    rH   r\   r   r   r   r   r   r4   r   r   r(   r   r   r   SYSTEM_CONFIG_PATHrI   )r   r   r   r   rZ   r<   s         r   jupyter_config_pathr   g  sJ   2 
zz~~)*"$%%E 
zz~~+,crzz:O/P/V/VWYWaWa/bcc  !D *1})E4##%4>>$x	:;Gd"G$
 ,,)IQQ6H-HqII#%STTS ,,'(L Js   *F18F1c                N    	 t        j                  |        y# t        $ r Y yw xY w)zcReplacement for `os.path.exists` which works for host mapped volumes
    on Windows containers
    FT)r   lstatr8   )r;   s    r   r/   r/     s+    
   s    	$$c                ~   t        |       } | j                  j                  d      ry|	 t        |       j                         }	 |j                  t        j                  z  ry	 y# t        $ r(}|j
                  t
        j                  k(  rY d}~y d}~ww xY w# t        $ r t        j                  dd       Y yw xY w)  Is a file hidden?

    This only checks the file itself; it should be called in combination with
    checking the directory containing the file.

    Use is_hidden() instead to check the file and its parent directories.

    Parameters
    ----------
    abs_path : unicode
        The absolute path to check.
    stat_res : os.stat_result, optional
        The result of calling stat() on abs_path. If not passed, this function
        will call stat() internally.
    .TNFzThidden files are not detectable on this system, so no file will be marked as hidden.   
stacklevel)r   r   rG   r5   r8   errnoENOENTst_file_attributesFILE_ATTRIBUTE_HIDDENAttributeErrorwarningswarnabs_pathstat_reses      r   is_file_hidden_winr     s      H~H}}$	H~**,H
''(() ) )  	ww%,,&	  
 	b	

 
s/   A% B %	B.BBB B<;B<c                   t        |       } | j                  j                  d      ry|t        j                  |j
                        r	 | j                         }t        j                  |j
                        r6t        j                  | t        j                  t        j                  z        syt        |dd      t        z  ryy# t        $ r(}|j                  t        j                  k(  rY d}~y d}~ww xY w)r   r   TNFst_flagsr   )r   r   rG   r5   S_ISLNKst_moder8   r   r   S_ISDIRr   r9   X_OKR_OKgetattrr   r   s      r   is_file_hidden_posixr     s      H~H}}$4<<(8(89	}}H ||H$$%yy277RWW#45 xQ')3  	ww%,,&	s   
C 	C6C10C11C6c                   t        t        j                  j                  |             } |r)t        t        j                  j                  |            }nt	        | j
                        d   }| |k(  ry| j                         sd| d}t        |      |j                         sd|d}t        |      | j                  |      sd| d|d}t        |      t        |       ry	| j                  |      }t        d
 |j                  D              ry	| j
                  D ]A  }|j                         s||k(  r y	 |j                         }t!        |dd      t"        z  sA y	 y# t        $ r Y  y	w xY w)a  Is a file hidden or contained in a hidden directory?

    This will start with the rightmost path element and work backwards to the
    given root to see if a path is hidden or in a hidden directory. Hidden is
    determined by either name starting with '.' or the UF_HIDDEN flag as
    reported by stat.

    If abs_path is the same directory as abs_root, it will be visible even if
    that is a hidden folder. This only checks the visibility of files
    and directories *within* abs_root.

    Parameters
    ----------
    abs_path : str or Path
        The absolute path to check for hidden directories.
    abs_root : str or Path
        The absolute path of the root directory in which hidden directories
        should be checked for.
    Fz	abs_path=z, is not absolute. abs_path must be absolute.z	abs_root=z, is not absolute. abs_root must be absolute.z# is not a subdirectory of abs_root=z#. abs_path must be within abs_root.Tc              3  >   K   | ]  }|j                  d         yw)r   N)rG   )r}   parts     r   r~   zis_hidden.<locals>.<genexpr>C  s     
@D4??3
@s   r   r   )r   r   r;   normpathlistparentsis_absolute
ValueErroris_relative_tois_file_hiddenrelative_toanypartsr/   r   r8   r   r   )r   abs_root_msgrelative_pathr0   r=   s         r   	is_hiddenr     s   ( BGG$$X./H((23(()"-8 !(HI!(HI""8,xk=H;>ab 	 h((2M

@M,?,?
@@ "" }}X 	B 2z1%	1   		s   ;E$$	E10E1c                n   	 ddl }ddl}ddl}|j                  |j                        }|j                  d|j                  |j                              \  }}}|j                  | |j                        }|j                         }	|	j                  |j                  |j                  |j                   z  |j"                  z  |       |	j                  |j                  |j$                  |       |j'                  d|	d       |j)                  | |j                  |       y# t        $ r t        |       cY S w xY w)a  Secure a windows file to read-only access for the user.
    Follows guidance from win32 library creator:
    http://timgolden.me.uk/python/win32_how_do_i/add-security-to-a-file.html

    This method should be executed against an already generated file which
    has no secrets written to it yet.

    Parameters
    ----------

    fname : unicode
        The path to the file to secure
    r   N    )win32apiImportError#_win32_restrict_file_to_user_ctypesntsecurityconwin32securityCreateWellKnownSidWinBuiltinAdministratorsSidLookupAccountNameGetUserNameExNameSamCompatibleGetFileSecurityDACL_SECURITY_INFORMATIONACLAddAccessAllowedAceACL_REVISIONFILE_GENERIC_READFILE_GENERIC_WRITEDELETEFILE_ALL_ACCESSSetSecurityDescriptorDaclSetFileSecurity)
fnamer   conr   adminsr   _domain_typesddacls
             r   win32_restrict_file_to_userr   X  s    :   --m.W.WXF(::
H""8#=#=>D'5 
	&	&um.U.U	VBD"" 6 66C
 	]779L9LfU  D!,!!%)P)PRTU3  :2599:s   D D43D4c                
  %&'()*+,- ddl +ddl m- +j                  dd      *+j                  dd      ,d}d	}d
}d%d&d'd}d}d}d}d}d}	d}
d}d}d
}d}d}||z  dz  }||	z  |z  |
z  |z  }||z  |z  |z  |z  |z  } G -fdd+j                        }+j                  )+j                  |      (+j                  -j                        }d%+fd}|,j                  _        -j                  ,j                  _
        +j                  -j                  -j                  f,j                  _        |*j                  _        -j                  *j                  _
        -j                   ))-j"                  f*j                  _        |*j$                  _        -j                  *j$                  _
        -j                  -j                  )-j&                  -j                  -j&                  -j&                  f*j$                  _        |*j(                  _        -j                  *j(                  _
        (-j                   -j                   )f*j(                  _        |*j*                  _        -j                  *j*                  _
        |-j                  (-j                  f*j*                  _        |*j,                  _        -j                  *j,                  _
        -j.                  -j                   |-j                   -j&                  f*j,                  _        |*j0                  _        -j                  *j0                  _
        -j.                  -j                   |f*j0                  _        |*j2                  _        -j                  *j2                  _
        ||-j&                  (-j&                  (-j&                  )-j&                  )-j&                  f*j2                  _        |*j4                  _        -j                  *j4                  _
        ||-j&                  f*j4                  _        |*j6                  _        -j                  *j6                  _
        (-j                   -j                   f*j6                  _        d&&*+-fd}d''+,fd}d(&*+-fd}d)*fd}d*&*+-fd}	 	 	 	 	 	 	 	 d+*fd}	 	 	 	 	 	 	 	 	 	 d,*fd }d-&()*+-fd!}d.&*+-fd"}d/%(*+fd#} ||      }  |d$ ||            d   }! |       }" ||"%||         ||"%||z  |z  |!        || |      }# ||#      }$ ||$d|"d        ||$      }# || ||#       y)0a  Secure a windows file to read-only access for the user.

    Follows guidance from win32 library creator:
    http://timgolden.me.uk/python/win32_how_do_i/add-security-to-a-file.html

    This method should be executed against an already generated file which
    has no secrets written to it yet.

    Parameters
    ----------

    fname : unicode
        The path to the file to secure
    r   Nwintypesadvapi32T)use_last_errorsecur32r         z      i   i   i   i   r               i  c                      e Zd ZdW  j                  fdW  j                  fdW  j                  fdW  j                  fdW  j                  fgZy)0_win32_restrict_file_to_user_ctypes.<locals>.ACLAclRevisionSbz1AclSizeAceCountSbz2N)__name__
__module____qualname__BYTEWORD_fields_r   s   r   r   r     sF    HMM*X]]#&'X]]#
r   r   c                J    | sj                  j                               |S r   )WinErrorget_last_error)resultfuncargsctypess      r   _nonzero_successz=_win32_restrict_file_to_user_ctypes.<locals>._nonzero_success  s#    //&"7"7"9::r   c           	     l    j                   dz         }j                         }	 j                  | d |j                  |             |d d  S # t        $ r[}|j
                  k7  r  j                   |j                  z         }j                  | d |j                  |             Y d }~dd }~ww xY w)Nr   )c_charDWORDr   byrefr8   winerrorvalue)WellKnownSidTypepSidcbSidr   ERROR_INSUFFICIENT_BUFFERr   r  r   s       r   r   z?_win32_restrict_file_to_user_ctypes.<locals>.CreateWellKnownSid,  s    !!$ 	[''(8$fllSXFYZ Aw  	[zz66/FMMEKK/2D''(8$fllSXFYZZ		[s   #A 	B3AB..B3c                n   j                  j                  d            }	 j                  | d |       |j
                  j                  sy j                  |j
                  j                        }j                  | ||       |j                  S # t        $ r}|j                  k7  r Y d }~yd }~ww xY wNr   )pointerc_ulongGetUserNameExWr8   r  contentsr  create_unicode_buffer)
NameFormatnSizer   lpNameBufferERROR_MORE_DATAr  r   s       r   r   z:_win32_restrict_file_to_user_ctypes.<locals>.GetUserNameEx9  s     v~~a01	"":tU; ~~##33ENN4H4HIz<?!!!  	zz_, -	s   B 	B4B//B4c                   j                  d      }j                  d      }j                  d      }	 j                  | |d j                  |      d j                  |      j                  |             j                  d|j                        }j                  j                  |      j                        }j                  d|j                  dz         }j                  | ||j                  |      |j                  |      j                  |            }	|	sj                         ||j                  |j                  fS # t        $ r}|j                  
k7  r Y d }~d }~ww xY w)Nr   r   r   )r  LookupAccountNameWr  r8   r  r  r  castr  LPVOIDr  )lpSystemNamelpAccountNamer  cchReferencedDomainNamepeUser   Sidr  lpReferencedDomainNamesuccessr  r   r  r   s             r   r   z>_win32_restrict_file_to_user_ctypes.<locals>.LookupAccountNameH  s[    q!"*.."3q!	''U#45U# **2u{{;{{6>>#.@!'!=!=bBYB_B_bcBc!d--LL"LL01LL
 //##+115;;>>#  	zz66 7	s   AE 	E2E--E2c                .    j                  | |||       y r   )r   )pAcldwAceRevision
AccessMaskr  r   s       r   r   z@_win32_restrict_file_to_user_ctypes.<locals>.AddAccessAllowedAcek  s     	$$T=*dKr   c           	     ^   j                  d      }	 j                  | |d dj                  |             |j
                  sy  j                  |j
                  z         }j                  | |||j                  |             |S # t        $ r}|j                  k7  r Y d }~od }~ww xY wr  )r  GetFileSecurityWr  r8   r  r  r   )	
lpFileNameRequestedInformationnLengthr   pSecurityDescriptorr  r   r  r   s	        r   r   z<_win32_restrict_file_to_user_ctypes.<locals>.GetFileSecurityp  s    ..#
	%%$W% }}<x}}w}}<?!! LL!	
 #"  	zz66 7	s   $B	 		B,B''B,c                ,    j                  | ||       y r   )SetFileSecurityW)r/  r0  r2  r   s      r   r   z<_win32_restrict_file_to_user_ctypes.<locals>.SetFileSecurity  s     	!!*.BDWXr   c                .    j                  | |||       y r   )r   )r2  bDaclPresentpDaclbDaclDefaultedr   s       r   r   zF_win32_restrict_file_to_user_ctypes.<locals>.SetSecurityDescriptorDacl  s     	**+>eUcdr   c                t   d }j                  d      }d }j                  d      }d }j                  d      }d }j                  d      }d }	j                  d      }
	 j                  | |j                  |      |j                  |      |j                  |      |j                  |      |	j                  |
              j
                  |j                  z         } j
                  |j                  z         }j                  |      j                  } j
                  |j                  z         }j                  |      j                  } j
                  |j                  z         }j                  |      } j
                  |
j                  z         }j                  |      }	j                  | |j                  |      |j                  |      |j                  |      |||	j                  |
             |S # t        $ r}|j                  k7  r Y d }~jd }~ww xY wr  )	r  MakeAbsoluteSDr  r8   r  r   r  r   r  )pSelfRelativeSecurityDescriptorpAbsoluteSecurityDescriptor"lpdwAbsoluteSecurityDescriptorSizer7  lpdwDaclSizepSacllpdwSaclSizepOwnerlpdwOwnerSizepPrimaryGrouplpdwPrimaryGroupSizer   	pDaclData	pSaclData
pOwnerDatapPrimaryGroupDatar  PACLPSIDr   r  r   s                   r   r:  z;_win32_restrict_file_to_user_ctypes.<locals>.MakeAbsoluteSD  s    '+#-5^^A->*~~a(~~a( q)'~~a0	##/+?@\*\*]+12  (`x}}7Y7_7_'_&b#7X]]\%7%77:	It,557X]]\%7%77:	It,559hmmm&9&99<
Z.GX]]-A-G-GGJ$5t<+'LL;<LL&LL&LL-.	
 +*3  	zz66 7	s   "A&H 	H7H22H7c                @   d }j                  d      }	 j                  | |j                  |              j
                  |j                  z         }j                  | |j                  |             |S # t        $ r}|j                  k7  r Y d }~`d }~ww xY wr  )r  MakeSelfRelativeSDr  r8   r  r   r  )r<  r;  lpdwBufferLengthr   r  r   r  r   s       r   rL  z?_win32_restrict_file_to_user_ctypes.<locals>.MakeSelfRelativeSD  s     +/'#>>!,	''+/-. ,R8==;K;Q;Q+Q*T'##'+LL)*	

 /.  	zz66 7	s   "A: :	BBBc                     d} j                  |       }j                  |      j                  }j                  ||        |S )Ni  )create_string_bufferr   r  InitializeAcl)
nAclLengthacl_datar*  r   rI  r   r  s      r   NewAclz3_win32_restrict_file_to_user_ctypes.<locals>.NewAcl  sE    
..z:{{8T*33tZ>r   r   )r  intr  r   r  r   returnr   )r  r   rU  r   )r  r   rU  r   )r"  r   r#  r   rU  r   )
r*  r   r+  r   r,  r   r  r   rU  r   )r/  r   r0  r   rU  r   )r/  r   r0  r   r2  r   rU  r   )
r2  r   r6  r   r7  r   r8  r   rU  r   )r;  r   rU  r   )r<  r   rU  r   )rU  r   )r  r   WinDLL	Structurec_void_pPOINTERr   r  errcheckBOOLrestypec_intLPWSTRPULONGargtypesr   r  PDWORDr  LPDWORDr   r   r.  LPCWSTRr4  r:  rL  rP  ).r   r   r   r   SYNCHRONIZEr   STANDARD_RIGHTS_REQUIREDSTANDARD_RIGHTS_READSTANDARD_RIGHTS_WRITEFILE_READ_DATAFILE_READ_EAFILE_READ_ATTRIBUTESFILE_WRITE_DATAFILE_APPEND_DATAFILE_WRITE_EAFILE_WRITE_ATTRIBUTESr   r   r   r   PSECURITY_DESCRIPTORr  r   r   r   r   r   r   r   r:  rL  rS  	SidAdminsSidUserAclSelfRelativeSD
AbsoluteSDr   r  r  rI  rJ  r   r  r   r   s.                                        @@@@@@@@@r   r   r     s    }}Z}=HmmIdm;G"$ !L #OKF&"#NLOM.<uDO~-0DD|SVaa  	
	
	  	 		
 	 
f 
 ??D>>#D!>>(--8
 '7G#%-]]G"'G# ,<H(*2--H'	,H( ,<H(*2--H',H( -=H  )+3==H  (	-H  ) 3CH&&/19H&&.	3H&&/ *:H&(0H%*H& *:H&(0H%*H& (8H$&.mmH#(H$ ,<H(*2--H',H( '7H#%-]]H"'H# "!? !?FL
# #4YY/2YILY	Ye e03e<?eQTe	e4+ 4+l/ /,  ##>?IM2C$DEaHG
(C\?IF..7	 %U,EFN/Jj!S!4'
3NE4nEr   c                t    t        j                  t        |       j                         j                        dz  S )zRetrieves the file mode corresponding to fname in a filesystem-tolerant manner.

    Parameters
    ----------

    fname : unicode
        The path to the file to get mode from

    i  )r5   S_IMODEr   r   )r   s    r   get_file_moderw    s-     	T%[%%'//069r   JUPYTER_ALLOW_INSECURE_WRITESr   )true1c              #    K   |rdnd}|rdnd}t         j                  t         j                  z  t         j                  z  }	 t	        |       j                          t         j                  dk(  rit        rt                nXt        j                  | |d      }t        j                  |       t         j                  t         j                  z  }t        |        t        j                  t        j                  | |d      ||      5 }t         j                  dk7  r>t        |       }|dk7  r.t        rt                nd|  d	t        |       d
}t!        |      | ddd       y# t        $ r Y w xY w# 1 sw Y   yxY ww)aD  Opens a file in the most restricted pattern available for
    writing content. This limits the file mode to `0o0600` and yields
    the resulting opened filed handle.

    Parameters
    ----------

    fname : unicode
        The path to the file to write

    binary: boolean
        Indicates that the file is binary
    wbwNzutf-8rq   i  )encodingz0Permissions assignment failed for secure file: 'z'. Got 'z' instead of '0o0600'.)r   O_CREATO_WRONLYO_TRUNCr   unlinkr8   r   allow_insecure_writesissue_insecure_write_warningopencloser   fdopenrw  octRuntimeError)	r   binarymoder~  	open_flagfdf	file_modemsgs	            r   secure_writer    sH     4sDt7H

R[[(2::5IU
 
ww$  )*
 	62BHHRLbjj0I'.	2775)V4dX	N RS77d?%e,IF"(02 K5' R!!$Y 00FH  's++ %  $ sC   ?E?E# B)E?AE3	E?#	E0,E?/E00E?3E<8E?c                 P    dd} | t         _        t        j                  dd       y)z Issue an insecure write warning.c                    t        |       dz   S )N
)r(   )r  r  kwargss      r   format_warningz4issue_insecure_write_warning.<locals>.format_warningM  s    3x$r   zWARNING: Insecure writes have been enabled via environment variable 'JUPYTER_ALLOW_INSECURE_WRITES'! If this is not intended, remove the variable or set its value to 'False'.r   r   N)r  r(   r  r   r  r   rU  r(   )r   formatwarningr   )r  s    r   r  r  J  s'     ,HMM	0 	r   )F)r   r(   r   r   rU  r   )r   r(   r   NonerU  bool | None)r   r(   r   r  rU  r  )rU  r   )rU  r(   )r;   r(   rU  r   )r   r(   rU  r(   )r   r(   rU  rt   )rU  rt   r   )r   
str | Pathr   z
Any | NonerU  r   )r   )r   r  r   r  rU  r   )r   r(   rU  r  )r   r(   rU  rT  )r   r(   r  r   rU  zIterator[Any])rU  r  )E__doc__
__future__r   r   r   r   r5   rD   rM   r   collections.abcr   
contextlibr   pathlibr   typingr   r   rV   r;   joinrY   re   rE   rG   rX   r   r   r   r$   r+   r,   __annotations__r>   rI   rP   r\   ri   rm   ro   rp   r   r    rH   r(   r   site_data_dirr   r   ru   r   r   r   site_config_dirr   r/   r   r   r   r   r   r   rw  getenvr!   r  r  r  r   r   r   <module>r     s'    #  	   
   $ %    
 <<7LLHSZZ%:%:?%KGG D+u-	 
 9 
 9 
 8 
 8S2(  !6,
'*!B0"   9: $ :# * #77d?'zz~~mT: 	ww$/"4

GY#GHI8l88u

%


 	
 
ww$#()99#E"F#&tCJJ'K#L"M ' 

  #4

GY#GHI ) IUp "$szz5)"DEF F	ww$/ -Q/9\99u

%


 	 WW_ !$'7"CDE,Q/ 	!BJ+\&R <<7'N)NAH)XwFt& "		"A7KQQSWdd  2 2jr   