
    gF                        d Z ddlZddlmZ ddlmZmZmZmZ ddlm	Z
mZ ddlmZ ddlmZmZ d Zd	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 G d de      Z G d de      Z G d de      Z G d de      Z ed      xZZ eej>                        e_         edej@                        e_          edejB                  d       e_!         edejD                        e_"         ed!ejF                        e_$         ed!ejJ                        e_&         ed"ejN                  d#       e_'         eejP                        e_)         ed$ejT                  d%       e_+         edejX                        e_,         ed&ejZ                  d'       e_.         ed$ej^                  d(       e_/         eej`                        e_0         eejb                        e_1         e       e_	         eejd                        e_3         ed&ejh                  d)      e_         ed$ejj                        e_5         edejl                        e_6         edejn                        e_7         ed&ejp                  d*       e_8         ed$ejr                        e_9         edejt                        e_:         edejP                        e_(         edejv                  d       e_;         edejx                  d       e_<         ed&ejz                  d       e_=         ed&ej|                  d+       e_         ed&ej~                  d+       e_         ed&ej                  d,       e_A        [[[[y)-a  CherryPy tools. A "tool" is any helper, adapted to CP.

Tools are usually designed to be used in a variety of ways (although
some may only offer one if they choose):

    Library calls
        All tools are callables that can be used wherever needed.
        The arguments are straightforward and should be detailed within the
        docstring.

    Function decorators
        All tools, when called, may be used as decorators which configure
        individual CherryPy page handlers (methods on the CherryPy tree).
        That is, "@tools.anytool()" should "turn on" the tool via the
        decorated function's _cp_config attribute.

    CherryPy config
        If a tool exposes a "_setup" callable, it will be called
        once per Request (if the feature is "turned on" via config).

Tools may be implemented as any object with a namespace. The builtins
are generally either modules or instances of the tools.Tool class.
    Nexpose)cptoolsencodingstatic	jsontools)sessions
xmlrpcutil)caching)
auth_basicauth_digestc                     ddl }t        | |j                        r| j                  } | j                  }|j
                  d|j                   S )z?Return the names of all static arguments to the given function.r   N)types
isinstance
MethodType__func____code__co_varnamesco_argcount)funcr   cos      &/opt/Tautulli/lib/cherrypy/_cptools.py_getargsr   "   s>     $(()}}	B>>/2>>**    zCherryPy Tools cannot be turned on directly. Instead, turn them on via config, or use them as decorators on your page handlers.c                   j    e Zd ZdZdZddZed        Zej                  d        Zd Z	ddZ
d	 Zd
 Zy)ToolzA registered function for use with CherryPy request-processing hooks.

    help(tool.callable) should give you more information about this
    Tool.
    toolsNc                     || _         || _        || _        || _        | j                  j                  | _        | j                          y N)_pointcallable_name	_priority__doc___setargs)selfpointr!   nameprioritys        r   __init__zTool.__init__;   s8     
!}},,r   c                      t        t              r   AttributeError_attr_errorr&   s    r   onzTool.onC       [))r   c                      t        t              r   r,   )r&   values     r   r0   zTool.onG   r1   r   c                 8   	 t        | j                        D ]  }t        | |d        y# t        t        f$ rL t        | j                  d      r3t        | j                  j                        D ]  }t        | |d        Y yY yt        $ r Y yt        $ r Y yw xY w)z,Copy func parameter names to obj attributes.N__call__)	r   r!   setattr	TypeErrorr-   hasattrr5   NotImplementedError
IndexError)r&   args     r   r%   zTool._setargsK   s    	. )c4()>* 	-t}}j1#DMM$:$:; -CD#t,- 2 # 	  		s   '* ABBBBc                     |r|j                         }ni }t        j                  j                  j                  | j
                     }| j                  |v r|j                  || j                            d|v r|d= |S )z5Return a dict of configuration entries for this Tool.r0   )copycherrypyservingrequesttoolmaps	namespacer"   update)r&   dconftms       r   _merged_argszTool._merged_args^   sh    668DD%%..t~~>::KK4::'4<T
r   c                 J     |rt        d j                  z         fd}|S )zCompile-time decorator (turn on the tool in config).

        For example::

            @expose
            @tools.proxy()
            def whats_my_base(self):
                return cherrypy.request.base
        zQThe %r Tool does not accept positional arguments; you must use keyword arguments.c                     t        | d      si | _        j                  dz   j                  z   dz   }d| j                  |dz   <   j	                         D ]  \  }}|| j                  ||z   <    | S )N
