o
    tf0                     @  s   d dl m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mZ d dlmZ d dlmZmZmZ g dZG d	d
 d
ejdZG dd deZG dd deZG dd deZd ddZd!ddZdS )"    )annotationsN)Request)ResponseMountRoute)ProviderProtocol)create_file_responsecreate_resource_identifierguess_media_type)ResourceFileResourceDirectoryResourceContentResourcecreate_resource_routecreate_resourcec                   @  sJ   e Zd ZdZ	ddddZedddZedddZej	dddZ
dS )r   z,A resource that can be served by a provider.Nproviderr   headersNone | dict[str, str]c                 C  s    |pi | _ || _t j| _dS )a  Create a new Resource.

        Parameters
        ----------
        provider : ProviderProtocol
            The provider that will serve this resource.
        headers : dict[str, str], optional
            Additional headers to include in the response.
        N)r   	_provideruuiduuid4hex_guid)selfr   r    r   Z/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/servir/_resources.py__init__    s   
zResource.__init__returnstrc                 C  s   | j S )z(The unique identifier for this resource.)r   r   r   r   r   guid2   s   zResource.guidc                 C  s   | j j d| j S )zThe URL for this resource.z/resources/)r   urlr!   r    r   r   r   r"   7   s   zResource.urlrequestr   r   c                 C  s   dS )zGet the resource.

        Parameters
        ----------
        request : Request
            The request to handle.

        Returns
        -------
        Response
            The response to send.
        Nr   )r   r#   r   r   r   get<   s   zResource.getN)r   r   r   r   )r   r   r#   r   r   r   )__name__
__module____qualname____doc__r   propertyr!   r"   abcabstractmethodr$   r   r   r   r   r      s    r   )	metaclassc                      s(   e Zd Zd fddZdddZ  ZS )r   pathpathlib.Pathkwargs
typing.Anyc                   sD   t  jdi | | sJ d|| _t|  |jd| _d S )NzPath must be a filedataidr   )	superr   is_file_pathr
   resolveas_posixnamer   r   r/   r1   	__class__r   r   r   N   s   zFileResource.__init__r#   r   r   r   c                 C  s&   t | j|jd}|j| j |S )Nrange)r	   r8   r   r$   update)r   r#   responser   r   r   r$   V   s   zFileResource.getr/   r0   r1   r2   r&   )r'   r(   r)   r   r$   __classcell__r   r   r=   r   r   M   s    r   c                      s,   e Zd ZdZd fddZdddZ  ZS )r   z Serve a directory as a resource.r/   r0   r1   r2   c                   sJ   t  jdi | | std|| _t|  |j	dd| _
dS )a^  Create a new DirectoryResource.

        Parameters
        ----------
        path : pathlib.Path
            The path to the directory to serve.

        kwargs : dict
            Additional keyword arguments to pass to the Resource constructor.

        Raises
        ------
        ValueError
            If the path is not a directory.
        zPath must be a directory/r3   Nr   )r6   r   is_dir
ValueErrorr8   r
   r9   r:   r;   lstripr   r<   r=   r   r   r   _   s   

zDirectoryResource.__init__r#   r   r   r   c                 C  sH   |j d }| j|| jdd }t||jd}|j| j |S )Nr/    rD   r?   )	path_paramsr8   replacer   rG   r	   r   r$   r@   )r   r#   	full_pathresolvedrA   r   r   r   r$   z   s
   
zDirectoryResource.getrB   r&   r'   r(   r)   r*   r   r$   rC   r   r   r=   r   r   \   s    r   c                      s0   e Zd ZdZ	dd fd	d
ZdddZ  ZS )r   z&Serve a string or bytes as a resource.Ncontentstr | bytes	extension
None | strr1   r2   c                   sJ   t  jdi | || _|du rt|trdnd}t|d| d| _dS )aQ  Create a new ContentResource.

        Parameters
        ----------
        content : str | bytes
            The content to serve.
        extension : str, optional
            The extension to use for the resource identifier.
        kwargs : dict
            Additional keyword arguments to pass to the Resource constructor.
        Nz.txtz.binrN   r3   r   )r6   r   _content
isinstancer   r
   r   )r   rN   rP   r1   r=   r   r   r      s
   zContentResource.__init___r   r   r   c                 C  s   t | jt| j| j dS )N)rN   
media_typer   )r   rR   r   r   r   copy)r   rT   r   r   r   r$      s
   zContentResource.getr%   )rN   rO   rP   rQ   r1   r2   )rT   r   r   r   rM   r   r   r=   r   r      s
    r   	resourcestyping.Mapping[str, Resource]r   r   c                   s*   d fdd}t dtd|d	d
gdgdS )zCreate a route for serving resources.

    Parameters
    ----------
    resources : typing.Mapping[str, Resource]
        A mapping of resource identifiers to resources.

    Returns
    -------
    Mount
        A route for serving resources.
    r#   r   r   r   c                   s&   | j d }|dd } | | S )Nr/   rD   r   )rI   splitr$   )r#   r/   r!   rW   r   r   endpoint   s   
z'create_resource_route.<locals>.endpointz
/resourcesz/{path:path}GETHEAD)methods)r/   ZroutesNr&   r   )rW   r[   r   rZ   r   r      s   r   xpathlib.Path | strr   r   r1   r2   c                 K  s   t | trt| fd|i|S t | tjr#|  r#t| fd|i|S t | tjr7|  r7t| fd|i|S t	dt
|  )a  Create a resource from a path or string.

    Parameters
    ----------
    x : pathlib.Path | str
        The path or string to create a resource from.
    provider : ProviderProtocol
        The provider that will serve the resource.
    kwargs : dict
        Additional keyword arguments to pass to the Resource constructor.

    Returns
    -------
    Resource
        The created resource.
    r   zUnsupported type: )rS   r   r   pathlibPathr7   r   rE   r   	TypeErrortype)r_   r   r1   r   r   r   r      s   
r   )rW   rX   r   r   )r_   r`   r   r   r1   r2   r   r   )
__future__r   r,   ra   typingr   Zstarlette.requestsr   Zstarlette.responsesr   Zstarlette.routingr   r   Zservir._protocolsr   Zservir._utilr	   r
   r   __all__ABCMetar   r   r   r   r   r   r   r   r   r   <module>   s"    
0&
