
    Mh*                     $   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	  ej
                    Zd Zej        dk    rd Znd Zd Zd	ed
efdZd Zdded
efdZ G d de          Zdd
efdZd Zd Zd Zd Zd ZdZd Zd ZddZdS ) z
Utilities for path handling.
    N)systemc                 ~    t           j                            |           ot          j        | t           j                  S )zBWhether `path` is a directory, to which the user has write access.)ospathisdiraccessW_OKr   s    R/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/utils/path.py_writable_dirr      s)    7==;29T27#;#;;    win32c                    	 ddl }n"# t          $ r}t          d          |d}~ww xY w|j        j        j        }|j        |j        |j        g|_        |                    d          } || |d          }|dk    s|dk    r| S |j	        S )zGet a long path name (expand ~) on Windows using ctypes.

        Examples
        --------

        >>> get_long_path_name('c:\\docume~1')
        'c:\\Documents and Settings'

        r   Nz2you need to have ctypes installed for this to worki  )
ctypesImportErrorwindllkernel32GetLongPathNameW	c_wchar_pc_uintargtypescreate_unicode_buffervalue)r   r   e_GetLongPathNamebufrvs         r   _get_long_path_namer      s    	[MMMM 	[ 	[ 	[RSSYZZ	[!=1B%+%5v7GM%! **3//dC--77b3hhK9s    
&!&c                     | S )zDummy no-op. r
   s    r   r   r   6   s    r   c                      t          |           S )zExpand a path into its long form.

    On Windows this expands any ~ in the paths. On other platforms, it is
    a null operation.
    )r   r
   s    r   get_long_path_namer"   <   s     t$$$r   r   returnc                     t           j                            d          }|                     |          rd| t	          |          d         z   } | S )z%Reverse of :func:`os.path.expanduser`~N)r   r   
expanduser
startswithlen)r   homes     r   compress_userr*   E   sI    7c""Dt 'd3t99::&&Kr   c                    t           j                            |           } t           j                            |           r| S |                     d          s&| dz   }t           j                            |          r|S t          d| z            )zReturn a valid python filename in the current directory.

    If the given name is not a file, it adds '.py' and searches again.
    Raises IOError with an informative message if the file isn't found.
    z.pyzFile `%r` not found.)r   r   r&   isfileendswithIOError)namepy_names     r   get_py_filenamer1   L   s     7d##D	w~~d == ,7>>'"" 	N
(4/
0
00r   filenamec                 L   |                      d                               d          } t          j                            |           r!t          j                            |           r| S |d}nt          |t                    r|f}|D ]}|dk    rt          j                    }t          t          j        	                    ||                     }t          j                            |          r!t          j        
                    |          c S t          d| d|          )a  Find a file by looking through a sequence of paths.

    This iterates through a sequence of paths looking for a file and returns
    the full, absolute path of the first occurrence of the file.  If no set of
    path dirs is given, the filename is tested as is, after running through
    :func:`expandvars` and :func:`expanduser`.  Thus a simple call::

        filefind('myfile.txt')

    will find the file in the current working dir, but::

        filefind('~/myfile.txt')

    Will find the file in the users home directory.  This function does not
    automatically try any paths, such as the cwd or the user's home directory.

    Parameters
    ----------
    filename : str
        The filename to look for.
    path_dirs : str, None or sequence of str
        The sequence of paths to look for the file in.  If None, the filename
        need to be absolute or be in the cwd.  If a string, the string is
        put into a sequence and the searched.  If a sequence, walk through
        each element and join with ``filename``, calling :func:`expandvars`
        and :func:`expanduser` before testing for existence.

    Returns
    -------
    path : str
        returns absolute path to file.

    Raises
    ------
    IOError
    "'N) .zFile z, does not exist in any of the search paths: )stripr   r   isabsr,   
isinstancestrgetcwdexpand_pathjoinabspathr.   )r2   	path_dirsr   testnames       r   filefindrB   ]   s   N ~~c""((--H	w}}X 27>>(#;#; 			Is	#	# !L	 - -3;;ry{{rw||D(;;<<7>>(## 	-7??8,,,,,	- '88YY( * * *r   c                       e Zd ZdS )HomeDirErrorN)__name__
__module____qualname__r    r   r   rD   rD      s        Dr   rD   Fc                    t           j                            d          }t           j                            |          }t	          |          slt           j        dk    r\	 ddl}|                    |j        d          5 }|	                    |d          d         }ddd           n# 1 swxY w Y   n#  Y nxY w| rt	          |          r!t          |t                    s
J d            |S t          d|z            )	aV  Return the 'home' directory, as a unicode string.

    Uses os.path.expanduser('~'), and checks for writability.

    See stdlib docs for how this is determined.
    For Python <3.8, $HOME is first priority on *ALL* platforms.
    For Python >=3.8 on Windows, %HOME% is no longer considered.

    Parameters
    ----------
    require_writable : bool [default: False]
        if True:
            guarantees the return value is a writable directory, otherwise
            raises HomeDirError
        if False:
            The path is resolved, but it is not guaranteed to exist or be writable.
    r%   ntr   Nz@Software\Microsoft\Windows\CurrentVersion\Explorer\Shell FoldersPersonalz#Homedir should be unicode not byteszD%s is not a writable dir, set $HOME environment variable to override)r   r   r&   realpathr   r/   winregOpenKeyHKEY_CURRENT_USERQueryValueExr:   r;   rD   )require_writablehomedirwregkeys       r   get_home_dirrT      sV   & g  %%G gw''G!! 
