
    ^.h                         d 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Zddl	m
Z
 ddlmZmZmZ ddlmZ g dZg dZd	 Z G d
 de      Z G d de      Zd Z G d de      Zy)zMapper and Sub-Mapper    N)LRUCache)request_config)controller_scanRoutesException
as_unicodeRoute)indexcreatenew)showupdatedeleteeditc                 ^    | j                  d      r| dd } | j                  d      r| dd } | S )z8Remove slashes from the beginning and end of a part/URL./   N)
startswithendswith)names    a/var/www/html/School_Mangement_New/src/backend/venv/lib/python3.12/site-packages/routes/mapper.pystrip_slashesr      s5    sABx}}SCRyK    c                   *    e Zd ZdZd ZdddeedfdZy)SubMapperParentzbBase class for Mapper and SubMapper, both of which may be the parent
    of SubMapper objects
    c                     t        | fi |S )a  Create a partial version of the Mapper with the designated
        options set

        This results in a :class:`routes.mapper.SubMapper` object.

        If keyword arguments provided to this method also exist in the
        keyword arguments provided to the submapper, their values will
        be merged with the saved options going first.

        In addition to :class:`routes.route.Route` arguments, submapper
        can also take a ``path_prefix`` argument which will be
        prepended to the path of all routes that are connected.

        Example::

            >>> map = Mapper(controller_scan=None)
            >>> map.connect('home', '/', controller='home', action='splash')
            >>> map.matchlist[0].name == 'home'
            True
            >>> m = map.submapper(controller='home')
            >>> m.connect('index', '/index', action='index')
            >>> map.matchlist[1].name == 'index'
            True
            >>> map.matchlist[1].defaults['controller'] == 'home'
            True

        Optional ``collection_name`` and ``resource_name`` arguments are
        used in the generation of route names by the ``action`` and
        ``link`` methods.  These in turn are used by the ``index``,
        ``new``, ``create``, ``show``, ``edit``, ``update`` and
        ``delete`` methods which may be invoked indirectly by listing
        them in the ``actions`` argument.  If the ``formatted`` argument
        is set to ``True`` (the default), generated paths are given the
        suffix '{.format}' which matches or generates an optional format
        extension.

        Example::

            >>> from routes.util import url_for
            >>> map = Mapper(controller_scan=None)
            >>> m = map.submapper(path_prefix='/entries', collection_name='entries', resource_name='entry', actions=['index', 'new'])
            >>> url_for('entries') == '/entries'
            True
            >>> url_for('new_entry', format='xml') == '/entries/new.xml'
            True

        )	SubMapper)selfkargss     r   	submapperzSubMapperParent.submapper%   s    ` '''r   Nz/{id}c	           	          ||xs |}||d}
n
d}
n|d}
nd}
|
j                  ||      }t        | f|||||d|	}t        |f||d|xs i |_        |S )a  Create a submapper that represents a collection.

        This results in a :class:`routes.mapper.SubMapper` object, with a
        ``member`` property of the same type that represents the collection's
        member resources.

        Its interface is the same as the ``submapper`` together with
        ``member_prefix``, ``member_actions`` and ``member_options``
        which are passed to the ``member`` submapper as ``path_prefix``,
        ``actions`` and keyword arguments respectively.

        Example::

            >>> from routes.util import url_for
            >>> map = Mapper(controller_scan=None)
            >>> c = map.collection('entries', 'entry')
            >>> c.member.link('ping', method='POST')
            >>> url_for('entries') == '/entries'
            True
            >>> url_for('edit_entry', id=1) == '/entries/1/edit'
            True
            >>> url_for('ping_entry', id=1) == '/entries/1/ping'
            True

         z/{collection_name}z{pre}z{pre}/{collection_name})precollection_name)r%   resource_namepath_prefix
controlleractions)r'   r)   )formatr   member)r   r%   r&   r'   member_prefixr(   collection_actionsmember_actionsmember_optionskwargspath_prefix_str
collections               r   r2   zSubMapperParent.collectionW   s    < &9/J&"$"6&")"; &,,=L - N t E_-:+6:'9E >DE

 &j @m.<@)7)=2@
 r   )__name__
__module____qualname____doc__r!   COLLECTION_ACTIONSMEMBER_ACTIONSr2    r   r   r   r       s&    0(d FJ!(T&8"09r   r   c                   x    e Zd ZdZ	 	 ddZddZ	 	 ddZd Zd ZddZ	dd	Z
dd
Zd Zd Zd Zd Zd Zd Zy)r   z#Partial mapper for use with_optionsNc                 :   || _         || _        || _        d | _        |xs0 t	        |dd       xs! |j                  dd       xs t	        |dd       | _        ||| _        n%t	        |dd       | _        | j                  d| _         | j                  |xs g fi | y )Nr&   r(   	formattedT)	r0   objr%   r+   getattrgetr&   r<   add_actions)r   r=   r&   r%   r)   r<   r0   s          r   __init__zSubMapper.__init__   s    .* 0sOT20zz,-0 sL$/ 	  &DN$S+t<DN~~%!%B1&1r   c                 D   i }|}|}t        j                  | j                        D ]  \  }}|dk(  rD|!dj                  | j                  |   |f      }.dj                  | j                  |   |f      }O|dk(  r&|!dj                  | j                  |   |f      }wd }z||v r,t	        |t
              rt        |fi ||   ||<   ||   ||<   | j                  |   ||<    |D ]  }|| j                  vs||   ||<    ||f}	 | j                  j                  |	i |S )Nr'   r#   name_prefix)six	iteritemsr0   join
isinstancedictr=   connect)
r   	routenamepathr0   newkargs
_routename_pathkeyvaluenewargss
             r   rI   zSubMapper.connect   sE   
--4 	1JCm## GGT[[%5t$<=EGGT[[%5y$ABE%# "$$++c*:I)F!GJ!%JeT*$($>&+$>HSM %+3KHSM $C 05	16  	,C$++% &s	, u%txx5H55r   c                     |s|| j                   rd}nd} | j                  |xs |dz   | j                  z   d|xs |z   |z   fd|xs |xs |it        ||      S )a  Generates a named route for a subresource.

        Example::

            >>> from routes.util import url_for
            >>> map = Mapper(controller_scan=None)
            >>> c = map.collection('entries', 'entry')
            >>> c.link('recent', name='recent_entries')
            >>> c.member.link('ping', method='POST', formatted=True)
            >>> url_for('entries') == '/entries'
            True
            >>> url_for('recent_entries') == '/entries/recent'
            True
            >>> url_for('ping_entry', id=1) == '/entries/1/ping'
            True
            >>> url_for('ping_entry', id=1, format='xml') == '/entries/1/ping.xml'
            True

        	{.format}r#   _r   actionr<   rI   r&   _kwargs_with_conditions)r   relr   rU   methodr<   r0   suffixs           r   linkzSubMapper.link   s}    * *t~~ FFt||DDS3Y1C1C%C3;$/&8G#)#8S#8DG 6ffEG 	Gr   c                 *     | j                   dddi|S )z4Generates the "new" link for a collection submapper.rX   r   r9   r[   r   r0   s     r   r   zSubMapper.new   s    tyy-U-f--r   c                 *     | j                   dddi|S )z<Generates the "edit" link for a collection member submapper.rX   r   r9   r]   r^   s     r   r   zSubMapper.edit   s    tyy.V.v..r   c                     |s|| j                   rd}nd} | j                  |xs |dz   | j                  z   |fd|xs |it        ||      S )a  Generates a named route at the base path of a submapper.

        Example::

            >>> from routes import url_for
            >>> map = Mapper(controller_scan=None)
            >>> c = map.submapper(path_prefix='/entries', controller='entry')
            >>> c.action(action='index', name='entries', formatted=True)
            >>> c.action(action='create', method='POST')
            >>> url_for(controller='entry', action='index', method='GET') == '/entries'
            True
            >>> url_for(controller='entry', action='index', method='GET', format='xml') == '/entries.xml'
            True
            >>> url_for(controller='entry', action='create', method='POST') == '/entries'
            True

        rS   r#   rT   rU   rV   )r   r   rU   rY   r<   r0   rZ   s          r   rU   zSubMapper.action   si    & *t~~ FFt||DGVc\D4F4F%F"G#)>TG 6ffEG 	Gr   c                 J     | j                   d|xs | j                  ddd|S )z8Generates the "index" action for a collection submapper.r
   GETr   rU   rY   r9   )rU   r%   r   r   r0   s      r   r
   zSubMapper.index  s8    t{{ C <(<(<")%C;AC 	Cr   c                 J     | j                   d|xs | j                  ddd|S )z>Generates the "show" action for a collection member submapper.r   rb   rc   r9   )rU   r&   rd   s      r   r   zSubMapper.show  s8    t{{ B :(:(:"(B:@B 	Br   c                 ,     | j                   dddd|S )z9Generates the "create" action for a collection submapper.r   POSTrU   rY   r9   rU   r^   s     r   r   zSubMapper.create  s    t{{D(6DVDDr   c                 ,     | j                   dddd|S )z@Generates the "update" action for a collection member submapper.r   PUTrh   r9   ri   r^   s     r   r   zSubMapper.update  s    t{{C(5CFCCr   c                 ,     | j                   dddd|S )z@Generates the "delete" action for a collection member submapper.r   DELETErh   r9   ri   r^   s     r   r   zSubMapper.delete"  s    t{{F(8FvFFr   c                 N    |D cg c]  } t        | |      di | c} y c c}w Nr9   )r>   )r   r)   r0   rU   s       r   r@   zSubMapper.add_actions&  s%    7>?V	v		(	(??s   "c                     | S Nr9   r   s    r   	__enter__zSubMapper.__enter__*  s    r   c                      y rq   r9   )r   typerP   tbs       r   __exit__zSubMapper.__exit__-  s    r   )NNNNrq   )NNNrb   N)NNrb   N)r3   r4   r5   r6   rA   rI   r[   r   r   rU   r
   r   r   r   r   r@   rs   rw   r9   r   r   r   r      sc    -@D)-2$$6L =BG>./G8C
B
EDG@r   r   c                 D    |rd| vr| j                         }d|i|d<   |S | S )N
conditionsrY   )copy)r0   rY   	newkwargss      r   rW   rW   2  s1    ,f,KKM	#+V"4	,r   c                       e Zd ZdZeddddfdZd Zd Zd Zd	 Z	 e
eee	      Zdd
Zd Zd Zd Zd ZddZd ZddZddZd Zd Zd Zy)Mapperap  Mapper handles URL generation and URL recognition in a web
    application.

    Mapper is built handling dictionary's. It is assumed that the web
    application will handle the dictionary returned by URL recognition
    to dispatch appropriately.

    URL generation is done by passing keyword parameters into the
    generate function, a URL is then returned.

    NFTc                    g | _         i | _        i | _        t        d      | _        d| _        d| _        d| _        d| _        t        j                         | _        || _        || _        || _        d| _        i | _        d| _        d| _        d| _        g | _        d| _        || _        d| _        d| _        d| _        d| _        t        j6                         | _        |rt;               }| |_        yy)a
  Create a new Mapper instance

        All keyword arguments are optional.

        ``controller_scan``
            Function reference that will be used to return a list of
            valid controllers used during URL matching. If
            ``directory`` keyword arg is present, it will be passed
            into the function during its call. This option defaults to
            a function that will scan a directory for controllers.

            Alternatively, a list of controllers or None can be passed
            in which are assumed to be the definitive list of
            controller names valid when matching 'controller'.

        ``directory``
            Passed into controller_scan for the directory to scan. It
            should be an absolute path if using the default
            ``controller_scan`` function.

        ``always_scan``
            Whether or not the ``controller_scan`` function should be
            run during every URL match. This is typically a good idea
            during development so the server won't need to be restarted
            anytime a controller is added.

        ``register``
            Boolean used to determine if the Mapper should use
            ``request_config`` to register itself as the mapper. Since
            it's done on a thread-local basis, this is typically best
            used during testing though it won't hurt in other cases.

        ``explicit``
            Boolean used to determine if routes should be connected
            with implicit defaults of::

                {'controller':'content','action':'index','id':None}

            When set to True, these defaults will not be added to route
            connections and ``url_for`` will not use Route memory.

        Additional attributes that may be set after mapper
        initialization (ie, map.ATTRIBUTE = 'something'):

        ``encoding``
            Used to indicate alternative encoding/decoding systems to
            use with both incoming URL's, and during Route generation
            when passed a Unicode string. Defaults to 'utf-8'.

        ``decode_errors``
            How to handle errors in the encoding, generally ignoring
            any chars that don't convert should be sufficient. Defaults
            to 'ignore'.

        ``minimization``
            Boolean used to indicate whether or not Routes should
            minimize URL's and the generated URL's, or require every
            part where it appears in the path. Defaults to False.

        ``hardcode_names``
            Whether or not Named Routes result in the default options
            for the route being used *or* if they actually force url
            generation to use the route. Defaults to False.

        i@  FNz[^\.\/]+?\.[^\.\/]+utf-8ignoreT)	matchlistmaxkeysminkeysr   urlcache_created_regs_created_gens_master_regexpprefix	threadinglocalreq_data	directoryalways_scanr   
_regprefix_routenamesdebugappend_slashsub_domainssub_domains_ignoredomain_matchexplicitencodingdecode_errorshardcode_namesminimizationLockcreate_regs_lockr   mapper)r   r   r   r   registerr   configs          r   rA   zMapper.__init__G  s    F  """!)"&.
! "$2 %"! ) 0#%F FM r   c                    d }dg| j                   D cg c]_  }|j                  xs d ||      |j                  xs d|j                  j	                  dd      |j                  j	                  dd      fa c}z   }t        t        |d               D cg c]  t        fd|D               c}dj                  fd	|D              S c c}w c c}w )
z*Generates a tabular string representation.c                     | j                   rD| j                   j                  dd      }t        |      t        u xr |xs dj	                  |      S y)NrY   r#   z, )ry   r?   ru   strrF   )rrY   s     r   format_methodsz&Mapper.__str__.<locals>.format_methods  sD    ||))(B7F|s*5vJ69JJr   )z
Route nameMethodsPath
ControllerrU   r#   r(   rU   r   c              3   :   K   | ]  }t        |           y wrq   len).0rowcols     r   	<genexpr>z!Mapper.__str__.<locals>.<genexpr>  s     5c#c(m5s   
c           	   3   z   K   | ]1  d j                  fdt        t                    D               3 yw) c              3   L   K   | ]  }|   j                  |           y wrq   )ljust)r   r   r   widthss     r   r   z+Mapper.__str__.<locals>.<genexpr>.<genexpr>  s*      4 X^^F3K0 4s   !$N)rF   ranger   )r   r   r   s    @r   r   z!Mapper.__str__.<locals>.<genexpr>  s:        HH 4 %c&k 24 4s   7;)	r   r   	routepathdefaultsr?   r   r   maxrF   )r   r   r   tabler   r   s       `@r   __str__zMapper.__str__  s    	 KK ..* &&,Bq 11;;3D"**..r2AJJNN8R4PR ** #3uQx=13 5u55 3 yy    	*3s   A$CCc                 N    	 | j                   j                  S # t        $ r Y y w xY wrq   )r   environAttributeErrorrr   s    r   _envgetzMapper._envget  s)    	==((( 		s    	$$c                 &    || j                   _        y rq   r   r   )r   envs     r   _envsetzMapper._envset  s     #r   c                     | j                   `y rq   r   rr   s    r   _envdelzMapper._envdel  s    MM!r   c                    |D ]  }|r*|j                   rdj                  ||j                  g      }n|r||j                  z   }n|j                  } | j                  |j                  |fd|j
                  i|j                    y)a  Extends the mapper routes with a list of Route objects

        If a path_prefix is provided, all the routes will have their
        path prepended with the path_prefix.

        Example::

            >>> map = Mapper(controller_scan=None)
            >>> map.connect('home', '/', controller='home', action='splash')
            >>> map.matchlist[0].name == 'home'
            True
            >>> routes = [Route('index', '/index.htm', controller='home',
            ...                 action='index')]
            >>> map.extend(routes)
            >>> len(map.matchlist) == 2
            True
            >>> map.extend(routes, path_prefix='/subapp')
            >>> len(map.matchlist) == 3
            True
            >>> map.matchlist[2].routepath == '/subapp/index.htm'
            True

        .. note::

            This function does not merely extend the mapper with the
            given list of routes, it actually creates new routes with
            identical calling arguments.

        r   ry   N)r   rF   r   rI   r   ry   _kargs)r   routesr'   router   s        r   extendzMapper.extend  s    <  	Eu11HHk5??%CD	'%//9	!OO	DLL"$)$4$4 !<<	r   c                     t        |i |S )zjMake a new Route object

        A subclass can override this method to use a custom Route class.
        r   )r   argsr    s      r   
