o
    i                  	   @   s  U d 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mZmZmZmZ ddlZddlmZ ejjZejdv rNd	nd
ZeeddZdSdedee dee fddZdefddZdefddZ i Z!eeef e"d< dedefddZ#defddZ$dedefddZ%defdd Z&defd!d"Z'defd#d$Z(e rej)edd%d&*ej+Z,n(ed' ej-d(krej./d)dZ0e0ree0d
gZ,nejej1d*d
gZ,nd+d,gZ,ejej1d*d
gZ2ee e"d-< d.edee fd/d0Z3e rej4edd%d&*ej+Z5n ej-d(kr,ej./d)dZ0e0r)eje0d
gZ5ng Z5nd1d2gZ5ejej1d3d
gZ6ee e"d4< dee fd5d6Z7dedefd7d8Z8dTd9ed:ee defd;d<Z9dTd9ed:ee defd=d>Z:ejd?krwe9Z;ne:Z;dUd9edAedefdBdCZ<dDeddfdEdFZ=dDeddfdGdHZ>dDede?fdIdJZ@eAdKdLB dMv ZCe	dSdDedNedee fdOdPZDdVdQdRZEdS )WzPath utility functions.    N)contextmanager)Path)AnyDictIteratorListOptional   )deprecation)win32darwinJupyterjupyter	UF_HIDDENi   Fnamedefaultreturnc                 C   s    | t jvr|S t 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lower)r   r    r   M/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_core/paths.pyenvset$   s   
r   c                   C   s
   t ddS )zDetermine if platformdirs should be used for system-specific paths.

    We plan for this to default to False in jupyter_core version 5 and to True
    in jupyter_core version 6.
    JUPYTER_PLATFORM_DIRSF)r   r   r   r   r   use_platform_dirs2   s   
r   c                  C   s    t jd} tt|  } | S )z'Get the real path of the home directory~)r   path
expanduserstrr   resolve)homedirr   r   r   get_home_dir;   s   r&   _dtempsr!   c              	   C   s   t |  }| s||jkr|j}| s||jksz	| t kW S  ty-   Y nw ttdrKz|	 }|j
t kW S  ttfyJ   Y nw t|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)r!   pstr   r   r   	_do_i_ownG   s"   
r7   c                   C   sl   dt jv r	tdS tjtjkrttjrdS dt jv r4tjt jd r4t jdddkr4ttjr4dS dS )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_prefixr7   
startswithgetr   r   r   r   prefer_environment_over_userb   s   

rA   c                 C   s:   zt |  W S  ty   tj| d d }t | < | Y S 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.
    -)r=   )r'   KeyErrortempfilemkdtemp)r   dr   r   r   _mkdtemp_oncex   s   
rG   c                  C   sN   t j} | drtdS | dr| d S t rtjtd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   r@   rG   r   platformdirsuser_config_dirAPPNAMEr&   pjoin)envhome_dirr   r   r   jupyter_config_dir   s   


rR   c                  C   s   t j} | dr| d S t rtjtddS t }tj	dkr&t j
|ddS t jdkrCt jdd	}|r=tt|d
 S tt dS | dd	}|sQt|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_DIRFrJ   r   Libraryr   ntAPPDATANr   dataXDG_DATA_HOMEz.localshare)r   r   r@   r   rL   user_data_dirrN   r&   r<   platformr!   joinr   r#   r   r$   rO   rR   )rP   homeappdataxdgr   r   r   jupyter_data_dir   s"   



r`   c                  C   s$   t 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   r@   rO   r`   )rP   r   r   r   jupyter_runtime_dir   s   
rc   T)rK   	multipatha7  Jupyter is migrating its paths to use standard platformdirs
given by the platformdirs library.  To remove this warning and
see the appropriate new directories, set the environment variable
`JUPYTER_PLATFORM_DIRS=1` and then run `jupyter --paths`.
The use of platformdirs will be the default in `jupyter_core` v6rU   PROGRAMDATArY   z/usr/local/share/jupyterz/usr/share/jupyterENV_JUPYTER_PATHsubdirsc                     s   g }t jdr|dd t jd t jD  t g}tjr?t	tdr)t
 ntj}|r?t j|dd}||vr?|| dd tD }t rT|| || n
