o
    )Ti8                     @   s  U 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 d dlmZ d dlmZ d dlmZmZmZmZ d	d
lmZ d	dlmZmZmZmZ edddZeee  ed< G dd deZdefddZ defddZ!eG dd dZ"dedee" fddZ#dede$fddZ%dedefddZ&dede$fdd Z'G d!d" d"e(Z)G d#d$ d$eZ*e	deee*ef  deee  fd%d&Z+dS )'    N)Counter)Iterator)contextmanager)
ContextVar)	dataclass)StringIO)IOAnyOptionalUnion   )PathDeprecations)fsspec_supportimport_fsspecimport_requestsurl_support_current_path_dir)defaultc                   @      e Zd ZdZdS )_CachedStdinz/Used to allow reading sys.stdin multiple times.N__name__
__module____qualname____doc__ r   r   G/home/ubuntu/.local/lib/python3.10/site-packages/jsonargparse/_paths.pyr          r   returnc                   C   s"   t tjtsttj t_tjS N)
isinstancesysstdinr   readr   r   r   r   _get_cached_stdin   s   r$   c                  C   s   t  } |  }| d |S )Nr   )r$   r#   seek)r"   valuer   r   r   _read_cached_stdin"   s   
r'   c                   @   s   e Zd ZU eed< eed< dS )_UrlDataschemeurl_pathN)r   r   r   str__annotations__r   r   r   r   r(   )   s   
 r(   urlc                 C   s:   |  d}|dkrd S t| d |d  | |d d  dS )N://r      )r)   r*   )rfindr(   )r-   indexr   r   r   
_parse_url/   s   
r2   pathc                 C   s   |  ddkr	dS tj| S )Nr.   r   T)findosr3   isabs)r3   r   r   r   _is_absolute_path9   s   r7   c                 C   sF   |  d}g }|D ]}|dkr|  q	|dkr|| q	d|S )N/...)splitpopappendjoin)r3   partsresolvedpartr   r   r   _resolve_relative_path?   s   



rB   c                 C   sD   t d ddlm} |D ]}| |d s| |d r dS qdS )N_known_to_fsspecr   )known_implementationsr.   z::TF)r   fsspec.registryrD   
startswith)r3   rD   protocolr   r   r   rC   J   s   rC   c                   @   r   )	PathErrorz.Exception raised for errors in the Path class.Nr   r   r   r   r   rH   T   r   rH   c                
   @   s@  e Zd ZU dZee ed< edZ			d(de
eejd f dedee
eejf  fd	d
ZedefddZedefddZedefddZedefddZedefddZdd Zdd ZdefddZdedefddZd)dedefd d!Zed)dedee fd"d#Zedee fd$d%Z e!defd&d'Z"dS )*Pathax  Base class for Path types. Stores a (possibly relative) path and the corresponding absolute path.

    From the object the absolute path can be obtained without having to remember
    the working directory (or parent remote path) from when the object was
    created.

    When a Path instance is created, it is checked that: the path exists,
    whether it is a file or directory and whether it has the required access
    permissions (f=file, d=directory, r=readable, w=writeable, x=executable,
    c=creatable, u=url, s=fsspec or in uppercase meaning not, i.e., F=not-file,
    D=not-directory, R=not-readable, W=not-writeable and X=not-executable).

    The creatable flag "c" can be given one or two times. If given once, the
    parent directory must exist and be writeable. If given twice, the parent
    directory does not have to exist, but should be allowed to create.

    An instance of Path class can also refer to the standard input or output.
    To do that, path must be set with the value "-"; it is a common practice.
    Then, getting the content or opening it will automatically be done on
    standard input or output.
    	_url_dataz