make_routezMapper.make_route  s    
 d$e$$r   c                    d}t        |      dkD  r|d   }nd|z   }d|vr| j                  |d<   d|vr| j                  |d<    | j                  |i |}| j                  dk7  s| j
                  dk7  r&d	|vr"| j                  |_        | j
                  |_        |j                  s| j                  j                  |       |r|| j                  |<   ||_
        |j                  ryd
}| j                  D ]3  }||j                  k(  s| j                  |   j                  |       d} n |s|g| j                  |j                  <   d
| _        y)a  Create and connect a new Route to the Mapper.

        Usage:

        .. code-block:: python

            m = Mapper()
            m.connect(':controller/:action/:id')
            m.connect('date/:year/:month/:day', controller="blog",
                      action="view")
            m.connect('archives/:page', controller="blog", action="by_page",
            requirements = { 'page':'\d{1,2}' })
            m.connect('category_list', 'archives/category/:section',
                      controller='blog', action='category',
                      section='home', type='list')
            m.connect('home', '', controller='blog', action='view',
                      section='home')

        Nr   r   rq   	_explicit	_minimizer   r   	_encodingFT)r   r   r   r   r   r   staticr   appendr   r   r   r   )r   r   r    rJ   r   existsrO   s          r   rI   zMapper.connect  sF   ( 	t9q=QIT>De#!%E+e#!%!2!2E+// MMW$(:(:h(Fe#!]]EN"&"4"4E||NN!!%(*/DY'"EJ<<<< 	Cemm#S!((/		
 +0'DLL'"r   c                    i }i }| j                   D ]P  }|j                  rd|j                  v rd||j                  d   <   d|j                  v s?d||j                  d   <   R t        |j	                               dgz   }t        |j	                               dgz   }i }| j                   D ]  }|j                  r|}|}d|j
                  v r|j                  d   g}d|j
                  v r#t        j                  |j                  d         g}|D ]A  }|D ]:  }|j                  |i       }	|	j                  |g i f      d   j                  |       < C  || _
        d| _        y)z.Create the generation hashes for route lookupsr(   TrU   *r   N)r   r   r   listkeys	hardcodedrD   	text_type
