o
    bi                     @   sL   d dl Z d dlZd dlmZ d dlmZ G dd dZdede	fdd	Z
dS )
    N)Path)Unionc                   @   s   e Zd ZdZdefddZdedd fddZedefd	d
Z	edddZ
edefddZedefddZdd Zedejjdeeef defddZdd Zdd ZdS )URIam  Represents a URI, supporting path appending and retrieving parent URIs.

    Example Usage:

        >>> s3_uri = URI("s3://bucket/a?scheme=http&param=1")
        >>> s3_uri
        URI<s3://bucket/a?scheme=http&param=1>
        >>> str(s3_uri / "b" / "c")
        's3://bucket/a/b/c?scheme=http&param=1'
        >>> str(s3_uri.parent)
        's3://bucket?scheme=http&param=1'
        >>> str(s3_uri)
        's3://bucket/a?scheme=http&param=1'
        >>> s3_uri.parent.name, s3_uri.name
        ('bucket', 'a')
        >>> local_path = URI("/tmp/local")
        >>> str(local_path)
        '/tmp/local'
        >>> str(local_path.parent)
        '/tmp'
        >>> str(local_path / "b" / "c")
        '/tmp/local/b/c'

    Args:
        uri: The URI to represent.
            Ex: s3://bucket?scheme=http&endpoint_override=localhost%3A900
            Ex: file:///a/b/c/d
    uric                 C   sF   t j|| _| jjst|| _d S ttj	| jj
| jj | _d S N)urllibparseurlparse_parsedschemer   _pathospathnormpathnetloc)selfr    r   O/home/ubuntu/.local/lib/python3.10/site-packages/ray/air/_internal/uri_utils.py__init__%   s   "zURI.__init__subpathreturnc                 C   sJ   t | jt |sJ | j|ft | jt |d}t| | j|S )ao  Returns a new URI that strips the given subpath from the end of this URI.

        Example:
            >>> uri = URI("s3://bucket/a/b/c/?param=1")
            >>> str(uri.rstrip_subpath(Path("b/c")))
            's3://bucket/a?param=1'

            >>> uri = URI("/tmp/a/b/c/")
            >>> str(uri.rstrip_subpath(Path("/b/c/.//")))
            '/tmp/a'

         )strr   endswithreplacer   _get_str_representationr
   )r   r   stripped_pathr   r   r   rstrip_subpath-   s   "zURI.rstrip_subpathc                 C      | j jS r   )r   namer   r   r   r   r   >      zURI.namec                 C   s4   | j jdksJ t|  dt| | j| j jS )N.z has no valid parent URI)r   parentr   r   r   r
   r    r   r   r   r#   B   s   z
URI.parentc                 C   r   r   )r
   r   r    r   r   r   r   G   r!   z
URI.schemec                 C   s
   t | jS r   )r   r   r    r   r   r   r   K   s   
zURI.pathc                 C   s&   t |tsJ t| | j| j| S r   )
isinstancer   r   r   r
   r   )r   path_to_appendr   r   r   __truediv__O   s   zURI.__truediv__
parsed_urir   c                 C   s$   |j st|S |jt|dd S )Nr   )r   r   )r   r   _replacegeturl)clsr'   r   r   r   r   r   U   s   zURI._get_str_representationc                 C   s   dt |  dS )NzURI<>)r   r    r   r   r   __repr__]      zURI.__repr__c                 C   s   |  | j| jS r   )r   r
   r   r    r   r   r   __str__`   r-   zURI.__str__N)r   r   )__name__
__module____qualname____doc__r   r   r   r   propertyr   r#   r   r   r&   classmethodr   r   ParseResultr   r   r,   r.   r   r   r   r   r      s.    
r   r   r   c                 C   s   t tj| jS r   )boolr   r   r	   r   )r   r   r   r   is_urid   s   r7   )r   urllib.parser   pathlibr   typingr   r   r   r6   r7   r   r   r   r   <module>   s    ]