_cp_config.Tr0   )r8   rJ   rB   r"   items)fsubspacekvkwargsr&   s       r   tool_decoratorz%Tool.__call__.<locals>.tool_decorator}   sr    1l+!~~+djj83>H,0ALLD) /1-.X\*/Hr   )r7   r"   )r&   argsrQ   rR   s   ` ` r   r5   zTool.__call__n   s4      I"jj) * *	 r   c                 $   | j                         }|j                  dd      }|!t        | j                  d| j                        }t        j                  j                  j                  j                  | j                  | j                  fd|i| yHook this tool into cherrypy.request.

        The standard CherryPy request object will automatically call
        this method when the tool is "turned on" in config.
        r)   N)rG   popgetattrr!   r#   r>   r?   r@   hooksattachr    r&   rE   ps      r   _setupzTool._setup   |       "HHZ&9z4>>BA  &&--dkk4== 	B78	B<@	Br   )N2   r   )__name__
__module____qualname__r$   rB   r*   propertyr0   setterr%   rG   r5   r]    r   r   r   r   2   sT     I * * YY* *& 2Br   r   c                   *    e Zd ZdZddZd Zd Zd Zy)HandlerToola  Tool which is called 'before main', that may skip normal handlers.

    If the tool successfully handles the request (by setting
    response.body), if should return True. This will cause CherryPy to
    skip any 'normal' page handler. If the tool did not handle the
    request, it should return False to tell CherryPy to continue on and
    call the normal page handler. If the tool is declared AS a page
    handler (see the 'handler' method), returning False will raise
    NotFound.
    Nc                 4    t         j                  | d||       y )Nbefore_handlerr   r*   r&   r!   r(   s      r   r*   zHandlerTool.__init__   s    d,h=r   c                 ,     t          fd       }|S )zUse this tool as a CherryPy page handler.

        For example::

            class Root:
                nav = tools.staticdir.handler(section="/nav", dir="nav",
                                              root=absDir)
        c                       j                   i j                        }|st        j                         t        j                  j
                  j                  S r   )r!   rG   r>   NotFoundr?   responsebody)akwhandledrS   rQ   r&   s      r   handle_funcz(HandlerTool.handler.<locals>.handle_func   sM    #dmmTGT->->v-FGG''))##,,111r   r   )r&   rS   rQ   rt   s   ``` r   handlerzHandlerTool.handler   s     
	2 
	2
 r   c                 h     | j                   di |r d t        j                  j                  _        y y Nre   )r!   r>   r?   r@   ru   )r&   rQ   s     r   _wrapperzHandlerTool._wrapper   s,    4=="6"/3H$$, #r   c                 $   | j                         }|j                  dd      }|!t        | j                  d| j                        }t        j                  j                  j                  j                  | j                  | j                  fd|i| yrU   )rG   rW   rX   r!   r#   r>   r?   r@   rY   rZ   r    rx   r[   s      r   r]   zHandlerTool._setup   r^   r   r   )r`   ra   rb   r$   r*   ru   rx   r]   re   r   r   rg   rg      s    	>"4Br   rg   c                   "    e Zd ZdZ	 	 ddZd Zy)HandlerWrapperToola)  Tool which wraps request.handler in a provided wrapper function.

    The 'newhandler' arg must be a handler wrapper function that takes a
    'next_handler' argument, plus ``*args`` and ``**kwargs``. Like all
    page handler
    functions, it must return an iterable for use as cherrypy.response.body.

    For example, to allow your 'inner' page handlers to return dicts
    which then get interpolated into a template::

        def interpolator(next_handler, *args, **kwargs):
            filename = cherrypy.request.config.get('template')
            cherrypy.response.template = env.get_template(filename)
            response_dict = next_handler(*args, **kwargs)
            return cherrypy.response.template.render(**response_dict)
        cherrypy.tools.jinja = HandlerWrapperTool(interpolator)
    Nc                 <    || _         || _        || _        || _        y r   )
