o
    !wiט                     @  s\  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sWejd	krZejd
sZdZndZeeddZedcddddZededdZdcdfddZdgddZdhddZi Z de!d < did"d#Z"dgd$d%Z#djd&d'Z$dhd(d)Z%dhd*d+Z&dhd,d-Z'ed.Z(de!d/< dZ)d0e!d1< ej*d2kre(rej+,d3dZ)e rej*d2kre(se-eejd4dgZ.n*ej/edd5d60ej1Z.nej*d2kre)ree)dgZ.ne-eejd4dgZ.nd7d8gZ.e-eejd4dgZ2d9e!d:< dkd<d=Z3e-eejd>dgZ4d9e!d?< e rBej*d2kr5e(s5e4dd Z5n*ej6edd5d60ej1Z5nej*d2kr[e)rTe-ee)dgZ5ne4dd Z5nd@dAgZ5dldBdCZ7didDdEZ8dmdndJdKZ9dmdndLdMZ:ejdkr~e9Z;ne:Z;dodpdPdQZ<dqdSdTZ=dqdUdVZ>drdXdYZ?e@dZd[A d\v ZBedcdsd_d`ZCdtdadbZDdS )uzPath utility functions.    )annotationsN)Iterator)contextmanager)Path)Anyoverloadwin32darwinz/opt/homebrewJupyterjupyter	UF_HIDDENi   Fnamestrdefaultboolreturnc                 C     d S N r   r   r   r   O/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/jupyter_core/paths.pyenvset(      r   Nonebool | Nonec                 C  r   r   r   r   r   r   r   r   ,   r   c                 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lowerr   r   r   r   r   0   s   
c                   C  s
   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   r   use_platform_dirs>   s   
r$   c                  C  s   t d } tt |  S )z'Get the real path of the home directory~)r   
expanduserr   resolve)homedirr   r   r   get_home_dirF   s   r)   zdict[str, str]_dtempspathc              	   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_ownQ   s"   
r;   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_prefixr;   
startswithgetr   r   r   r   prefer_environment_over_userl   s   

rE   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.
    -)rA   )r*   KeyErrortempfilemkdtemp)r   dr   r   r   _mkdtemp_once   s   
rK   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!   rD   rK   r$   platformdirsuser_config_dirAPPNAMEr)   pjoin)envhome_dirr   r   r   jupyter_config_dir   s   


rV   c                  C  s   t j} | dr| d S t rtjtddS t }tj	dkr&t
t|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_DIRFrN   r	   Libraryr
   r   APPDATANr   dataXDG_DATA_HOMEz.localshare)r    r!   rD   r$   rP   user_data_dirrR   r)   r@   platformr   r   r'   rS   rV   )rT   homeappdataxdgr   r   r   jupyter_data_dir   s"   



rb   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!   rD   rS   rb   )rT   r   r   r   jupyter_runtime_dir   s   
re   JUPYTER_USE_PROGRAMDATA_use_programdataz
str | None_win_programdatantPROGRAMDATAr\   T)rO   	multipathz/usr/local/share/jupyterz/usr/share/jupyter	list[str]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t|dd}||vr?|| ttkrFt}ndd tD }t r[|| || n
|| || tD ]}||vrr|| qg r~ fd	d|D }|S )
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                 s      | ]	}| tjV  qd S r   rstripr    sep.0r9   r   r   r   	<genexpr>%      zjupyter_path.<locals>.<genexpr>getuserbaser\   r   c                 S     g | ]}|t vr|qS r   )SYSTEM_JUPYTER_PATHrt   r   r   r   
<listcomp>:      z jupyter_path.<locals>.<listcomp>c                   s   g | ]
}t |g R  qS r   )rS   rt   rn   r   r   r{   J  s    )r    r!   rD   extendsplitpathseprb   siteENABLE_USER_SITEr2   rx   	USER_BASEr   r   appendrm   rz   rE   )rn   pathsuseruserbaseuserdirrT   _pathr   r}   r   jupyter_path   s2   +"