setdefaultr   _gendictr   )
r   controllerlist
actionlistr   gendictclistalistr(   rU   
actiondicts
             r   _create_genszMapper._create_gens4  s{    
 ^^ 	<E||u~~-?Cu~~l;<5>>)7;
5>>(34	< n1134u<*//+,u4

 ^^ 	ME||"EEu.565??*u~~h'?@A# M
# MF!(!3!3J!CJ))&2r(;A>EEeLMM	M  !r   c                     | j                   j                          	  | j                  |i | | j                   j                          y# | j                   j                          w xY w)zPAtomically creates regular expressions for all connected
        routes
        N)r   acquire_create_regsrelease)r   r   r0   s      r   create_regszMapper.create_regs^  sU     	%%'	,Dt.v.!!))+D!!))+s   A	 	A%c                    |i| j                   r| j                  | j                         }nAt        | j                        r| j                         }n| j                  sg }n| j                  }t        j                  | j
                        D ]  \  }}|D ]  }|j                  |         g }t        j                  t              }| j                  D ]  }|j                  r|j                  |j                  |d             dj                  t        j                  d |j                               }|j"                  r|j%                  d      sd|z   }||j'                  d         j                  |        || _        t+        t-        d |D              d	      | _        | j0                  r't3        j4                  | j0                  d
