
    j_
                         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mZ  ej                    Z	ddZ
d Z	 ej        Zn# e$ r dZY nw xY wd Zd	 ZddZdS )z
Utilities for path handling.
    N   )	py3compatc                 V   |                      d                               d          } t          j                            |           r!t          j                            |           r| S |d}nt          |t          j                  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 occurence 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
    -------
    Raises :exc:`IOError` or returns absolute path to file.
    "'N) .zFile z, does not exist in any of the search paths: )stripospathisabsisfile
isinstancer   string_typesgetcwdexpand_pathjoinabspathIOError)filename	path_dirsr   testnames       5lib/python3.11/site-packages/ipython_genutils/path.pyfilefindr      s   D ~~c""((--H	w}}X 27>>(#;#; 			Iy5	6	6 !L	 - -3;;y/11rw||D(;;<<7>>(## 	-7??8,,,,,	- '88YY( * * *    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'
    ntz$\IPYTHON_TEMP)r   namereplacer   
expandvars
expanduser)ss    r   r   r   K   sj     
w}}IIe^,,
27--a0011A	w}}IIne,,Hr   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_errnoes       r   r%   r%   g   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-%04Xr   i   r   )r   r   isdirr   basenamer%   r)   EEXISTstatst_inorandomrandintlink_or_copyremover(   renameshutilcopy)r*   r+   r,   new_dsts       r   r6   r6   x   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)   r1   r/   r   )r   r>   r-   s      r   ensure_dir_existsrA      s     7>>$ A	K4(((((( 	 	 	w%,&& '&&&&&	 W]]4   A84?@@@A As   9 
A$AA$)N)r<   )__doc__r   sysr)   r9   r4   r   r   getfilesystemencodingfs_encodingr   r   r'   AttributeErrorr%   r6   rA    r   r   <module>rH      s     
			 



          (c'))3* 3* 3* 3*l  .mGG   GGG  "" " "JA A A A A As   < AA