newhandlerr    r"   r#   )r&   r}   r'   r(   r)   s        r   r*   zHandlerWrapperTool.__init__   s    $
!r   c                      t         j                  j                  j                   fd}|t         j                  j                  _        y )Nc                  0     j                   g| i |S r   )r}   )rS   rQ   	innerfuncr&   s     r   wrapz)HandlerWrapperTool.callable.<locals>.wrap   s    "4??9>t>v>>r   )r>   r?   r@   ru   )r&   rS   rQ   r   r   s   `   @r   r!   zHandlerWrapperTool.callable   s3    $$,,44		?+/  (r   )ri   Nr_   )r`   ra   rb   r$   r*   r!   re   r   r   r{   r{      s    $ AE"0r   r{   c                   $    e Zd ZdZddZd Zd Zy)	ErrorToolzATool which is used to replace the default request.error_response.Nc                 4    t         j                  | d ||       y r   rj   rk   s      r   r*   zErrorTool.__init__   s    dD(D1r   c                 D     | j                   di | j                          y rw   )r!   rG   r/   s    r   rx   zErrorTool._wrapper   s    ,))+,r   c                 V    | j                   t        j                  j                  _        y)rV   N)rx   r>   r?   r@   error_responser/   s    r   r]   zErrorTool._setup   s     37--  /r   r   )r`   ra   rb   r$   r*   rx   r]   re   r   r   r   r      s    K2-@r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)SessionToolar  Session Tool for CherryPy.

    sessions.locking
        When 'implicit' (the default), the session will be locked for you,
        just before running the page handler.

        When 'early', the session will be locked before reading the request
        body. This is off by default for safety reasons; for example,
        a large upload would block the session, denying an AJAX
        progress meter
        (`issue <https://github.com/cherrypy/cherrypy/issues/630>`_).

        When 'explicit' (or any other value), you need to call
        cherrypy.session.acquire_lock() yourself before using
        session data.
    c                 N    t         j                  | dt        j                         y )Nbefore_request_body)r   r*   	_sessionsinitr/   s    r   r*   zSessionTool.__init__  s    d19>>Br   c                 T    t         j                  j                  j                          y r   )r>   r?   sessionacquire_lockr/   s    r   _lock_sessionzSessionTool._lock_session  s      --/r   c                 \   t         j                  j                  j                  }| j	                         }|j                  dd      }|!t        | j                  d| j                        } |j                  | j                  | j                  fd|i| |j                  dd      }|dk(  r|j                  d| j                         n%|dk(  r|j                  d| j                  d	       n	 |j                  d
t        j                         |j                  dt        j                         y)rV   r)   Nlockingimplicitri   earlyr   <   r)   before_finalizeon_end_request)r>   r?   r@   rY   rG   rW   rX   r!   r#   rZ   r    r   r   saveclose)r&   rY   rE   r\   r   s        r   r]   zSessionTool._setup  s       ((..  "HHZ&9z4>>BAT[[$--D!DtD((9j1j LL)4+=+=>LL.0B0B"$  & &	7%y7r   c                     t         j                  j                  }|j                          dt	        fd| j                         j                         D              }t        j                  di | y)z<Drop the current session and make a new one (with a new id).)pathpath_headerr(   timeoutdomainsecurec              3   4   K   | ]  \  }}|v r||f  y wr   re   ).0rO   rP   relevants      r   	<genexpr>z)SessionTool.regenerate.<locals>.<genexpr><  s(      
1H} F
s   Nre   )	r>   r?   r   
regeneratedictrG   rL   r   set_response_cookie)r&   sessrE   r   s      @r   r   zSessionTool.regenerate5  sb    '' P 
))+113
 

 	%%--r   N)r`   ra   rb   r$   r*   r   r]   r   re   r   r   r   r      s    "C08<.r   r   c                   (    e Zd ZdZddiZed        Zy)XMLRPCControllera]  A Controller (page handler collection) for XML-RPC.

    To use it, have your controllers subclass this base class (it will
    turn on the tool for you).

    You can also supply the following optional config entries::

        tools.xmlrpc.encoding: 'utf-8'
        tools.xmlrpc.allow_none: 0

    XML-RPC is a rather discontinuous layer over HTTP; dispatching to the
    appropriate handler must first be performed according to the URL, and
    then a second dispatch step must take place according to the RPC method
    specified in the request body. It also allows a superfluous "/RPC2"
    prefix in the URL, supplies its own handler args in the body, and
    requires a 200 OK "Fault" response instead of 404 when the desired
    method is not found.

    Therefore, XML-RPC cannot be implemented for CherryPy via a Tool alone.
    This Controller acts as the dispatch target for the first half (based
    on the URL); it then reads the RPC method from the request body and
    does its own second dispatch step based on that method. It also reads
    body params, and returns a Fault on error.

    The XMLRPCDispatcher strips any /RPC2 prefix; if you aren't using /RPC2
    in your URL's, you can safely skip turning on the XMLRPCDispatcher.
    Otherwise, you need to use declare it in config::

        request.dispatch: cherrypy.dispatch.XMLRPCDispatcher()
    ztools.xmlrpc.onTc                    t        j                         \  }}| }t        |      j                  d      D ]  }t	        ||d       } |rt	        |dd      r |||z   i |}nt        dz        t        j                  j                  j                  d   j                  di       }t        j                  ||j                  dd      |j                  d	d
             t        j                  j                  j                  S )NrK   exposedFzmethod "%s" is not supportedr   xmlrpcr   zutf-8
allow_noner   )_xmlrpcprocess_bodystrsplitrX   	Exceptionr>   r?   r@   rA   getrespondro   rp   )	r&   vpathparams	rpcparams	rpcmethod
subhandlerattrrp   rE   s	            r   defaultzXMLRPCController.defaulti  s    &335	9
	N((- 	9D T48J	9 '*i?	 1=f=D :TABB''009==hKW5q1	3 ((---r   N)r`   ra   rb   r$   rJ   r   r   re   r   r   r   r   D  s&    D $T*J. .r   r   c                       e Zd Zy)SessionAuthToolN)r`   ra   rb   re   r   r   r   r     s    r   r   c                   *    e Zd ZdZd Zde_        d Zy)CachingToolzCaching Tool for CherryPy.c                     t         j                  j                  }t        j                  di |rd |_        y |j                  r-|j                  j                  dt        j                  d       y y )Nr   d   r   re   )
r>   r?   r@   _cachingr   ru   	cacheablerY   rZ   
tee_output)r&   rQ   r@   s      r   rx   zCachingTool._wrapper  s\    ""**<<!&!"GO  $$%68K8K.1 % 3 !r   Z   c                     | j                         }|j                  dd      }t        j                  j                  j
                  j                  d| j                  fd|i| y)z#Hook caching into cherrypy.request.r)   Nri   )rG   rW   r>   r?   r@   rY   rZ   rx   r[   s      r   r]   zCachingTool._setup  s\      "HHZ&  &&--.> 	B78	B<@	Br   N)r`   ra   rb   r$   rx   r)   r]   re   r   r   r   r     s    $3 HBr   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)ToolboxzA collection of Tools.

    This object also functions as a config namespace handler for itself.
    Custom toolboxes should be added to each Application's toolboxes
    dict.
    c                     || _         y r   )rB   )r&   rB   s     r   r*   zToolbox.__init__  s	    "r   c                     t        |t              r$|j                  ||_        | j                  |_        t        j                  | ||       y r   )r   r   r"   rB   object__setattr__)r&   r(   r3   s      r   r   zToolbox.__setattr__  s:    eT"{{"""nnEO4u-r   c                 x    i xt         j                  j                  j                  | j                  <   fd}|S )z9Populate request.toolmaps from tools specified in config.c                 ^    | j                  dd      \  }}j                  |i       }|||<   y )NrK      )r   