z         | _        dj                  |D cg c]  }d|z  	 c}      }	|	| _        	 t3        j4                  |	      | _        d| _        yc c}w # t<        $ r d| _        Y d| _        yw xY w)z4Creates regular expressions for all connected routesNF)include_namesr#   c                 "    t        | t              S rq   )rG   r   )ps    r   <lambda>z%Mapper._create_regs.<locals>.<lambda>~  s    
1c8J r   r   c              3   2   K   | ]  }t        |        y wrq   r   )r   r   s     r   r   z&Mapper._create_regs.<locals>.<genexpr>  s     &E!s1v&Es   T)reversez(.*)|z(?:%s)) r   r   callablerD   rE   r   
makeregexpcollectionsdefaultdictr   r   r   r   rF   it	takewhile	routelistr   r   rstrip_prefix2routessortedset_prefix_lensr   recompiler   _master_regr   OverflowErrorr   )
r   r   rO   valr   regexpsprefix2routesr   xregexps
             r   r   zMapper._create_regsh  s   =~~,,T^^<$../,,.)),,dll3 	(HC (  '(	( #//5^^ 	@E<<u//U/KL.J.3oo"? @%%f.?.?.D 6\FfmmC0188?	@ ,"3&E}&E#E+/1 ;; jjv)=>DO 9A8a<9:!	'"$**V"4D " :  	'"&D!	's   &H#H( (IIc           	           j                   s j                  r j                          n j                   st        d       j                  r j                          g } j
                  rKt        j                   j                        r&t        j                   j                  d      sdndd|fS |xs  j                  } j                  } j                  } j                  } j                  } j                  !t        j                   j                        }nd}|sdd|fS t         j"                  j%                   fd j&                  D              }	|	D ]  }
