Re: Creating decorator that modifies _cp_dispatch

Adam Baxter <[email protected]> Sun, 28 Jul 2019 02:50:51 -0700 (PDT)
Newsgroups gmane.comp.python.cherrypy
Message-ID <[email protected]>
------=_Part_7842_1336360771.1564307451223
Content-Type: multipart/alternative; 
	boundary="----=_Part_7843_1622125496.1564307451223"

------=_Part_7843_1622125496.1564307451223
Content-Type: text/plain; charset="UTF-8"

Getting closer:

def from_path(*keys):
    def decorator(http_handler):
        def handler_wrapper(*args, **kwargs):
            kwargs = dict(zip(keys,args))
            return http_handler(args[0], **kwargs) 
        return handler_wrapper
    return decorator

class HelloWorld(object):
    def _cp_dispatch(self, vpath):
        print("dispatching " + repr(vpath))
        return self.index
    
    @cherrypy.expose('/')
    @from_path('one','two','three')
    def index(self, **kwargs):
        return f"My kwargs were {kwargs}"
    
cherrypy.quickstart(HelloWorld())

If I GET /a/b/c, the server responds with:
My kwargs were {'one': <__main__.HelloWorld object at 0x7f45622dd860>, 
'two': 'b', 'three': 'c'}

I am unsure what's eating the first path segment there.

I would like to implement something similar to the way ASP.NET does 
attribute-based routing but I'm not sure how much further I'll get. It's 
been an interesting dive into CherryPy at the least.

-- 
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 cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To view this discussion on the web visit https://groups.google.com/d/msgid/cherrypy-users/40055517-97e3-482b-abe0-ea329ea55f45%40googlegroups.com.

------=_Part_7843_1622125496.1564307451223
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Getting closer:</div><div><span style=3D"font-family:=
 courier new, monospace;"><br></span></div><div><span style=3D"font-family:=
 courier new, monospace;">def from_path(*keys):<br>=C2=A0=C2=A0=C2=A0 def d=
ecorator(http_handler):<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 def h=
andler_wrapper(*args, **kwargs):<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0 kwargs =3D dict(zip(keys,args))<br>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return http_handler(=
args[0], **kwargs) <br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return ha=
ndler_wrapper<br>=C2=A0=C2=A0=C2=A0 return decorator<br><br>class HelloWorl=
d(object):<br>=C2=A0=C2=A0=C2=A0 def _cp_dispatch(self, vpath):<br>=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print(&quot;dispatching &quot; + repr(=
vpath))<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return self.index<br>=
=C2=A0=C2=A0=C2=A0 <br>=C2=A0=C2=A0=C2=A0 @cherrypy.expose(&#39;/&#39;)<br>=
=C2=A0=C2=A0=C2=A0 @from_path(&#39;one&#39;,&#39;two&#39;,&#39;three&#39;)<=
br>=C2=A0=C2=A0=C2=A0 def index(self, **kwargs):<br>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 return f&quot;My kwargs were {kwargs}&quot;<br>=C2=A0=
=C2=A0=C2=A0 <br>cherrypy.quickstart(HelloWorld())</span><br></div><div><br=
></div><div>If I GET /a/b/c, the server responds with:</div><div>My kwargs =
were {&#39;one&#39;: &lt;__main__.HelloWorld object at 0x7f45622dd860&gt;, =
&#39;two&#39;: &#39;b&#39;, &#39;three&#39;: &#39;c&#39;}</div><div><br></d=
iv><div>I am unsure what&#39;s eating the first path segment there.</div><d=
iv><br></div><div>I would like to implement something similar to the way AS=
P.NET does attribute-based routing but I&#39;m not sure how much further I&=
#39;ll get. It&#39;s been an interesting dive into CherryPy at the least.<b=
r></div><div><br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher=
rypy-users+unsubscribe-/[email protected]</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/d/msgid/cherrypy-users/40055517-97e3-482b-abe0-ea329ea55f45%40googlegrou=
ps.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/=
msgid/cherrypy-users/40055517-97e3-482b-abe0-ea329ea55f45%40googlegroups.co=
m</a>.<br />

------=_Part_7843_1622125496.1564307451223--

------=_Part_7842_1336360771.1564307451223--