setdefault)rO   rP   toolnamer;   bucketmaps        r   populatez#Toolbox.__enter__.<locals>.populate  s/    GGCOMHc^^Hb1FF3Kr   )r>   r?   r@   rA   rB   )r&   r   r   s     @r   	__enter__zToolbox.__enter__  s4    BDD  ))$..9C	 r   c                    t         j                  j                  j                  j	                  | j
                        }|rH|j                         D ]4  \  }}|j	                  dd      st        | |      }|j                          6 yy)z/Run tool._setup() for each tool in our toolmap.r0   FN)	r>   r?   r@   rA   r   rB   rL   rX   r]   )r&   exc_typeexc_valexc_tbr   r(   settingstools           r   __exit__zToolbox.__exit__  sl    &&//33DNNC"%))+ "h<<e,"4.DKKM" r   c                       fd}|S )zb
        Return a decorator which registers the function
        at the given hook point.
        c                 t    j                  d| j                        }t        | fi }t        ||       | S )Nr(   )r   r`   r   r6   )r   	attr_namer   rQ   r'   r&   s      r   	decoratorz#Toolbox.register.<locals>.decorator  s9    

64==9It.v.DD)T*Kr   re   )r&   r'   rQ   r   s   ``` r   registerzToolbox.register  s    
	
 r   N)	r`   ra   rb   r$   r*   r   r   r   r   re   r   r   r   r     s     #."
r   r   r   on_start_resourcer      r   before_error_responser   r   r   K   ri   F   P   r   r   r      )Br$   r>   cherrypy._helperr   cherrypy.libr   r   r   r   r	   r   r
   r   r   r   r   r   r   r.   r   r   rg   r{   r   r   r   r   r   r   default_toolbox_dsession_authallowproxyresponse_headerslog_tracebacklog_tracebackslog_request_headerslog_headers	log_hooksredirecterr_redirectvalidate_etagsetagsdecodeResponseEncoderencodegzip	staticdir
staticfileon_errorr   r   expiresignore_headersreferertrailing_slashflattenacceptautovaryjson_injson_out
basic_authdigest_authconvert_paramsr   re   r   r   <module>r     s2  0  # = = E , 0+F `B6 `BF/B$ /Bd0 0D@ @*C.$ C.L;.v ;.|	k 	B$ B.0f 0f w' '"!'"6"67#W]]3%w}}rB.0H0HI 0'2G2GH -w/J/JK$g&7&7#FG,,-!7#9#9BG&8	!8#;#;bI	
 (--"
=6++,F--.mg&&'	)8<<C
#X%5%56
.0F0FG '9
)7+A+ABO #W__5
$gnn5	&(8(89&(8(81E'):):RH
#Y%7%7"E%z'<'<qI&(?(?!L!7#9#9BG	6r   