|
j(                  r|r|j+                  t-        |
d             .|
j                  ||||      }|r%|j+                  t-        |
t/        |                   t1        |t,              s|s}||
|fc S  dd|fS )	zInternal Route matcher

        Matches a URL against a route, and returns a tuple of the match
        dict and the route object if a match is successfull, otherwise
        it returns empty.

        For internal use only.

        z:You must generate the regular expressions before matching.z\1r   NTc              3   \   K   | ]#  }j                   j                  d | d       % y wro   )r   r?   )r   
prefix_lenr   urls     r   r   z Mapper._match.<locals>.<genexpr>  s6      +P/9 ,0+>+>+B+B3{
CSUW+X +Ps   ),)r   r   )r   r  )r   r   r   r   r   r   r   matchr   subr   r   r   r   r   r   r   chainfrom_iterabler   r   r   rH   boolrG   )r   r  r   matchlogr   r   r   r   	valid_urlr   r   r  s   ``          r   _matchzMapper._match  s    !!d&:&:##! #6 7 7 ;;xx-ffT__eS9CdH--)T\\&&!44((

* !4!4c:I
 I$))HH** +P=A=N=N+P P	 
	0E||OODuT$BCKKWk;M ,.E5e EF%&%uh//
	0 dH%%r   c                     ||st        d      ||d   }| j                  ||      }| j                  r|d   |d   |d   fS t        |d   t              s|d   r|d   S y)zMatch a URL against against one of the routes contained.

        Will return None if no valid match is found.

        .. code-block:: python

            resultdict = m.match('/joe/sixpack')

        NURL or environ must be provided	PATH_INFOr   r      r   r  r   rG   rH   r   r  r   results       r   r  zMapper.match  sw     ;w!"CDD;+&CS'*::!9fQi22fQi&&)!9r   c                     ||st        d      ||d   }| j                  ||      }| j                  r|d   |d   |d   fS t        |d   t              s|d   r
|d   |d   fS y)a  Match a URL against against one of the routes contained.

        Will return None if no valid match is found, otherwise a
        result dict and a route object is returned.

        .. code-block:: python

            resultdict, route_obj = m.match('/joe/sixpack')

        Nr  r  r   r   r  r  r  s       r   