^file:///?frNr3   modecwdc              
   K   sZ  |  | | | d| _d}d}t|tr-|j| _|j}|j}|j}|j}|j	}|j
}nt|ttjfr|dkr<d| _t|}|rHt|nd}tj|}| j|rd| jtjdkr`dnd|}t|}	t|}t|pvt pvt }
d|v sd	|v r|s|
r|	s|
r|	st|
jd | }|
j| }t|}|du rt pt }d|v rtrd}n'd	|v rtrt|rd}n|du rt }|	r|ntj ||}d}nt!d
| j"s|rd|v rt#d}z
|$|%  W n# |j&y
 } z
t!| d| |d}~ww n	| j"sd|rdd dd |D }|rbt'd}z|(||}|(  |)  W n t*t+fyM } zt!d||d}~w t,ya } zt!d||d}~ww n| j"s| jsd|v rsdnd}d|v rtj-tj |d}tj.|s|/ddkrd}tj.|s||kr|}tj-tj |d}tj.|s||kstj.|st!| d|t0|tj1st!| d|d|v rt0|tj2rtj.|st!| d|d|v rt0|tj2rtj3|st!| d|nKd|v s!d|v rbt0|tj2s2t!| d|d|v rEtj.|sEt!d|d|v rbtj3|sbt45t4|j6sbt!d|d|v rxt0|tj7sxt!| d |d!|v rt0|tj1st!| d"|d#|v rt0|tj8st!| d$|d%|v rtj.|rt!d&|d'|v rtj3|st45t4|j6rt!d(|d)|v rt0|tj7rt!| d*|d+|v r t0|tj1r t!| d,|d-|v rt0|tj8rt!| d.||| _9|| _:|| _;|| _<|| _=|| _>|| _dS )/a  Initializer for Path instance.

        Args:
            path: The path to check and store.
            mode: The required type and access permissions among ``[fdrwxcuFDRWX]``.
            cwd: Working directory for relative paths. If ``None`` then ``os.getcwd()`` is used.

        Raises:
            ValueError: If the provided mode is invalid.
            PathError: If the path does not exist or does not agree with the mode.
        F-TNnt r8   usz;Expected path to be a string, os.PathLike or a Path object.rzPath with URL supportz HEAD not accessible :: c                 s   s    | ]	}|d v r|V  qdS )>   rS   wNr   ).0cr   r   r   	<genexpr>   s    z Path.__init__.<locals>.<genexpr>rI   zPath does not exist: z)Path exists but no permission to access: d	DirectoryFilerV   r9      z9 is not creatable since parent directory does not exist: z8 is not creatable since parent directory not writeable: z- is not creatable since path already exists: fz does not exist: zPath is not a directory: zPath is not a file: z is not readable: rT   z is not writeable: xz is not executable: DzPath is a directory: FzPath is a file: Rz is readable: Wz is writeable: Xz is executable: )?_deprecated_kwargs_check_mode_std_ior    rI   is_url	is_fsspecrJ   rM   absoluterelativer+   r5   PathLikefspathr3   
expanduser_file_schemematchsubnamer7   r2   r   getgetcwdrB   r*   r)   r   r   rC   r>   rH   _skip_checkr   headraise_for_status	HTTPErrorr   opencloseFileNotFoundErrorKeyErrorPermissionErrorrealpathisdircountaccessW_OKF_OKisfilestatS_ISFIFOst_modeR_OKX_OK	_relative	_absolute_cwd_mode_is_url
_is_fsspec)selfr3   rL   rM   kwargsrf   rg   url_dataabs_pathis_absolutecwd_url_datarequestsexfsspec_modefsspechandleptypepdirppdirr   r   r   __init__r   s   







((,,
zPath.__init__r   c                 C      | j S )z^Returns the relative representation of the path (how the path was given on instance creation).r   r   r   r   r   ri         zPath.relativec                 C   r   )z0Returns the absolute representation of the path.r   r   r   r   r   rh      r   zPath.absolutec                 C   r   r   )r   r   r   r   r   rL         z	Path.modec                 C   r   r   )r   r   r   r   r   rf     r   zPath.is_urlc                 C   r   r   )r   r   r   r   r   rg     r   zPath.is_fsspecc                 C   r   r   r   r   r   r   r   __str__     zPath.__str__c                 C   sD   d| j  }| |}d}| j| jkrd| j }| d| j | dS )NPath_rP   z, cwd=())r   _repr_skip_checkr   r   r   )r   rp   rM   r   r   r   __repr__  s   


zPath.__repr__c                 C   r   r   r   r   r   r   r   
__fspath__  r   zPath.__fspath__otherc                 C   s0   t |tr| j|jkS t |trt| |kS dS )NF)r    rI   r   r+   )r   r   r   r   r   __eq__  s
   

zPath.__eq__rS   c              	   C   s   | j rt S | jr |dksJ td}|| j}|  |jS | jr^t	d}|
| j|&}|}| W  d   W  d   S 1 sGw   Y  W d   dS 1 sWw   Y  dS t
| j|}| W  d   S 1 ssw   Y  dS )z4Returns the contents of the file or the remote path.rS   Path.get_contentN)re   r'   r   r   rq   r   ru   textr   r   rw   r#   )r   rL   r   responser   r   
input_filer   r   r   get_content   s(   "$r   c                 c   s    | j rd|v rt V  dS d|v rtjV  dS dS | jr&t|  V  dS | jrJtd}|	| j
|}|V  W d   dS 1 sCw   Y  dS t	| j
|}|V  W d   dS 1 s_w   Y  dS )z*Return an opened file object for the path.rS   rT   	Path.openN)re   r$   r!   stdoutr   r   r   r   r   rw   r   )r   rL   r   r   r   r   r   rw   3  s"   ""r   c                 c   sD    t | }t|tsJ |V  W d   dS 1 sw   Y  dS )z_Context manager to use this path's parent (directory or URL) for relative paths defined within.N)change_to_path_dirr    r+   )r   path_dirr   r   r   relative_path_contextE  s
   
"zPath.relative_path_contextc                 C   s   t | ts	tdtt| td dkrtdt|  D ]\}}||dkr*dndkr8td| d	| d
qd| v rEd| v rEtdd| v rQd| v rQtdd| v r]d| v r_tdd S d S )NzExpected mode to be a string.fdrwxcusFDRWXr   z4Expected mode to only include [fdrwxcusFDRWX] flags.rV   r[   r   zToo many occurrences (z) for flag "z".r\   rX   z$Both modes "f" and "d" not possible.rQ   z$Both modes "d" and "u" not possible.rR   z$Both modes "d" and "s" not possible.)r    r+   
ValueErrorlensetr   items)rL   flagr~   r   r   r   rd   L  s   
zPath._check_mode)rK   N)rS   )#r   r   r   r   r
   r(   r,   recompilerm   r   r+   r5   rj   r   propertyri   rh   rL   boolrf   rg   r   r   r   r	   r   r   r   r   r   rw   r   staticmethodrd   r   r   r   r   rI   X   sF   
 

 rI   c              	   c   s    t  }d}| dur>t| trt| dd} | jr(| js| jr(| jj}| jj	}nd}| j
}d}d| jvr:tj|}|| }t |}|rV|rVt }tj|}t| z|V  W t | |rit| dS dS t | |rxt| w w )z>A context manager for running code in the directory of a path.FNrX   )rL   rP   T)r   rq   r    r+   rI   rJ   rf   rg   r)   r*   rh   rL   r5   r3   dirnamer   rr   abspathchdirreset)r3   r   r   r)   tokenr   r   r   r   ]  s<   






r   ),r5   r   r   r!   collectionsr   collections.abcr   
contextlibr   contextvarsr   dataclassesr   ior   typingr   r	   r
   r   _deprecatedr   
_optionalsr   r   r   r   r   r+   r,   r   r$   r'   r(   r2   r   r7   rB   rC   	TypeErrorrH   rI   r   r   r   r   r   <module>   s:   
 

  ,