
    g+                     p    d Z ddlZddlZddlmZmZmZmZmZ ddlm	Z	m
Z
  G d de      Z G d de      Zy)	z&CherryPy Application and Tree objects.    N)	_cpconfig
_cplogging
_cprequest_cpwsgitools)httputilreprconfc                       e Zd ZdZdZ	 i Z	  ej                         Zde	j                  iZdZ	 dZ	 ej                  Zej"                  ZdZddZd ZdZed        Zej2                  d	        Zd
 ZddZd Zd Zd Zy)Applicationa!  A CherryPy Application.

    Servers and gateways should not instantiate Request objects
    directly. Instead, they should ask an Application object for a
    request object.

    An instance of this class may also be used as a WSGI callable (WSGI
    application object) for itself.
    Nr   Fc                     t        j                  t               t        j                  j
                         _        | _        | _        t        j                          _
         j                  j                          _         fd j                  d<    j                  j                   j                  d<    j                  j                  j                          _        |r j!                  |       yy)z'Initialize Application with given root.c                 2    t        j                  | |      S N)setattrlog)kvselfs     %/opt/Tautulli/lib/cherrypy/_cptree.py<lambda>z&Application.__init__.<locals>.<lambda>>   s    gdhh1.E     r   wsgiN)r   
LogManageridcherrypyr   logger_rootrootscript_namer   	CPWSGIAppwsgiapp
namespacescopynamespace_handler	__class__configmerge)r   r   r   r$   s   `   r   __init__zApplication.__init__6   s    ((D8<<3K3KL	&((.//..0!E"&,,"@"@nn++002JJv r   c                     | j                   d| j                  j                  d| j                  d| j                  dS )z6Generate a representation of the Application instance..(z, ))
__module__r#   __name__r   r   r   s    r   __repr__zApplication.__repr__E   s.    "&//4>>3J3J"&))T-=-=? 	?r   a  The URI "mount point" for this app. A mount point
    is that portion of the URI which is constant for all URIs that are
    serviced by this application; it does not include scheme, host, or proxy
    ("virtual host") portions of the URI.

    For example, if script_name is "/my/cool/app", then the URL
    "http://www.example.com/my/cool/app/page1" might be handled by a
    "page1" method on the root object.

    The value of script_name MUST NOT end in a slash. If the script_name
    refers to the root of the URI, it MUST be an empty string (not "/").

    If script_name is explicitly set to None, then the script_name will be
    provided for each call from request.wsgi_environ['SCRIPT_NAME'].
    c                     | j                   | j                   S t        j                  j                  j                  d   j                  d      S )a  The URI "mount point" for this app.

        A mount point is that portion of the URI which is constant for
        all URIs that are serviced by this application; it does not
        include scheme, host, or proxy ("virtual host") portions of the
        URI.

        For example, if script_name is "/my/cool/app", then the URL "

        http://www.example.com/my/cool/app/page1"
        might be handled by a
        "page1" method on the root object.

        The value of script_name MUST NOT end in a slash. If the script_name
        refers to the root of the URI, it MUST be an empty string (not "/").

        If script_name is explicitly set to None, then the script_name will be
        provided for each call from request.wsgi_environ['SCRIPT_NAME'].
        SCRIPT_NAME/)_script_namer   servingrequestwsgi_environrstripr-   s    r   r   zApplication.script_nameZ   sF    * ($$$ ''44]CJJ3OOr   c                 8    |r|j                  d      }|| _        y )Nr1   )r6   r2   )r   values     r   r   zApplication.script_namev   s    LL%E!r   c                     t        j                  | j                  |       | j                  | j                  j	                  di              y)z(Merge the given config into self.config.r1   N)r   r%   r$   r    get)r   r$   s     r   r%   zApplication.merge|   s1    V, 	R01r   c                     |xs d}|rS| j                   j                  |i       }||v r||   S |j                  d      }|dk(  r	 |S |dk(  r|dk7  rd}n|d| }|rS|S )z>Return the most-specific value for key along path, or default.r1   r   N)r$   r:   rfind)r   pathkeydefaulttrailnodeconf	lastslashs          r   find_configzApplication.find_config   s~    {{ub1Hh}$C(IB  aESLjy)  r   c                 ~   | j                  ||||      }| |_        | j                  j                         D ]  \  }}||j                  |<    | j                         }t        j                  j                  ||       t        j                  j                  d       t        j                  j                  d       ||fS )z0Create and return a Request and Response object.acquire_threadbefore_request)request_classapp	toolboxesitemsr    response_classr   r3   loadenginepublish)	r   localremoteschemesprotoreqnametoolboxresps	            r   get_servingzApplication.get_serving   s      ?!^^113 	+MD'#*CNN4 	+ ""$c4( 01 01Dyr   c                     t         j                  j                  }t         j                  j	                  d       	 |j                          t         j                  j                          y# t        $ r t        j                  dd       Y Aw xY w)z3Release the current serving (request and response).after_requestT(   )	tracebackseverityN)	r   r3   r4   rN   rO   close	Exceptionr   clear)r   rT   s     r   release_servingzApplication.release_serving   sf    &&0	6IIK 	   	6LL4"5	6s   A* * BBc                 &    | j                  ||      S )zCall a WSGI-callable.)r   )r   environstart_responses      r   __call__zApplication.__call__   s    ||G^44r    Nr   )r,   r+   __qualname____doc__r   r$   r	   NamespaceSetr    r   r   rJ   r   r   r   RequestrH   ResponserL   relative_urlsr&   r.   script_name_docpropertyr   setterr%   rD   rX   ra   re    r   r   r   r   
   s     D F '&&(J(..)I
C
 G
 &&M((NM?
O  P P6 " "
2&!5r   r   c                   :    e Zd ZdZi Z	 d ZddZd	dZd
dZd Z	y)TreezA registry of CherryPy applications, mounted at diverse points.

    An instance of this class may also be used as a WSGI callable (WSGI
    application object), in which case it dispatches to all mounted
    apps.
    c                     i | _         y)zInitialize registry Tree.N)appsr-   s    r   r&   zTree.__init__   s	    	r   Nc                 B   |t        d      |j                  d      }t        |t              r.|}|dk7  r||j                  k7  rt        d      |j                  }nt        ||      }|dk(  xr |duxr t        |d       }|rxt        j                  j                  t        j                         t        j                  j                  t              d      }t        j                  j                  |      |_        |r|j#                  |       || j$                  |<   |S )a  Mount a new app from a root object, script_name, and config.

        root
            An instance of a "controller class" (a collection of page
            handler methods) which represents the root of the application.
            This may also be an Application instance, or None if using
            a dispatcher other than the default.

        script_name
            A string containing the "mount point" of the application.
            This should start with a slash, and be the path portion of the
            URL at which to mount the given root. For example, if root.index()
            will handle requests to "http://www.example.com:8080/dept/app1/",
            then the script_name argument would be "/dept/app1".

            It MUST NOT end in a slash. If the script_name refers to the
            root of the URI, it MUST be an empty string (not "/").

        config
            A file or dict containing application config.
        Na  The 'script_name' argument may not be None. Application objects may, however, possess a script_name of None (in order to inpect the WSGI environ for SCRIPT_NAME upon each request). You cannot mount such Applications on this Tree; you must pass them to a WSGI server interface directly.r1   rg   zYCannot specify a different script name and pass an Application instance to cherrypy.mountfavicon_icozfavicon.ico)	TypeErrorr6   
isinstancer   r   
ValueErrorhasattrosr>   joingetcwddirname__file__r   
staticfilehandlerrw   r%   ru   )r   r   r   r$   rI   needs_faviconfavicons          r   mountz
Tree.mount   s   , JK K "((-dK(Cb [COO%C => > //KdK0C r! 5$5m44 
 '',,IIKGGOOH-!
 $)#3#3#;#;G#D IIf!$		+
r   c                 D    |j                  d      }|| j                  |<   y)z/Mount a wsgi callable at the given script_name.r1   N)r6   ru   )r   wsgi_callabler   s      r   graftz
Tree.graft
  s"     "((-!.		+r   c                    |E	 t         j                  j                  }t        j                  |j
                  |j                        }	 || j                  v r|S |dk(  ry|d|j                  d       }+# t        $ r Y yw xY w)zzReturn the script_name of the app at the given path, or None.

        If path is None, cherrypy.request is used.
        Nrg   r1   )
r   r3   r4   r   urljoinr   	path_infoAttributeErrorru   r=   )r   r>   r4   s      r   r   zTree.script_name  s    
 <"**22''(;(;(/(9(9;
 tyy rz (C)D  " s   AA4 4	B ?B c                 P   |}t        j                  |j                  dd      |j                  dd            }| j                  |xs d      }| |dg        g S | j                  |   }|j                         }||d<   |t        |j                  d            d |d<    |||      S )z/Pre-initialize WSGI env and call WSGI-callable.r0   rg   	PATH_INFOr1   Nz404 Not Found)r   r   r:   r   ru   r!   lenr6   )r   rc   rd   env1xr>   snrI   s          r   re   zTree.__call__'  s    
 		- < %		+r :<dkc*:?B/Iiim ,,.!##C		#$7$897N++r   rf   )rg   r   )
r,   r+   rh   ri   ru   r&   r   r   r   re   rq   r   r   rs   rs      s-     D?>@/*.,r   rs   )ri   r|   r   r   r   r   r   r   cherrypy.libr   r	   objectr   rs   rq   r   r   <module>r      s4    , 	  F F +j5& j5ZC,6 C,r   