
    ^.hM                         d Z ddlZddlZddlZddlmZ ddlmZ  G d de      Z	 G d de	      Z
 G d	 d
e	      ZddZd Zd Zd Zd Z G d de      Zd Zd ZddZddZd Zy)zUtility functions for use in templates / controllers

*PLEASE NOTE*: Many of these functions expect an initialized RequestConfig
object. This is expected to have been initialized for EACH REQUEST by the web
framework.

    N)urllib)request_configc                       e Zd ZdZy)RoutesExceptionzTossed during Route exceptionsN__name__
__module____qualname____doc__     _/var/www/html/School_Mangement_New/src/backend/venv/lib/python3.12/site-packages/routes/util.pyr   r      s    (r   r   c                       e Zd ZdZy)MatchExceptionz%Tossed during URL matching exceptionsNr   r   r   r   r   r      s    /r   r   c                       e Zd ZdZy)GenerationExceptionz'Tossed during URL generation exceptionsNr   r   r   r   r   r      s    1r   r   c                    |j                   }t        j                  |       D ]4  \  }}t        |t        j                        s!|j                  |      | |<   6 |j                  r|j                  r|st        | ||      S |j                  r|s| S t        | j                  d      |      }|r|j                  d      r| d   dd | d<   | S |r	d| vrd| d<   |j                  d      }|r|d   j                         }	ni }	t        j                  |       D 
cg c]
  \  }}
|
	| }}}
|D ]  }| |= |	j                  |d        |	j                  |        |j                  rt        |	||      }	|	S c c}
}w )z
    Private function that takes a dict, and screens it against the current
    request dict to determine what the dict should look like that is used.
    This is responsible for the requests "memory" of the current.
    
controller/   Nactionindexwsgiorg.routing_args)encodingsix	iteritems
isinstance	text_typeencodeexplicitsub_domains_subdomain_check
as_unicodeget
startswithcopypopupdate)kargsmapperenvironforce_explicitr   keyvalcontroller_name
route_argsmemory_kargsvalue
empty_keyss               r   _screenargsr4      sv    HMM%( .Sc3==)H-E#J. 6--nvw77	 <!8(CO?55c:#L1!"5l	XU2!h34J!!}))+ ),e(<N*#u#NJN $#Jd#$
  'fgF Os   
E1"E1c                 l   |j                   r&| j                  dd      }t        |t        j                        rt        |      }|j                  d      xs |j                  d      }|s| S |j                  d      }|d   }d}t        |      dkD  r|d|d   z   z  }t        j                  d	|j                  z  |      }|r|j                         nd|f\  }	}
t        ||j                        }|r#|	|k7  r||j                  vr|d
z   |
z   |z   | d<   | S ||j                  v s||
|k7  r|
|z   | d<   | S | S )zrScreen the kargs for a subdomain and alter it appropriately depending
    on the current subdomain or lack therof.
sub_domainN	HTTP_HOSTSERVER_NAME:r    r   z^(.+?)\.(%s)$._host)r!   r'   r   r   r   strr$   splitlenrematchdomain_matchgroupsr#   r   sub_domains_ignore)r)   r*   r+   	subdomainfullhost	hostmatchhostportrA   host_subdomaindomains              r   r"   r"   M   sB    IIlD1	i/II;;{+Iw{{=/I LNN3'	|y>AC)A,&&D)F,?,??F38tTly&//:	94V666&_v5<E'N  64446T>#d]E'Nr   c                 8   |rnt        | t        j                        r| j                  |      }nMt        | t        j                        r| }n0t        j                  |       j                  |      }nt	        |       }t
        j                  j                  |d      S )z+A Unicode handling version of urllib.quote.r   )r   r   r   r   r=   r   parsequotestringr   ss      r   
