
    ܖi7                         d dl Z d dlZd dlmZ 	  G d de      Z G d de      Z G d de      Z G d	 d
e      Z	 G d de      Z
y)    N)wrapsc                   "    e Zd ZdZd Zd Zd Zy)RuleaR  
    A rule is a mapping between a pattern (typically a URI) and a callable
    (typically a function).
    The pattern is a regex string pattern and must match entirely a string
    (typically a URI) for the rule to be considered, i.e. for the endpoint to
    be resolved and eventually invoked for a given string (typically a URI).
    c                 0   |j                  d      j                  d      | _        t        j                  d| j                  z   dz         j
                  | _        t        |      sJ t        j                  |      r |       }t        |      sJ || _
        y )N^$)lstriprstrippatternrecompilematchpattern_matchcallableinspectisclassendpoint)selfr   r   objs       \/var/www/html/content-pipeline/venv/lib/python3.12/site-packages/packageurl/contrib/route.py__init__zRule.__init__@   sz     ~~c*11#6ZZdll(:S(@AGG !!!??8$*CC= =     c                 ~    d| j                    d| j                  j                   d| j                  j                   dS )Nz	Rule(r"""z""", .))r   r   
__module____name__r   s    r   __repr__zRule.__repr__O   s8    4<<.dmm.F.F-GqI_I_H``abbr   c                 $    | j                  |      S )zP
        Match a string with the rule pattern, return True is matching.
        )r   )r   strings     r   r   z
Rule.matchR   s     !!&))r   N)r   r   __qualname____doc__r   r   r    r   r   r   r   7   s    !c*r   r   c                       e Zd ZdZy)RouteAlreadyDefinedzF
    Raised when this route Rule already exists in the route map.
    Nr   r   r"   r#   r$   r   r   r&   r&   Y       r   r&   c                       e Zd ZdZy)NoRouteAvailablez3
    Raised when there are no route available.
    Nr'   r$   r   r   r*   r*   _   r(   r   r*   c                       e Zd ZdZy)MultipleRoutesDefinedz=
    Raised when there are more than one route possible.
    Nr'   r$   r   r   r,   r,   e   r(   r   r,   c                   H    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zy)Routera  
    A router is:
    - a container for a route map, consisting of several rules, stored in an
     ordered dictionary keyed by pattern text
    - a way to process a route, i.e. given a string (typically a URI), find the
     correct rule and invoke its callable endpoint
    - and a convenience decorator for routed callables (either a function or
     something with a __call__ method)

    Multiple routers can co-exist as needed, such as a router to collect,
    another to fetch, etc.
    Nc                 8    |xs
 t               | _        d| _        y)zG
        'route_map' is an ordered mapping of pattern -> Rule.
        N)dict	route_map_is_routable)r   r1   s     r   r   zRouter.__init__y   s     #,df r   c                 ,    t        | j                        S N)reprr1   r   s    r   r   zRouter.__repr__   s    DNN##r   c                 H    t        | j                  j                               S r4   )iterr1   itemsr   s    r   __iter__zRouter.__iter__   s    DNN((*++r   c                 6    | j                   j                         S r4   )r1   keysr   s    r   r;   zRouter.keys   s    ~~""$$r   c                 h    || j                   v rt        |      t        ||      | j                   |<   y)z
        Append a new pattern and endpoint Rule at the end of the map.
        Use this as an alternative to the route decorator.
        N)r1   r&   r   )r   r   r   s      r   appendzRouter.append   s0    
 dnn$%g.."&w"9wr   c                       fd}|S )a  
        Decorator to make a callable 'endpoint' routed to one or more patterns.

        Example:
        >>> my_router = Router()
        >>> @my_router.route('http://nexb.com', 'http://deja.com')
        ... def somefunc(uri):
        ...    pass
        c                 h    sJ D ]  }j                  ||         t        |       fd       }|S )Nc                  (     j                   | i |S r4   )process)argskwargsr   s     r   	decoratedz2Router.route.<locals>.decorator.<locals>.decorated   s    #t||T4V44r   )r=   r   )r   patrD   patternsr   s      r   	decoratorzRouter.route.<locals>.decorator   sG    O8 +C*+ 8_5 5 r   r$   )r   rF   rG   s   `` r   routezRouter.route   s    		 r   c                 t    | j                  |      }t        j                  |      r |       } ||g|i |S )z
        Given a string (typically a URI), resolve this string to an endpoint
        by searching available rules then execute the endpoint callable for
        that string passing down all arguments to the endpoint invocation.
        )resolver   r   )r   r!   rB   rC   r   s        r   rA   zRouter.process   s<     <<'??8$  zH0000r   c                 R   | j                   j                         D cg c]  }|j                  |      s| }}|st        |      t	        |      dkD  r;t        |D cg c]  }|j                   c}      }dt               z  }t        |      |d   j                  S c c}w c c}w )a}  
        Resolve a string: given a string (typically a URI) resolve and
        return the best endpoint function for that string.

        Ambiguous resolution is not allowed in order to keep things in
        check when there are hundreds rules: if multiple routes are
        possible for a string (typically a URI), a MultipleRoutesDefined
        TypeError is raised.
           z-%(string)r matches multiple patterns %(pats)rr   )
r1   valuesr   r*   lenr5   r   localsr,   r   )r   r!   r
candidatespatsmsgs         r   rJ   zRouter.resolve   s     "&!6!6!8LAAGGFOaL
L"6**z?Q J7q78DAFHLC',,!}%%% M 8s   BBB$c                 
   |sy| j                   s[ddj                  d | j                  D              z   dz   }t        j                  |t        j
                        j                  | _         t        | j                  |            S )z{
        Return True if `string` is routable by this router, e.g. if it
        matches any of the route patterns.
        Nz^(|c              3       K   | ]  }|  y wr4   r$   ).0rE   s     r   	<genexpr>z%Router.is_routable.<locals>.<genexpr>   s     'F'Fs   z)$)r2   joinr1   r   r   UNICODEr   bool)r   r!   	routabless      r   is_routablezRouter.is_routable   sh    
   sxx'Ft~~'FFFMI "

9bjj A G GDD%%f-..r   r4   )r   r   r"   r#   r   r   r9   r;   r=   rH   rA   rJ   r]   r$   r   r   r.   r.   k   s4    !$,%:01&4/r   r.   )r   r   	functoolsr   objectr   	TypeErrorr&   r*   r,   r.   r$   r   r   <module>ra      sT   6  	 0*6 *D) y I u/V u/r   