routematchzMapper.routematch  s     ;w!"CDD;+&CS'*::!9fQi22fQi&&)!9fQi''r   c                    | j                   s| j                          | j                  rd|d<   | j                  sd|vrd|d<   d|vrd|d<   |j	                  d| j
                        xs i }d|v r|d   }n,| j
                  rd| j
                  v r| j
                  d   }nd	}|j                  dd
      }|j                  dd
      }t        j                  |      j                  d      t        j                  |      j                  d      z   }| j                  ]t        j                  r#dj                  |j                  d      |f      }n|d|}| j                  j                  ||       }	|	| k7  r|	S t        || j                        }t        || j                        }| j                  j                  |      xs | j                  j                  di       }
|
s|sy
|
j                  |      xs |
j                  dd
i f      \  }}|s|sy
t!        |j#                               d}t        j                        }|j                  |      }|r|}nq|r|}nld}g }|D ]<  }t%        |j&                  |j(                  z
  z
        dk(  s,|j+                  |       > |} G fdd      }|j-                  |       |r|||<   |D ]  }d}|j.                  D ][  }|j                  |      }|st        || j                        }||j0                  |   k7  s@t3        |j0                  |         rYd} n |rs |j4                  di |}|r| j6                  r| j6                  |z   }|j8                  xr |j:                  }|j<                  s
|s||z   }}n|}| j                  %| j                  j?                  |tA        |             tA        |      c S  y
)zGenerate a route from a set of keywords

        Returns the url text, or None if no URL could be generated.

        .. code-block:: python

            m.generate(controller='content',action='view',id=10)

        T_append_slashr(   contentrU   r
   _environSCRIPT_NAMEr#   Nutf8   :r   :r   Fr   c                   *    e Zd Zd Zd Z fdZd Zy)"Mapper.generate.<locals>.KeySorterc                     || _         y rq   )r=   )r   r=   r   s      r   rA   z+Mapper.generate.<locals>.KeySorter.__init__P  s	    "DHr   c                 T    | j                  | j                  |j                        dk  S )Nr   )_keysortr=   )r   others     r   __lt__z)Mapper.generate.<locals>.KeySorter.__lt__S  s     ==599=AAr   c                    |j                   }|j                   }t        |z        }t        |z        }|dk(  r|dk(  ry|dk(  ry|dk(  ry| j                  ||      dk7  r| j                  ||      S t        |z        t        |z        k(  r$| j                  t        |      t        |            S | j                  t        |z        t        |z              S )zOSorts two sets of sets, to order them ideally for
                    matching.r   r   r   )r   r   _compare)r   ablendiffalendiffbr   s        r   r%  z+Mapper.generate.<locals>.KeySorter._keysortV  s     		A		A"4!8}H"4!8}H1}Q   1}!  1}  }}Xx8A=#}}Xx@@ 4!8}D1H5#}}SVSV<<  $}}S]CqMJJr   c                     ||k  ry||k  ryy)Nr   r   r   r9   )r   obj1obj2s      r   r)  z+Mapper.generate.<locals>.KeySorter._comparey  s    d{!  r   N)r3   r4   r5   rA   r'  r%  r)  )r   s   r   	KeySorterr"  N  s    #B!KF!r   r1  )rO   r9   )!r   r   r   r   popr   r?   rD   r   encoder   PY3rF   r   r   r   	frozensetr   r   r   dotkeysr   sortr   r   r   generater   r   externalabsoluteputr   )r   r   r    r   script_namer(   rU   	cache_keycache_key_script_namer   r   keylist	sortcachecachesetcachekey	cachelistnewlistr   r1  failrO   kvalrK   external_staticr   s                           @r   r8  zMapper.generate   s    !!%)E/"}}5(&/l#u$")h))J5;G#!-0K\\mt||;,,}5KKYY|T2
8T*
 MM$'..v6MM% ''/0	 ==$ww(,		;3E3Eg3N3<3> )?% 4?	(J% --##$94@Cd{

DMM:
FDMM2]]&&z2Pdmm6G6GR6P
$)~~f5  ,NN3r
+ 	)t&==&MM(+	GGHG  *u}}u}}4t;<ANN5)* G1! 1!f LLYL'&-	(#  	ED yy~!$65>>#..$U^^C%89D !5>>*E*D;;;;-D"',,"A5>>~~o&-D/C#C==,MM%%c3t954y 7	8 r   c           	      v   |j                  di       }|j                  di       }|j                  di       }|j                  dd      }|j                  dd      }|j                  dd      }	|	||	d   d	|	d
   d}|d|	d
   z  }n|d}|d}d|d<   |j                  ddi       d }
 |