_url_quoterR   p   sm    fcmm,h'A.Af%,,X6AK<<a%%r   c                     |rot        | t        j                        r| j                  |      }|S t        | t        j                        r| }|S t        j                  |       j                  |      }S N)r   r   r   r   rO   s      r   _str_encoderU      sd    fcmm,h'A H .A H f%,,X6AHr   c                  	   |j                  d      }|j                  d      }|j                  dd      }|j                  dd      }dD ]5  }|j                  |      r||= |dz   |v s|j                  |dz         ||<   7 d|v r||d<   t               }d}d	}	|j                  j                  }
d
}t        |       dkD  r|j                  j                  j                  | d         }|sd}	| d   }|j                  d      rEt        |d      r9|j                  j                  d      r|j                  j                  d      |z   }|	r|r|dz  }g }t        j                  |      D ]L  \  }}t        |t        t        f      r|D ]  }|j                  t         j"                  j%                  t        j&                  |      j)                  |
            dt         j"                  j%                  t        j&                  |      j)                  |
                    |j                  t         j"                  j%                  t        j&                  |      j)                  |
            dt         j"                  j%                  t        j&                  |      j)                  |
                   O |dj+                  |      z  }t-        |di       }d|vr9|j/                         }t-        |dd      }||j/                         }ni }d|f|d<   |	sg }|r|j                  j0                  r|j                  |       |j2                  j/                         }|j5                  |       |j6                  r|j7                  |      }|j8                  s/t;        ||j                  |      }nt=        ||j                  |      }|j                  dd      xs |}|j                  dd      xs |}|j                  d|      } |j                  j>                  |i |}||dtA        ||
      z   z  }|s||rV|s!|s|jB                  jE                  d      d   }n|s|jB                  }||jF                  }|d
k7  r|dz  }||dz   |z   |z   }tI        |      s|tK        d|z        |tK        d| d|      |S )a  Generates a URL

    All keys given to url_for are sent to the Routes Mapper instance for
    generation except for::

        anchor          specified the anchor name to be appened to the path
        host            overrides the default (current) host if provided
        protocol        overrides the default (current) protocol if provided
        qualified       creates the URL with the host/port information as
                        needed

    The URL is generated based on the rest of the keys. When generating a new
    URL, values will be used from the current request's parameters (if
    present). The following rules are used to determine when and how to keep
    the current requests parameters:

    * If the controller is present and begins with '/', no defaults are used
    * If the controller is changed, action is set to 'index' unless otherwise
      specified

    For example, if the current request yielded a dict of
    {'controller': 'blog', 'action': 'view', 'id': 2}, with the standard
    ':controller/:action/:id' route, you'd get the following results::

        url_for(id=4)                    =>  '/blog/view/4',
        url_for(controller='/admin')     =>  '/admin',
        url_for(controller='admin')      =>  '/admin/view/2'
        url_for(action='edit')           =>  '/blog/edit/2',
        url_for(action='list', id=None)  =>  '/blog/list'

    **Static and Named Routes**

    If there is a string present as the first argument, a lookup is done
    against the named routes table to see if there's any matching routes. The
    keyword defaults used with static routes will be sent in as GET query
    arg's if a route matches.

    If no route by that name is found, the string is assumed to be a raw URL.
    Should the raw URL begin with ``/`` then appropriate SCRIPT_NAME data will
    be added if present, otherwise the string will be used as the url with
    keyword args becoming GET query args.

    anchorrH   protocolN	qualifiedrW   rH   _	protocol_Fr:   r   Tr   r+   SCRIPT_NAME?=&r   mapper_dictr   _anchorr<   	_protocol#r9   //z9url_for can only return a string, got unicode instead: %sz2url_for could not generate URL. Called with args:  )&r$   r'   r   r*   r   r?   _routenamesr%   hasattrr+   r   r   r   listtupleappendr   rM   rN   r   r   joingetattrr&   hardcode_namesdefaultsr(   filterstaticr"   r4   generaterR   rH   r>   rX   ascii_charactersr   )argsr)   rW   rH   rX   rY   r-   configrouterq   r   url
query_argsr.   r2   r+   ra   
match_dictr0   newargss                       r   url_forr{      sk   X YYx F99VDyyT*H		+t,I " ,99S>c
s7e3s7+E#J	, e%kFEF}}%%H
C
4y1}))--d1g6 Fq'C>>#769#=NN&&}5..$$]3c9Cs

 #e 4 	VHC!#e}5%( \E&-- & 2 23==3E3L3LX3V W & 2 23==3G3N3Nx3X Y/[ \\
 #))"LL..s}}S/A/H/H/RS"LL..s}}S/A/H/H/RS+U V	V sxx