r   etcENV_CONFIG_PATHz/usr/local/etc/jupyterz/etc/jupyterc                  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t|dd}||vrI|| ttkrPt}ndd	 tD }t re| | | | n
| | | | ttkrx| t | S )
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']

    rL   JUPYTER_CONFIG_PATHc                 s  rp   r   rq   rt   r   r   r   rv     rw   z&jupyter_config_path.<locals>.<genexpr>rx   r   r   c                 S  ry   r   )SYSTEM_CONFIG_PATHrt   r   r   r   r{     r|   z'jupyter_config_path.<locals>.<listcomp>)r    r!   rD   rV   r~   r   r   r   r   r2   rx   r   r   r   r   r   r   rE   )r   r   r   r   rT   r   r   r   jupyter_config_pathg  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    lstatr6   )r+   r   r   r   r-     s   r-   abs_path
str | Pathstat_res
Any | Nonec              
   C  s   t | } | jdrdS |du r3zt |  }W n ty2 } 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	yP   t
jdd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.   
stacklevel)r   r   rC   r3   r6   errnoENOENTst_file_attributesFILE_ATTRIBUTE_HIDDENAttributeErrorwarningswarnr   r   er   r   r   is_file_hidden_win  s6   r   c              
   C  s   t | } | jdrdS |du st|jr7z|  }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   rC   r3   S_ISLNKst_moder6   r   r   S_ISDIRr    r7   X_OKR_OKgetattrr   r   r   r   r   is_file_hidden_posix  s$   r    abs_rootc              	   C  s,  t tj| } |rt tj|}nt| jd }| |kr dS |  s.d| d}t|| s<d|d}t|| |sNd| d|d}t|t	| rTd	S | 
|}td
d |jD red	S | jD ]+}| soqh||krv dS z| }W n ty   Y  d	S w t|ddt@ r d	S qhd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 : 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                 s  s    | ]}| d V  qdS )r   N)rC   )ru   partr   r   r   rv   C  s    zis_hidden.<locals>.<genexpr>r   r   )r   r    r+   normpathlistparentsis_absolute
ValueErroris_relative_tois_file_hiddenrelative_toanypartsr-   r   r6   r   r   )r   r   _msgrelative_pathr.   r:   r   r   r   	is_hidden  sH   


	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      )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_userX  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}dF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_dGfd"d#}dHfd%d&}dIfd)d*}dJfd/d0}dKfd3d4}dLfd6d7}dMfd;d<}dNfd>d?}dOfdAdB}dP fdCdD}||} |dE||d }!| }"||" ||  ||" ||B |B |! || |}#||#}$||$d|"d ||$}#|| ||# dS )Qa  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                      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   resultintfuncr   argsr   c                   s   | s	    |S r   )WinErrorget_last_error)r   r   r   )ctypesr   r   _nonzero_success  s   z=_win32_restrict_file_to_user_ctypes.<locals>._nonzero_successWellKnownSidTypec                   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   byrefr6   winerrorvalue)r   pSidcbSidr   ERROR_INSUFFICIENT_BUFFERr   r   r   r   r   r   ,  s   
"z?_win32_restrict_file_to_user_ctypes.<locals>.CreateWellKnownSid
NameFormatc              
     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GetUserNameExWr6   r   contentsr   create_unicode_buffer)r  nSizer   lpNameBuffer)ERROR_MORE_DATAr   r   r   r   r   9  s   
z:_win32_restrict_file_to_user_ctypes.<locals>.GetUserNameExlpSystemNamelpAccountNamec           
        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   r6   r   r  r   castr  LPVOIDr   )
r  r  r  cchReferencedDomainNamepeUser   Sidr  lpReferencedDomainNamesuccessr  r   r   r   H  sD   


	
	z>_win32_restrict_file_to_user_ctypes.<locals>.LookupAccountNamepAcldwAceRevision
AccessMaskr  c                        | ||| d S r   )r   )r  r  r  r  r   r   r   r   k  s   z@_win32_restrict_file_to_user_ctypes.<locals>.AddAccessAllowedAce
lpFileNameRequestedInformationc              
     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   r6   r   r   r   )r  r  nLengthr   pSecurityDescriptorr  r   r   r   p  s4   