|i       } |
|i       } |
|i       }|j                  dg       j                  dd       |j                  dg       j                  dd       |j                  dg       j                  dd       t	        |      }t	        |      }d|z   }|r|dk7  r	|dz   |z   }nd|z   }|}|dz   }|dz   }|j                  d|      |||	|j                  d      dd|v r|d   d<   fd}t        j                  |      D ]  \  }dk7  xr |j                  d      xs d} |      }|D ]F  }||d <   ||d!|} | j                  d"|z   |d|d#fi |  | j                  ||d|fi | H |sx||d <    | j                  d$|z  fi |  | j                  |fi |   | j                  d"|z   |z   |d#z   fd%d&dgid'  | j                  ||z   |fd%d&dgid' t        j                  |      D ]s  \  } |      }|D ]a  }d(|z   }||d <   |dk(  r|}|d#z   }n|d|}|d!z   |z   }|d|d#} | j                  d"|z   |z   |fi |  | j                  ||z   |fi | c u d)}t        j                  |      D ]  \  } |      }d*|i|d<   d+vr|j                  d      }nd}|D ]I  }||d <    | j                  d"||d!||d|d#fi |  | j                  ||d!||d|fi | K |s~||d <    | j                  d$|z  fi |  | j                  |fi |   |d      }d,|d <   d*|i|d<    | j                  d"|z   |z   |d#z   fi |  | j                  ||z   |fi | y)-a  Generate routes for a controller resource

        The member_name name should be the appropriate singular version
        of the resource given your locale and used with members of the
        collection. The collection_name name will be used to refer to
        the resource collection methods and should be a plural version
        of the member_name argument. By default, the member_name name
        will also be assumed to map to a controller you create.

        The concept of a web resource maps somewhat directly to 'CRUD'
        operations. The overlying things to keep in mind is that
        mapping a resource is about handling creating, viewing, and
        editing that resource.

        All keyword arguments are optional.

        ``controller``
            If specified in the keyword args, the controller will be
            the actual controller used, but the rest of the naming
            conventions used for the route names and URL paths are
            unchanged.

        ``collection``
            Additional action mappings used to manipulate/view the
            entire set of resources provided by the controller.

            Example::

                map.resource('message', 'messages', collection={'rss':'GET'})
                # GET /message/rss (maps to the rss action)
                # also adds named route "rss_message"

        ``member``
            Additional action mappings used to access an individual
            'member' of this controllers resources.

            Example::

                map.resource('message', 'messages', member={'mark':'POST'})
                # POST /message/1/mark (maps to the mark action)
                # also adds named route "mark_message"

        ``new``
            Action mappings that involve dealing with a new member in
            the controller resources.

            Example::

                map.resource('message', 'messages', new={'preview':'POST'})
                # POST /message/new/preview (maps to the preview action)
                # also adds a url named "preview_new_message"

        ``path_prefix``
            Prepends the URL path for the Route with the path_prefix
            given. This is most useful for cases where you want to mix
            resources or relations between resources.

        ``name_prefix``
            Perpends the route names that are generated with the
            name_prefix given. Combined with the path_prefix option,
            it's easy to generate route names and paths that represent
            resources that are in relations.

            Example::

                map.resource('message', 'messages', controller='categories',
                    path_prefix='/category/:category_id',
                    name_prefix="category_")
                # GET /category/7/message/1
                # has named route "category_message"

        ``requirements``

           A dictionary that restricts the matching of a
           variable. Can be used when matching variables with path_prefix.

           Example::

                map.resource('message', 'messages',
                     path_prefix='{project_id}/',
                     requirements={"project_id": R"\d+"})
                # POST /01234/message
                #    success, project_id is set to "01234"
                # POST /foo/message
                #    404 not found, won't be matched by this route


        ``parent_resource``
            A ``dict`` containing information about the parent
            resource, for creating a nested resource. It should contain
            the ``member_name`` and ``collection_name`` of the parent
            resource. This ``dict`` will
            be available via the associated ``Route`` object which can
            be accessed during a request via
            ``request.environ['routes.route']``

            If ``parent_resource`` is supplied and ``path_prefix``
            isn't, ``path_prefix`` will be generated from
            ``parent_resource`` as
            "<parent collection name>/:<parent member name>_id".

            If ``parent_resource`` is supplied and ``name_prefix``
            isn't, ``name_prefix`` will be generated from
            ``parent_resource`` as  "<parent member name>_".

            Example::

                >>> from routes.util import url_for
                >>> m = Mapper()
                >>> m.resource('location', 'locations',
                ...            parent_resource=dict(member_name='region',
                ...                                 collection_name='regions'))
                >>> # path_prefix is "regions/:region_id"
                >>> # name prefix is "region_"
                >>> url_for('region_locations', region_id=13)
                '/regions/13/locations'
                >>> url_for('region_new_location', region_id=13)
                '/regions/13/locations/new'
                >>> url_for('region_location', region_id=13, id=60)
                '/regions/13/locations/60'
                >>> url_for('region_edit_location', region_id=13, id=60)
                '/regions/13/locations/60/edit'

            Overriding generated ``path_prefix``::

                >>> m = Mapper()
                >>> m.resource('location', 'locations',
                ...            parent_resource=dict(member_name='region',
                ...                                 collection_name='regions'),
                ...            path_prefix='areas/:area_id')
                >>> # name prefix is "region_"
                >>> url_for('region_locations', area_id=51)
                '/areas/51/locations'

            Overriding generated ``name_prefix``::

                >>> m = Mapper()
                >>> m.resource('location', 'locations',
                ...            parent_resource=dict(member_name='region',
                ...                                 collection_name='regions'),
                ...            name_prefix='')
                >>> # path_prefix is "regions/:region_id"
                >>> url_for('locations', region_id=51)
                '/regions/51/locations'

        r2   r+   r   r'   NrC   parent_resourcer%   z/:member_name_idz%s_r#   rb   r   c                     t        j                  |       D ]4  \  }}|j                  |j                         g       j	                  |       6 |S )ziSwap the keys and values in the dict, and uppercase the values
            from the dict during the swap.)rD   rE   r   upperr   )dctnewdctrO   r   s       r   swapzMapper.resource.<locals>.swapV  sF      MM#. ?S!!#))+r299#>?Mr   rg   r   r   rk   r   rm   r   r   z/newz/:(id)r(   _filter)r(   _member_name_collection_name_parent_resourcerQ  requirementsc                 ^    j                         }dk7  rd| j                         gi|d<   |S )zUReturns a new dict to be used for all route creation as the
            route optionsanyrY   ry   )rz   rM  )methoptsrY   optionss     r   requirements_forz)Mapper.resource.<locals>.requirements_for{  s3     <<>D&.%?\"Kr   rU   rT   
formatted_z
.:(format)z%s.:(format)r
   rY   )rU   ry   new_z[^\/]+(?<!\\)id)rg   rb   rW  r   )	r2  r   r   insertr   r?   rD   rE   rI   )r   rJ  r%   r0   r2   r+   r   r'   rC   rI  rP  collection_methodsmember_methodsnew_methodsr(   rK   collection_pathnew_pathmember_pathr[  lstprimaryroute_optionsrU   