++fi,GW,,,.fmT:"$))+JJ+-z*:&'
}}++!!%(nn))+GNN5! ||,,w/<<*7FMM7K!%@GY-7{{7D)1T;;{H5$fmm$$j<G<sZ111$I ;;$$S)!,D;;DHr>OH?T/D(3.CC S_! #.03#4 5 	5
{!5 	 Jr   c                   "    e Zd ZdZd Zd Zd Zy)URLGeneratora  The URL Generator generates URL's

    It is automatically instantiated by the RoutesMiddleware and put
    into the ``wsgiorg.routing_args`` tuple accessible as::

        url = environ['wsgiorg.routing_args'][0][0]

    Or via the ``routes.url`` key::

        url = environ['routes.url']

    The url object may be instantiated outside of a web context for use
    in testing, however sub_domain support and fully qualified URL's
    cannot be generated without supplying a dict that must contain the
    key ``HTTP_HOST``.

    c                 2    || _         d|vrd|d<   || _        y)a  Instantiate the URLGenerator

        ``mapper``
            The mapper object to use when generating routes.
        ``environ``
            The environment dict used in WSGI, alternately, any dict
            that contains at least an ``HTTP_HOST`` value.

        r]   r:   N)r*   r+   )selfr*   r+   s      r   __init__zURLGenerator.__init__1  s$     '%'GM"r   c                 
   |j                  d      }|j                  d      }|j                  dd      }|j                  dd      }dD ]5  }|j                  |      r||= |dz   |v s|j                  |dz         ||<   7 d|v r||d<   d}d	|v xr |j                  d	      }	d
}
| j                  j                  }d}t	        |      dkD  }|r(| j                  j
                  j                  |d         }|s|rd}
|d   }|j                  d      r9| j                  j                  d      r| j                  j                  d      |z   }|
r|r|dz  }g }t        j                  |      D ]L  \  }}t        |t        t        f      r|D ]  }|j                  t        j                  j!                  t        j"                  |      j%                  |            dt        j                  j!                  t        j"                  |      j%                  |                    |j                  t        j                  j!                  t        j"                  |      j%                  |            dt        j                  j!                  t        j"                  |      j%                  |                   O |dj'                  |      z  }|
sg }|r| j                  j(                  r|j                  |       |j*                  j-                         }|j/                  |       |j0                  r|j1                  |      }|j2                  r|j2                  r|j4                  s|j                  dd      }t7        || j                  | j                        }d|j*                  v rT||d<   nN|	r$t9        || j                  | j                  d      }n(d|v r"t7        || j                  | j                        }n|}|xs |j                  dd      }|xs |j                  dd      }||j                  dd      }| j                  |d<    | j                  j:                  |i |}||dt=        ||      z   z  }|s||rd| j                  vrt?        | j                         | j                  d   }|s|s|d   jA                  d      d   }n|s|d   }||d   }|dk7  r|dz  }|'|d   dk7  r|dz  }|dz   |z   |jC                  d      z   }tE        |      s|tG        d|z        |tG        d|d |      |S )!a  Generates a URL

        All keys given to url_for are sent to the Routes Mapper instance for
        generation except for::

            anchor          specified the anchor name to be appened to the path
            host            overrides the default (current) host if provided
            protocol        overrides the default (current) protocol if provided
            qualified       creates the URL with the host/port information as
                            needed

        rW   rH   rX   NrY   rZ   r[   r\   _use_currentFr:   r   Tr   r]   r^   r_   r`   r6   )r,   rb   r<   rc   _environrd   routes.cached_hostinfor9   re   z1Can only return a string, got unicode instead: %sz*Could not generate URL. Called with args: rf   )$r$   r'   r*   r   r?   rg   r%   r+   r   r   r   ri   rj   rk   r   rM   rN   r   r   rl   rn   ro   r&   r(   rp   rq   externalr"   r4   rr   rR   cache_hostinfor>   lstriprs   r   )r   rt   r)   rW   rH   rX   rY   r-   rv   use_currentrq   r   rw   	more_argsrx   r.   r2   r0   rz   subhostinfos                        r   __call__zURLGenerator.__call__@  s    8$yy 99Z.IIk40	 & 	0Cyy~#J3w%"YYs3w/c
		0 %!)E+$-K%))N2K;;''IM	KK++//Q8EFq'C~~c"t||'7'7'Fll&&}5;3JC!#J$'MM%$8 	ZS%cD%=9), ` * 1 1$*LL$6$6s}}S7I7P7PQY7Z$[$*LL$6$6s}}U7K7R7RS[7\$]3_ !``
 '-- & 2 23==3E3L3LX3V W & 2 23==3E3L3LX3V W/Y Z	Z 388J//CJ;;--%%e,..--/u% <<#ll73G||U^^!++lD9C.w/3||=G $u~~503-%eT[[$,,W[\&*5$++t||L;w{{9d;F57;;w5D";;{D9"&,,GJ&$++&&