|| || |t  rn fd	d|D }|S )
a  Return a list of directories to search for data files

    JUPYTER_PATH environment variable has highest priority.

    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.


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

    Examples:

    >>> jupyter_path()
    ['~/.local/jupyter', '/usr/local/share/jupyter']
    >>> jupyter_path('kernels')
    ['~/.local/jupyter/kernels', '/usr/local/share/jupyter/kernels']
    JUPYTER_PATHc                 s       | ]	}| tjV  qd S Nrstripr   sep.0r5   r   r   r   	<genexpr>       zjupyter_path.<locals>.<genexpr>getuserbaserY   r   c                 S      g | ]}|t vr|qS r   )SYSTEM_JUPYTER_PATHrn   r   r   r   
<listcomp>      z jupyter_path.<locals>.<listcomp>c                    s   g | ]
}t |g R  qS r   )rO   rn   rg   r   r   ru     s    )r   r   r@   extendsplitpathsepr`   siteENABLE_USER_SITEr.   rr   	USER_BASEr!   r\   appendrf   rA   rt   )rg   pathsuseruserbaseuserdirrP   r   rw   r   jupyter_path   s(   "




r   z/usr/local/etc/jupyterz/etc/jupyteretcENV_CONFIG_PATHc                  C   s   t jdr
t gS g } t jdr#| dd t jd t jD  t g}tjrIt	tdr3t
 ntj}|rIt j|dd}||vrI|| dd	 tD }t r^| | | | n
| | | | | t | S )
ar  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.
    rH   JUPYTER_CONFIG_PATHc                 s   ri   rj   rk   rn   r   r   r   rp   F  rq   z&jupyter_config_path.<locals>.<genexpr>rr   r   r   c                 S   rs   r   )SYSTEM_CONFIG_PATHrn   r   r   r   ru   U  rv   z'jupyter_config_path.<locals>.<listcomp>)r   r   r@   rR   rx   ry   rz   r{   r|   r.   rr   r}   r!   r\   r~   r   rA   r   )r   r   r   r   rP   r   r   r   jupyter_config_path4  s(   
"




r   c                 C   s&   zt |  W dS  ty   Y dS w )zcReplacement for `os.path.exists` which works for host mapped volumes
    on Windows containers
    FT)r   lstatr2   )r!   r   r   r   r)   c  s   r)   abs_pathstat_resc              
   C   s   t j| drdS |du r1zt | }W n ty0 } z|jtjkr+W Y d}~dS  d}~ww z|jtj	@ r;W dS W dS  t
yL   td Y dS 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.)r   r!   basenamer?   r/   r2   errnoENOENTst_file_attributesFILE_ATTRIBUTE_HIDDENAttributeErrorwarningswarnr   r   er   r   r   is_file_hidden_winn  s.   r   c              
   C   s   t j| drdS |du st|jr7zt | }W n ty6 } z|jtj	kr1W Y d}~dS  d}~ww t
|jrIt | t jt jB sIdS t|ddt@ rSdS dS )r   r   TNFst_flagsr   )r   r!   r   r?   r/   S_ISLNKst_moder2   r   r   S_ISDIRr3   X_OKR_OKgetattrr   r   r   r   r   is_file_hidden_posix  s"   r   r    abs_rootc                 C   s  t j| } t j|}| |krdS t| rdS |s&| t jdd t j }| t|d }tdd |t jD r=dS t j| }|r|	|r||krt
|sYt j|}qCzt |}W n
 tyj   Y dS w t|ddt@ rudS t j|}|r|	|r||ksNdS )	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 : unicode
        The absolute path to check for hidden directories.
    abs_root : unicode
        The absolute path of the root directory in which hidden directories
        should be checked for.
    FTr	   r   Nc                 s   s    | ]}| d V  qdS )r   N)r?   )ro   partr   r   r   rp     s    zis_hidden.<locals>.<genexpr>r   )r   r!   normpathis_file_hiddenry   rm   lenanydirnamer?   r)   r   r2   r   r   )r   r   inside_rootr!   r6   r   r   r   	is_hidden  s4   r   fnamec           
      C   s   zddl }W n ty   t|  Y S w ddl}ddl}||j}|d||j	\}}}|
| |j}| }	|	|j|j|jB |jB | |	|j|j| |d|	d || |j| dS )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   Nr   r	   )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)
r   r   conr   adminsr   _domain_typesddaclr   r   r   win32_restrict_file_to_user  s*   
r   c           %         s  ddl ddl m jdddjdddd}d	}d
}d ddd}d}d}d}d}d}	d}
d}d}d
}d}d}||B dB }||	B |B |
B |B }||B |B |B |B |B }G fdddj}j|j}fd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_|j_j	j_
jj|fj_|j_j	j_
||jjjjjfj_|j_j	j_
||jfj_|j_j	j_
jjfj_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/||d }!| }"||" ||  ||" ||B |B |! || |}#||#}$||$d|"d ||$}#|| ||# dS )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secur32         z      i   i   i   i   r	               i  c                       s8   e Zd Zd jfd jfd jfd jfd jfgZdS )z0_win32_restrict_file_to_user_ctypes.<locals>.ACLAclRevisionSbz1AclSizeAceCountSbz2N)__name__
__module____qualname__BYTEWORD_fields_r   r   r   r   r   \  s    r   c                    s   | s	    |S rj   )WinErrorget_last_error)resultfuncargs)ctypesr   r   _nonzero_successi  s   z=_win32_restrict_file_to_user_ctypes.<locals>._nonzero_successc                    s   j d  } }z| d || W n* tyA } z|j kr% j |j  }| d || W Y d }~nd }~ww |d d  S )Nr	   )c_charDWORDr   byrefr2   winerrorvalue)WellKnownSidTypepSidcbSidr   ERROR_INSUFFICIENT_BUFFERr   r   r   r   r   r     s   
"z?_win32_restrict_file_to_user_ctypes.<locals>.CreateWellKnownSidc              
      s    d}z	| d | W n ty) } z|j kr W Y d }~nd }~ww |jjs0d S |jj}| || |jS Nr   )pointerc_ulongGetUserNameExWr2   r   contentsr   create_unicode_buffer)
NameFormatnSizer   lpNameBuffer)ERROR_MORE_DATAr   r   r   r   r     s   
z:_win32_restrict_file_to_user_ctypes.<locals>.GetUserNameExc           
         s    d} d} d}z| |d |d || W n ty= } z|j kr3 W Y d }~nd }~ww d|j}|j	}d|jd }| ||||||}	|	sr
 ||j|jfS )Nr   r   r	   )r   LookupAccountNameWr   r2   r   r   r   castr   LPVOIDr   )
lpSystemNamelpAccountNamer   cchReferencedDomainNamepeUser   Sidr   lpReferencedDomainNamesuccessr   r   r   r     sD   


	
	z>_win32_restrict_file_to_user_ctypes.<locals>.LookupAccountNamec                         | ||| d S rj   )r   )pAcldwAceRevision
AccessMaskr   r   r   r   r     s   z@_win32_restrict_file_to_user_ctypes.<locals>.AddAccessAllowedAcec              
      s    d}z| |d d| W n ty+ } z|j kr! W Y d }~nd }~ww |js1d S j|j  }| |||| |S r   )r   GetFileSecurityWr   r2   r   r   r   )
lpFileNameRequestedInformationnLengthr   pSecurityDescriptorr   r   r   r     s4   

z<_win32_restrict_file_to_user_ctypes.<locals>.GetFileSecurityc                    s     | || d S rj   )SetFileSecurityW)r  r  r  r
  r   r   r   -  s   z<_win32_restrict_file_to_user_ctypes.<locals>.SetFileSecurityc                    r  rj   )r   )r  bDaclPresentpDaclbDaclDefaultedr
  r   r   r   1  s   zF_win32_restrict_file_to_user_ctypes.<locals>.SetSecurityDescriptorDaclc                    sl  d } d}d } d}d } d}d } d}d }	 d}
z | |||||||||	|
 W n ty[ } z|j krQ W Y d }~nd }~ww j|j  }j|j  }|j}j|j  }|j}j|j  }|}j|
j  }|}	| |||||||||	|
 |S r   )	r   MakeAbsoluteSDr   r2   r   r   r   r   r   )pSelfRelativeSecurityDescriptorpAbsoluteSecurityDescriptor"lpdwAbsoluteSecurityDescriptorSizer  lpdwDaclSizepSacllpdwSaclSizepOwnerlpdwOwnerSizepPrimaryGrouplpdwPrimaryGroupSizer   	pDaclData	pSaclData
pOwnerDatapPrimaryGroupData)r   PACLPSIDr   r   r   r   r   r  5  sj   





z;_win32_restrict_file_to_user_ctypes.<locals>.MakeAbsoluteSDc              
      s~   d } d}z| || W n ty+ } z|j kr! W Y d }~nd }~ww j|j  }| || |S r   )r   MakeSelfRelativeSDr   r2   r   r   r   )r  r  lpdwBufferLengthr   r   r   r   r%  k  s*   

z?_win32_restrict_file_to_user_ctypes.<locals>.MakeSelfRelativeSDc                     s.   d}  | }|j}||   |S )Ni  )create_string_bufferr   r   InitializeAcl)
nAclLengthacl_datar  )r   r#  r   r   r   r   NewAcl  s
   
z3_win32_restrict_file_to_user_ctypes.<locals>.NewAclr   )r   r   WinDLL	Structurec_void_pPOINTERr   r   errcheckBOOLrestypec_intLPWSTRPULONGargtypesr   r   PDWORDr   LPDWORDr   r   r  LPCWSTRr  r  r%  r(  )%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  r%  r+  	SidAdminsSidUserAclSelfRelativeSD
AbsoluteSDr   )	r   r   r   r#  r$  r   r   r   r   r   r   '  s   		











#6

r   c                 C   s   t t | jd@ 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  )r/   S_IMODEr   r   )r   r   r   r   get_file_mode  s   rL  JUPYTER_ALLOW_INSECURE_WRITESr   )true1binaryc           	      c   s   |rdnd}|rdnd}t jt jB t jB }zt |  W n	 ty&   Y nw t jdkrHtr2t  nt 	| |d}t 
| t jt jB }t|  t jt 	| |d||d,}t jdkrvt| }|dkrvtrit  ndj| t|d	}t||V  W d   dS 1 sw   Y  dS )
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-8rU   i  )encodingzaPermissions assignment failed for secure file: '{file}'. Got '{permissions}' instead of '0o0600'.)filepermissions)r   O_CREATO_WRONLYO_TRUNCremover2   r   allow_insecure_writesissue_insecure_write_warningopencloser   fdopenrL  formatoctRuntimeError)	r   rP  moderS  	open_flagfdf	file_modemsgr   r   r   secure_write  s<   


"rh  c                  C   s   dd } | t _t d dS )z Issue an insecure write warning.c                 _   s   t | d S )N
)r#   )rg  r   kwargsr   r   r   format_warning  s   z4issue_insecure_write_warning.<locals>.format_warningzWARNING: 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'.N)r   formatwarningr   )rk  r   r   r   r[    s
   r[  )Frj   )r   )r   N)F__doc__r   r   r{   r/   r<   rD   r   
contextlibr   pathlibr   typingr   r   r   r   r   rL   utilsr
   r!   r\   rO   r[   rN   r   r   r#   boolr   r   r&   r'   __annotations__r7   rA   rG   rR   r`   rc   site_data_dirry   rz   rt   r   r   r@   programdatar=   rf   r   site_config_dirr   r   r   r)   r   r   r   r   r   r   intrL  getenvr   rZ  rh  r[  r   r   r   r   <module>   s    
 		!
 ; /*(6+  x7