Creating decorator that modifies _cp_dispatch

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating decorator that modifies _cp_dispatch

Adam Baxter
Hi,
Perhaps I'm thinking about this the wrong way, but I can't seem to work out how I'd make a decorator like cherrypy.expose, except for modifying the way dispatch works.

What I'd like to do is implement an explicit decorator that /maps/path/segments to variables

@from_path("some","variable","here")
def my_function(*args,**kwargs):
   #if I GET /my_function/one/two/three
   #kwargs would be: {'some': 'one', 'variable': 'two', 'here': 'three'}

but I can't figure out what I need to pass back to the "pipeline" for cherrypy to keep working correctly.

Thanks,
Adam

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/69b04f8b-99ae-4846-8489-77e0a37b2ce9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Creating decorator that modifies _cp_dispatch

Sviatoslav Sydorenko (@webknjaz)
It's a classic case for decorators with args. You need to write a function which would return decorator, which would return the substitution for a decorated object.

def from_path(*keys):
    def decorator(http_handler):
        def handler_wrapper(*args, **kwargs):
            return http_handler(*args, **kwargs)  # <-- change kwargs here
        return handler_wrapper
    return decorator

нд, 23 черв. 2019 о 16:08 Adam Baxter <[hidden email]> пише:
Hi,
Perhaps I'm thinking about this the wrong way, but I can't seem to work out how I'd make a decorator like cherrypy.expose, except for modifying the way dispatch works.

What I'd like to do is implement an explicit decorator that /maps/path/segments to variables

@from_path("some","variable","here")
def my_function(*args,**kwargs):
   #if I GET /my_function/one/two/three
   #kwargs would be: {'some': 'one', 'variable': 'two', 'here': 'three'}

but I can't figure out what I need to pass back to the "pipeline" for cherrypy to keep working correctly.

Thanks,
Adam

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/69b04f8b-99ae-4846-8489-77e0a37b2ce9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Cheers,
Sviatoslav.

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/CAFYONRCqZ%3DL%2B3Dmg1U1yFSZEPkytvqVxk%2BLYJTLJT%2B8h4wVXpA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.