o
    NK&h}$                     @  s   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 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 d dlmZ d dlmZmZmZ d dlm Z  dZ!dZ"G dd deZ#dS )    )annotations)Iterable)	lru_cache)	signature)AnyOptionalUnion)UUID)
BaseRouter)NoMethod)NotFound)
RouteGroup)Route)HTTP_METHODS)check_error_format)MethodNotAllowedr   SanicException)RouteHandleri   )__file_uri__c                      s   e Zd ZdZdZeZd:d
dZee	dd:ddZ
											d;d< fd%d&Zee	d	d=d>d)d*Zed?d,d-Zed@d/d0Zed@d1d2Zed@d3d4ZdA fd6d7ZdBd8d9Z  ZS )CRouterz`The router implementation responsible for routing a `Request` object to the appropriate handler.GETpathstrmethodhostOptional[str]return*tuple[Route, RouteHandler, dict[str, Any]]c              
   C  s   z| j |||rd|idW S d dW S  ty( } z
td|j dd d }~w tyL } ztd| d| ||jrCt|jdd d dd d }~ww )Nr   )r   r   extrazRequested URL z
 not foundzMethod z not allowed for URL )r   allowed_methods)resolveRoutingNotFoundr   r   r   r   r   tuple)selfr   r   r   e r%   ?/var/www/html/venv/lib/python3.10/site-packages/sanic/router.py_get   s4   

zRouter._get)maxsizec                 C  s   d}|  |||S )a  Retrieve a `Route` object containing the details about how to handle a response for a given request

        :param request: the incoming request object
        :type request: Request
        :return: details needed for handling the request and returning the
            correct response
        :rtype: Tuple[ Route, RouteHandler, Dict[str, Any]]

        Args:
            path (str): the path of the route
            method (str): the HTTP method of the route
            host (Optional[str]): the host of the route

        Raises:
            NotFound: if the route is not found
            MethodNotAllowed: if the method is not allowed for the route

        Returns:
            Tuple[Route, RouteHandler, Dict[str, Any]]: the route, handler, and match info
        T)r'   )r#   r   r   r   __tracebackhide__r%   r%   r&   get3   s   z
Router.getNF/vurimethodsIterable[str]handlerr   #Optional[Union[str, Iterable[str]]]strict_slashesboolstreamignore_bodyversion Optional[Union[str, float, int]]nameunquotestaticversion_prefix	overwriteerror_formatUnion[Route, list[Route]]c              	     sN  |durt |dd}d| | |dg}| ||}t|||r.ttt |nd|	||
|d}t|t r?|g}n|pCdg}g }|D ]R}|rU|	dd|ii |	}t
|dkrl|	rj|	 d|d	d nd
}t jdi |}||j_||j_||j_||j_||j_||j_|rt|jj || qHt
|dkr|d S |S )a  Add a handler to the router

        Args:
            uri (str): The path of the route.
            methods (Iterable[str]): The types of HTTP methods that should be attached,
                example: ["GET", "POST", "OPTIONS"].
            handler (RouteHandler): The sync or async function to be executed.
            host (Optional[str], optional): Host that the route should be on. Defaults to None.
            strict_slashes (bool, optional): Whether to apply strict slashes. Defaults to False.
            stream (bool, optional): Whether to stream the response. Defaults to False.
            ignore_body (bool, optional): Whether the incoming request body should be read.
                Defaults to False.
            version (Union[str, float, int], optional): A version modifier for the uri. Defaults to None.
            name (Optional[str], optional): An identifying name of the route. Defaults to None.

        Returns:
            Route: The route object.
        N/v)r   r/   r-   r7   strictr8   r;   requirementsr      _.__unnamed__r   r%   )r   striplstripjoin
_normalizedict	frozensetmap
isinstanceupdatelenreplacesuperaddr   identr4   r3   hostsr9   r<   r   append)r#   r,   r-   r/   r   r1   r3   r4   r5   r7   r8   r9   r:   r;   r<   paramsrT   routesrS   route	__class__r%   r&   rR   N   sN   $


z
Router.add	view_nameOptional[Route]c                 C  s>   |sdS | j |}|s| jj|}| j |}|sdS |S )a+  Find a route in the router based on the specified view name.

        Args:
            view_name (str): the name of the view to search for
            name (Optional[str], optional): the name of the route. Defaults to `None`.

        Returns:
            Optional[Route]: the route object
        N)
name_indexr*   ctxappgenerate_name)r#   r[   r7   rX   	full_namer%   r%   r&   find_route_by_view_name   s   zRouter.find_route_by_view_namedict[tuple[str, ...], Route]c                 C  s   dd | j D S )z|Return all routes in the router.

        Returns:
            Dict[Tuple[str, ...], Route]: a dictionary of routes
        c                 S  s   i | ]}|j |qS r%   )parts).0rX   r%   r%   r&   
<dictcomp>   s    z%Router.routes_all.<locals>.<dictcomp>)rW   r#   r%   r%   r&   
routes_all   s   zRouter.routes_all!dict[tuple[str, ...], RouteGroup]c                 C     | j S )a0  Return all static routes in the router.

        _In this context "static" routes do not refer to the `app.static()`
        method. Instead, they refer to routes that do not contain
        any path parameters._

        Returns:
            Dict[Tuple[str, ...], Route]: a dictionary of routes
        )static_routesrg   r%   r%   r&   routes_static   s   zRouter.routes_staticc                 C  rj   )zReturn all dynamic routes in the router.

        _Dynamic routes are routes that contain path parameters._

        Returns:
            Dict[Tuple[str, ...], Route]: a dictionary of routes
        )dynamic_routesrg   r%   r%   r&   routes_dynamic   s   	zRouter.routes_dynamicc                 C  rj   )a  Return all regex routes in the router.

        _Regex routes are routes that contain path parameters with regex
        expressions, or otherwise need regex to resolve._

        Returns:
            Dict[Tuple[str, ...], Route]: a dictionary of routes
        )regex_routesrg   r%   r%   r&   routes_regex   s   
zRouter.routes_regexNonec                   sJ   t  j|i | | j D ]}tdd |jD r"td| dqdS )zFinalize the router.

        Raises:
            SanicException: if a route contains a parameter name that starts with "__" and is not in ALLOWED_LABELS
        c                 s  s"    | ]}| d o|tvV  qdS )__N)
startswithALLOWED_LABELS)re   labelr%   r%   r&   	<genexpr>   s
    
z"Router.finalize.<locals>.<genexpr>zInvalid route: z". Parameter names cannot use '__'.N)rQ   finalizerm   valuesanylabelsr   )r#   argskwargsrX   rY   r%   r&   rw      s   
zRouter.finalizec           	      C  s   d|vr|S t |}dd |j D }g }|dD ]&}|dr<d|vr<|dd }||}|r<d| d| d}|| qd|S )	N<c                 S  s.   i | ]}|j ttttfv r|j|j j qS r%   )
annotationr   intfloatr	   r7   __name__lower)re   paramr%   r%   r&   rf     s
    z%Router._normalize.<locals>.<dictcomp>r>   :rB   >)r   
parametersrx   splitrs   r*   rU   rH   )	r#   r,   r/   sigmappingreconstructionpartr7   r~   r%   r%   r&   rI      s   

zRouter._normalize)r   r   r   r   r   r   r   r   )NFFFNNFFr+   FN)r,   r   r-   r.   r/   r   r   r0   r1   r2   r3   r2   r4   r2   r5   r6   r7   r   r8   r2   r9   r2   r:   r   r;   r2   r<   r   r   r=   )N)r[   r   r7   r   r   r\   )r   rc   )r   ri   )r   rq   )r,   r   r/   r   r   r   )r   
__module____qualname____doc__DEFAULT_METHODr   ALLOWED_METHODSr'   r   ROUTER_CACHE_SIZEr*   rR   rb   propertyrh   rl   rn   rp   rw   rI   __classcell__r%   r%   rY   r&   r      s@    
X
r   N)$
__future__r   collections.abcr   	functoolsr   inspectr   typingr   r   r   uuidr	   sanic_routingr
   sanic_routing.exceptionsr   r   r!   sanic_routing.groupr   sanic_routing.router   sanic.constantsr   sanic.errorpagesr   sanic.exceptionsr   r   sanic.models.handler_typesr   r   rt   r   r%   r%   r%   r&   <module>   s$    