bgoo	!!!!&S  ? ++C
;;A>	? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
	D Hw!7!7 H'3''NN)NNNN =?FG H H 	Hs0   B3 >B'B3 'B++B3 .B+/B3 3B7c                     t           j        } t           j        dk    rl|                     dd          p+t           j                            t                      d          }|r(t          |          rt          |t                    sJ |S dS )zReturn the XDG_CONFIG_HOME, if it is defined and exists, else None.

    This is only for non-OS X posix (Linux,Unix,etc.) systems.
    posixXDG_CONFIG_HOMENz.config
r   environr/   getr   r>   rT   r   r:   r;   envxdgs     r   get_xdg_dirr^      s~     *C	w' gg'..Y"',,|~~y2Y2Y 	=%% 	c3'''''J4r   c                     t           j        } t           j        dk    rl|                     dd          p+t           j                            t                      d          }|r(t          |          rt          |t                    sJ |S dS )zReturn the XDG_CACHE_HOME, if it is defined and exists, else None.

    This is only for non-OS X posix (Linux,Unix,etc.) systems.
    rV   XDG_CACHE_HOMENz.cacherX   r[   s     r   get_xdg_cache_dirra      s~     *C	w' gg&--Wlnnh1W1W 	=%% 	c3'''''J4r   c                    t           j        dk    r|                     dd          } t           j                            t           j                            |                     } t           j        dk    r|                     dd          } | S )zExpand $VARS and ~names in a string, like a shell

    :Examples:

       In [2]: os.environ['FOO']='test'

       In [3]: expand_path('variable FOO is $FOO')
       Out[3]: 'variable FOO is test'
    rI   z$\IPYTHON_TEMP)r   r/   replacer   
expandvarsr&   )ss    r   r=   r=      sj     
w}}IIe^,,
27--a0011A	w}}IIne,,Hr   c                 t    d }d                     t          ||                     d                              S )z"Unescape glob pattern in `string`.c                 b    dD ]+}|                      d                    |          |          } ,| S )Nz*[]!?z\{0})rd   format)rf   patterns     r   unescapezunescape_glob.<locals>.unescape  s7     	< 	<G		'..117;;AAr   \z\\)r>   mapsplit)stringrk   s     r   unescape_globrp     s;       99S6<<#7#788999r   c                     g }t           j        dk    rt          nd }| D ]5}|                    t	          j        |          p ||          g           6|S )z
    Do glob expansion for each element in `args` and return a flattened list.

    Unmatched glob pattern will remain as-is in the returned list.

    r   c                     | S Nr    )xs    r   <lambda>zshellglob.<locals>.<lambda>  s    q r   )sysplatformrp   extendglob)argsexpandedrk   as       r   	shellglobr}     sb     H !$ 7 7}}[[H 7 7	!5!6666Or   i  c                     t          t          d          st          S d}	 t          j        | |           n# t          $ r}|j        }Y d}~nd}~ww xY w|S )zHard links ``src`` to ``dst``, returning 0 or errno.

    Note that the special errno ``ENOLINK`` will be returned if ``os.link`` isn't
    supported by the operating system.
    linkr   N)hasattrr   ENOLINKr   OSErrorerrno)srcdst
link_errnor   s       r   r   r     sp     2v J
S   W





s   6 
A AAc                    t           j                            |          r=t           j                            |t           j                            |                     }t          | |          }|t          j        k    rt          j        |           j	        t          j        |          j	        k    rdS |dt          j        dd          fz  z   }	 t          | |           n,#  	 t          j        |           n# t          $ r Y nw xY w xY wt          j        ||           dS |dk    rt!          j        | |           dS dS )a+  Attempts to hardlink ``src`` to ``dst``, copying if the link fails.

    Attempts to maintain the semantics of ``shutil.copy``.

    Because ``os.link`` does not overwrite files, a unique temporary file
    will be used if the target already exists, then that file will be moved
    into place.
    Nz
-temp-%04X   i   r   )r   r   r   r>   basenamer   r   EEXISTstatst_inorandomrandintlink_or_copyremover   renameshutilcopy)r   r   r   new_dsts       r   r   r   /  s=    
w}}S 7gll3 0 0 5 566c3JU\!!73<<"'#,,"555 Fv~a'?'?&BBB	g&&&&		'""""   
	'3	q 	C 
s0   C! !D
$C98D
9
DD
DD
  c                 4   t           j                            |           sG	 t          j        | |           dS # t          $ r!}|j        t
          j        k    r Y d}~dS d}~ww xY wt           j                            |           st          d| z            dS )zensure that a directory exists

    If it doesn't exist, try to create it and protect against a race condition
    if another process is doing the same.

    The default permissions are 755, which differ from os.makedirs default of 777.
    )modeNz %r exists but is not a directory)	r   r   existsmakedirsr   r   r   r   r.   )r   r   r   s      r   ensure_dir_existsr   S  s     7>>$ A	K4(((((( 	 	 	w%,&& '&&&&&	 W]]4   A84?@@@A As   9 
A$AA$rs   )F)r   ) __doc__r   rv   r   r   r   ry   warningsIPython.utils.processr   getfilesystemencodingfs_encodingr   rw   r   r"   r;   r*   r1   rB   	ExceptionrD   rT   r^   ra   r=   rp   r}   r   r   r   r   r    r   r   <module>r      s    
			 



      ( ( ( ( ( (
 (c'))< < < <7   2  % % %     1 1 1"8* 8*s 8*s 8* 8* 8* 8*v	 	 	 	 	9 	 	 	)H )HC )H )H )H )HV  &  &  .: : :     "" " "HA A A A A Ar   