>g>C3FH555CH(Y't||;t||,||$<=H	  '--c215'#J/2~C8s?CKDo,szz#>$% '247'8 9 9;%u  
r   c                      | |ddi|S )a8  Generate a route that includes params used on the current
        request

        The arguments for this method are identical to ``__call__``
        except that arguments set to None will remove existing route
        matches of the same name from the set of arguments used to
        construct a URL.
        r   Tr   )r   rt   kwargss      r   currentzURLGenerator.current  s     7777r   N)r   r	   r
   r   r   r   r   r   r   r   r}   r}     s    "{z	8r   r}   c                  N    t        | i |}t               }|j                  |      S )a6  Issues a redirect based on the arguments.

    Redirect's *should* occur as a "302 Moved" header, however the web
    framework may utilize a different method.

    All arguments are passed to url_for to retrieve the appropriate URL, then
    the resulting URL it sent to the redirect function as the URL.
    )r{   r   redirect)rt   r)   targetru   s       r   redirect_tor     s*     d$e$FF??6""r   c                    i }| j                  d      s7| j                  d      dk(  s#d| j                  dd      j                  d      v rd|d<   nd|d<   | j                  d	      r| d	   j                  dd
      d   |d<   nm| j                  d      r	| d   |d<   nS| d   |d<   | j                  d      dk(  r| d   dk7  r/|dxx   d| d   z   z  cc<   n| d   dk7  r|dxx   d| d   z   z  cc<   || d<   |S )ad  Processes the host information and stores a copy

    This work was previously done but wasn't stored in environ, nor is
    it guaranteed to be setup in the future (Routes 2 and beyond).

    cache_hostinfo processes environ keys that may be present to
    determine the proper host, protocol, and port information to use
    when generating routes.

    HTTPSzwsgi.url_schemehttpsHTTP_X_FORWARDED_PROTOr:   z, rX   httpHTTP_X_FORWARDED_HOSTr   r   rH   r7   r8   SERVER_PORT443r9   80r   )r$   r>   )r+   r   s     r   r   r     s"    H{{7w{{+<=H
W[[!92>DDTJ
J&%{{*+"#:;AA$J1M	[	!";/"=1;;()W4}%. C'-*@$@@ }%- C'-*@$@@ (0G$%Or   c                 Z    | g S dfd	 |       }|j                  t        d       |S )z=Scan a directory for python files and use them as controllersc                    g }t        j                  |       D ]  }t         j                  j                  | |      }t         j                  j	                  |      r.t        j                  d|      r|j                  ||dd z          pt         j                  j                  |      s|j                   |||z   dz                 |S )z!Locate controllers in a directoryz^[^_]{1,1}.*\.py$Nr   )prefix)
oslistdirpathrl   isfiler@   rA   rk   isdirextend)dirnamer   controllersfnamefilenamefind_controllerss        r   r   z)controller_scan.<locals>.find_controllers  s    ZZ( 	NEww||GU3Hww~~h'HH159""6E#2J#67x(""#3H;A%<;K$M N	N r   T)r-   reverse)r:   )sortr?   )	directoryr   r   s     @r   controller_scanr     s8    	 #9-Kd+r   c                 N    | "t        | t              r| j                  ||      S | S rT   )r   bytesdecode)r2   r   errorss      r   r#   r#     s'    Zu5||Hf--Lr   c                 ,    | yt        d | D              S )NTc              3   8   K   | ]  }t        |      d k    yw)   N)ord).0cs     r   	<genexpr>z#ascii_characters.<locals>.<genexpr>  s     ,s1v|,s   )all)rP   s    r   rs   rs     s    ~,V,,,r   )FrT   )strict)r   r   r@   r   	six.movesr   routesr   	Exceptionr   r   r   r4   r"   rR   rU   r{   objectr}   r   r   r   r#   rs   r   r   r   <module>r      s    
 	 
  !)i )0_ 02/ 2-` F&	Qhg86 g8T#B.-r   