z<_win32_restrict_file_to_user_ctypes.<locals>.GetFileSecurityr"  c                   s     | || d S r   )SetFileSecurityW)r  r  r"  r  r   r   r     s   z<_win32_restrict_file_to_user_ctypes.<locals>.SetFileSecuritybDaclPresentpDaclbDaclDefaultedc                   r  r   )r   )r"  r$  r%  r&  r  r   r   r     s   zF_win32_restrict_file_to_user_ctypes.<locals>.SetSecurityDescriptorDaclpSelfRelativeSecurityDescriptorc                   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   r6   r   r   r   r  r
  )r'  pAbsoluteSecurityDescriptor"lpdwAbsoluteSecurityDescriptorSizer%  lpdwDaclSizepSacllpdwSaclSizepOwnerlpdwOwnerSizepPrimaryGrouplpdwPrimaryGroupSizer   	pDaclData	pSaclData
pOwnerDatapPrimaryGroupData)r  PACLPSIDr   r   r   r   r   r(    sj   





z;_win32_restrict_file_to_user_ctypes.<locals>.MakeAbsoluteSDr)  c              
     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   r6   r   r   r   )r)  r'  lpdwBufferLengthr   r  r   r   r8    s*   

z?_win32_restrict_file_to_user_ctypes.<locals>.MakeSelfRelativeSDc                    s.   d}  | }|j}||   |S )Ni  )create_string_bufferr  r
  InitializeAcl)
nAclLengthacl_datar  )r   r6  r   r   r   r   NewAcl  s
   
z3_win32_restrict_file_to_user_ctypes.<locals>.NewAclr   )r   r   r   r   r   r   r   r   )r   r   r   r   )r  r   r   r   )r  r   r  r   r   r   )
r  r   r  r   r  r   r  r   r   r   )r  r   r  r   r   r   )r  r   r  r   r"  r   r   r   )
r"  r   r$  r   r%  r   r&  r   r   r   )r'  r   r   r   )r)  r   r   r   )r   r   )r   r   WinDLL	Structurec_void_pPOINTERr   r	  errcheckBOOLrestypec_intLPWSTRPULONGargtypesr   r   PDWORDr  LPDWORDr   r   r   LPCWSTRr#  r(  r8  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(  r8  r>  	SidAdminsSidUserAclSelfRelativeSD
AbsoluteSDr   )	r   r  r  r6  r7  r   r   r   r   r   r     s   		











#6

r   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  )r3   S_IMODEr   r   )r   r   r   r   get_file_mode  s   r_  JUPYTER_ALLOW_INSECURE_WRITESr   )true1binaryIterator[Any]c           	      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rItr3t	  nt 
| |d}t | t jt jB }t|  t jt 
| |d||d.}t jdkryt| }|dkrytrjt	  nd|  d	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-8ri   i  )encodingz0Permissions assignment failed for secure file: 'z'. Got 'z' instead of '0o0600'.)r    O_CREATO_WRONLYO_TRUNCr   unlinkr6   r   allow_insecure_writesissue_insecure_write_warningopencloser   fdopenr_  octRuntimeError)	r   rc  moderg  	open_flagfdf	file_modemsgr   r   r   secure_write  s:   


"ry  c                  C  s"   ddd} | t _t jd	d
d dS )z Issue an insecure write warning.rx  r   r   r   kwargsr   c                 _  s   t | d S )N
)r   )rx  r   rz  r   r   r   format_warningM  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'.r   r   N)rx  r   r   r   rz  r   r   r   )r   formatwarningr   )r|  r   r   r   rm  J  s   

rm  )F)r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   )r   r   )r+   r   r   r   )r   r   r   r   )rn   r   r   rl   )r   rl   r   )r   r   r   r   r   r   )r   )r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   )r   r   rc  r   r   rd  )r   r   )E__doc__
__future__r   r   r    r   r3   r@   rH   r   collections.abcr   
contextlibr   pathlibr   typingr   r   rP   r+   joinrS   r^   rA   rC   rR   r   r   r   r$   r)   r*   __annotations__r;   rE   rK   rV   rb   re   rg   rh   r   r!   rD   r   rz   site_data_dirr   r   rm   r   r   r   site_config_dirr   r-   r   r   r   r   r   r   r_  getenvr"   rl  ry  rm  r   r   r   r   <module>   s    








!

X

E.)
D
,  
|5