route_namer   formatted_pathrequirements_regexprY   rZ  s                               @@r   resourcezMapper.resource  s   f ZZb1
Hb)jj#jj5jj5 **%6= &"-<=N-O-<]-KM"#om&DD" "  v

E5>"	 "*b1fb)3m 	%%fb188HE!!%,33Ax@!!(B/66q(C #?3
#K0K';#-$z1D#D&=Xo !**\:>' / /zz),
 V#&,^&<GN#	 ==);< 	?KFC53771:>$G,V4M .*0h'*5vO
\J6-v97 I:GIZOV)L .,.. *1h'^o=OO_>>	?" 	\K//A$|3	@<C!)E7 3	@7>	@ 	[?2O 	P#E70C	PGN	P ==5 	HKFC,V4M H+*0h'U?#D%-%<N&.7D!C<$.D:BF%KN\K7$> .,.[4/GGH	H  1 ==8 	;KFC,V4M-13F,GM.)33''!* O*0h'K4?A2=vF.  -. +v{K(3V<O@MOO *1h'^k9K]K[:M:%	;* )/"(h)-/B(Cn%\K/+= </	B3@	B[;.M}Mr   c                 >   g d}dg}|j                  dd      }i i }	}||z   D ]  }
|
|v s||
   ||
<    d|d<   |D ]  }
|
|vs||
   |	|
<     | j                  |fi |	 | j                  d   } | j                  dt        |      z  |fi | d|_        ||_        y	)
a  Add a redirect route to the mapper

        Redirect routes bypass the wrapped WSGI application and instead
        result in a redirect being issued by the RoutesMiddleware. As
        such, this method is only meaningful when using
        RoutesMiddleware.

        By default, a 302 Found status code is used, this can be
        changed by providing a ``_redirect_code`` keyword argument
        which will then be used instead. Note that the entire status
        code string needs to be present.

        When using keyword arguments, all arguments that apply to
        matching will be used for the match, while generation specific
        options will be used during generation. Thus all options
        normally available to connected Routes may be used with
        redirect routes as well.

        Example::

            map = Mapper()
            map.redirect('/legacyapp/archives/{url:.*}', '/archives/{url}')
            map.redirect('/home/index', '/',
                         _redirect_code='301 Moved Permanently')

        )r   r   r   rQ  _redirect_codez	302 FoundT_staticr   z_redirect_%sN)r2  rI   r   r^  redirectredirect_status)r   
match_pathdestination_pathr   r0   	both_argsgen_argsstatus_codegen_dict
match_dictrO   match_routes               r   rp  zMapper.redirect  s    6 <	;jj!1;?!2* x' 	,Cf} &s	, #  	.C(""(+
3	. 	Z.:.nnR(^bo57G 	!	!#&1#r   )r#   rq   )NN)r3   r4   r5   r6   r   rA   r   r   r   r   propertyr   r   r   rI   r   r   r   r  r  r  r8  rl  rp  r9   r   r   r}   r}   ;  s    
 (7$"TD^!@,$"w1G)V%6#p("T,+"Z;&z..cJdNL	22r   r}   )r6   r   	itertoolsr   r   r   
repoze.lrur   rD   r   r   routes.utilr   r   r   routes.router	   r7   r8   r   objectr   r   rW   r}   r9   r   r   <module>r     so       	   
 ! 
  0 5pf pf[ [~B2_ B2r   