1
  C   ˆl–Ðz¾”Šj"Tâ‚À½q¶”Å£�þE#Ï,ãe#qº•à?Å„\mÏÄ_ŠI|¥�+Wé3ž¯ÀÃ -ˆ    """Helper functions for CP apps."""

import urllib.parse

from cherrypy._cpcompat import text_or_bytes

import cherrypy


def expose(func=None, alias=None):
    """Expose the function or class.

    Optionally provide an alias or set of aliases.
    """
    def expose_(func):
        func.exposed = True
        if alias is not None:
            if isinstance(alias, text_or_bytes):
                parents[alias.replace('.', '_')] = func
            else:
                for a in alias:
                    parents[a.replace('.', '_')] = func
        return func

    import sys
    import types
    decoratable_types = types.FunctionType, types.MethodType, type,
    if isinstance(func, decoratable_types):
        if alias is None:
            # @expose
            func.exposed = True
            return func
        else:
            # func = expose(func, alias)
            parents = sys._getframe(1).f_locals
            return expose_(func)
    elif func is None:
        if alias is None:
            # @expose()
            parents = sys._getframe(1).f_locals
            return expose_
        else:
            # @expose(alias="alias") or
            # @expose(alias=["alias1", "alias2"])
            parents = sys._getframe(1).f_locals
            return expose_
    else:
        # @expose("alias") or
        # @expose(["alias1", "alias2"])
        parents = sys._getframe(1).f_locals
        alias = func
        return expose_


def popargs(*args, **kwargs):
    """Decorate _cp_dispatch.

    (cherrypy.dispatch.Dispatcher.dispatch_method_name)

    Optional keyword argument: handler=(Object or Function)

    Provides a _cp_dispatch function that pops off path segments into
    cherrypy.request.params under the names specified.  The dispatch
    is then forwarded on to the next vpath element.

    Note that any existing (and exposed) member function of the class that
    popargs is applied to will override that value of the argument.  For
    instance, if you have a method named "list" on the class decorated with
    popargs, then accessing "/list" will call that function instead of popping
    it off as the requested parameter.  This restriction applies to all
    _cp_dispatch functions.  The only way around this restriction is to create
    a "blank class" whose only function is to provide _cp_dispatch.

    If there are path elements after the arguments, or more arguments
    are requested than are available in the vpath, then the 'handler'
    keyword argument specifies the next object to handle the parameterized
    request.  If handler is not specified or is None, then self is used.
    If handler is a function rather than an instance, then that function
    will be called with the args specified and the return value from that
    function used as the next object INSTEAD of adding the parameters to
    cherrypy.request.args.

    This decorator may be used in one of two ways:

    As a class decorator:

    .. code-block:: python

        @cherrypy.popargs('year', 'month', 'day')
        class Blog:
            def index(self, year=None, month=None, day=None):
                #Process the parameters here; any url like
                #/, /2009, /2009/12, or /2009/12/31
                #will fill in the appropriate parameters.

            def create(self):
                #This link will still be available at /create.
                #Defined functions take precedence over arguments.

    Or as a member of a class:

    .. code-block:: python

        class Blog:
            _cp_dispatch = cherrypy.popargs('year', 'month', 'day')
            #...

    The handler argument may be used to mix arguments with built in functions.
    For instance, the following setup allows different activities at the
    day, month, and year level:

    .. code-block:: python

        class DayHandler:
            def index(self, year, month, day):
                #Do something with this day; probably list entries

            def delete(self, year, month,