Re: Creating decorator that modifies _cp_dispatch
Adam Baxter <[email protected]> Sun, 28 Jul 2019 02:18:59 -0700 (PDT)
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_8080_433453732.1564305540010 Content-Type: multipart/alternative; boundary="----=_Part_8081_1321478431.1564305540010" ------=_Part_8081_1321478431.1564305540010 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Monday, 24 June 2019 05:40:03 UTC+10, Sviatoslav Sydorenko (@webknjaz)= =20 wrote: > > It's a classic case for decorators with args. You need to write a functio= n=20 > which would return decorator, which would return the substitution for a= =20 > decorated object. > > def from_path(*keys): > def decorator(http_handler): > def handler_wrapper(*args, **kwargs): > return http_handler(*args, **kwargs) # <-- change kwargs her= e > return handler_wrapper > return decorator > > Apologies for the super delayed response.=20 This looks like what I need, but where does http_handler come from in this= =20 case? --Adam > =D0=BD=D0=B4, 23 =D1=87=D0=B5=D1=80=D0=B2. 2019 =D0=BE 16:08 Adam Baxter = <[email protected] <javascript:>>=20 > =D0=BF=D0=B8=D1=88=D0=B5: > >> Hi, >> Perhaps I'm thinking about this the wrong way, but I can't seem to work= =20 >> out how I'd make a decorator like cherrypy.expose, except for modifying = the=20 >> way dispatch works. >> >> What I'd like to do is implement an explicit decorator that=20 >> /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'}= =20 >> >> but I can't figure out what I need to pass back to the "pipeline" for=20 >> cherrypy to keep working correctly. >> >> Thanks, >> Adam >> >> --=20 >> You received this message because you are subscribed to the Google Group= s=20 >> "cherrypy-users" group. >> To unsubscribe from this group and stop receiving emails from it, send a= n=20 >> email to cherryp...-/[email protected] <javascript:>. >> To post to this group, send email to cherryp...-/[email protected]=20 >> <javascript:>. >> Visit this group at https://groups.google.com/group/cherrypy-users. >> To view this discussion on the web visit=20 >> https://groups.google.com/d/msgid/cherrypy-users/69b04f8b-99ae-4846-8489= -77e0a37b2ce9%40googlegroups.com=20 >> <https://groups.google.com/d/msgid/cherrypy-users/69b04f8b-99ae-4846-848= 9-77e0a37b2ce9%40googlegroups.com?utm_medium=3Demail&utm_source=3Dfooter> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > > --=20 > Cheers, > Sviatoslav. > --=20 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 e= mail to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected] To view this discussion on the web visit https://groups.google.com/d/msgid/= cherrypy-users/b3112eb7-b578-48eb-906a-aadf9bb5b7a5%40googlegroups.com. ------=_Part_8081_1321478431.1564305540010 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><br><br>On Monday, 24 June 2019 05:40:03 UTC+10, Sviatosla= v Sydorenko (@webknjaz) wrote:<blockquote class=3D"gmail_quote" style=3D"m= argin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"= ><div dir=3D"ltr">It's a classic case for decorators with args. You nee= d to write a function which would return decorator, which would return the = substitution for a decorated object.<div><br></div><div>def from_path(*keys= ):</div><div>=C2=A0 =C2=A0 def decorator(http_handler):</div><div>=C2=A0 = =C2=A0 =C2=A0 =C2=A0 def=C2=A0handler_wrapper(*args, **kwargs):</div><div>= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <span>return</span>=C2=A0http_han= dler<span>(</span>*args, **kwargs)=C2=A0 # <-- change kwargs=C2=A0here</= div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return handler_wrapper</div><div>=C2= =A0 =C2=A0 return decorator</div></div><br></blockquote><div><br></div><div= >Apologies for the super delayed response. <br></div><div><br></div><div>Th= is looks like what I need, but where does http_handler come from in this ca= se?</div><div><br></div><div>--Adam<br></div><blockquote class=3D"gmail_quo= te" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;paddi= ng-left: 1ex;"><div class=3D"gmail_quote"><div dir=3D"ltr">=D0=BD=D0=B4, 23= =D1=87=D0=B5=D1=80=D0=B2. 2019 =D0=BE 16:08 Adam Baxter <<a href=3D"jav= ascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"KIFosgfpBwAJ" rel=3D"n= ofollow" onmousedown=3D"this.href=3D'javascript:';return true;" onc= lick=3D"this.href=3D'javascript:';return true;">volt...-PMccDsTWWeQpug/[email protected]= g</a>> =D0=BF=D0=B8=D1=88=D0=B5:<br></div><blockquote class=3D"gmail_quo= te" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204= );padding-left:1ex"><div dir=3D"ltr"><div>Hi,</div><div>Perhaps I'm thi= nking 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 dispa= tch works.</div><div><br></div><div>What I'd like to do is implement an= explicit decorator that /maps/path/segments to variables</div><div><div st= yle=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);bord= er-style:solid;border-width:1px"><code><div><div style=3D"color:rgb(212,212= ,212);background-color:rgb(30,30,30);font-family:Consolas,"Courier New= ",monospace;font-weight:normal;font-size:14px;line-height:19px;white-s= pace:pre-wrap"><span style=3D"color:rgb(0,0,0)"><br></span><div><span style= =3D"color:rgb(220,220,170)"><span style=3D"color:rgb(0,102,102)">@from_path= </span></span><span style=3D"color:rgb(212,212,212)"><span style=3D"color:r= gb(102,102,0)">(</span></span><span style=3D"color:rgb(206,145,120)"><span = style=3D"color:rgb(0,136,0)">"some"</span></span><span style=3D"c= olor:rgb(212,212,212)"><span style=3D"color:rgb(102,102,0)">,</span></span>= <span style=3D"color:rgb(206,145,120)"><span style=3D"color:rgb(0,136,0)">&= quot;variable"</span></span><span style=3D"color:rgb(212,212,212)"><sp= an style=3D"color:rgb(102,102,0)">,</span></span><span style=3D"color:rgb(2= 06,145,120)"><span style=3D"color:rgb(0,136,0)">"<wbr>here"</span= ></span><span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102= ,102,0)">)</span></span></div><div><span style=3D"color:rgb(86,156,214)"><s= pan style=3D"color:rgb(0,0,136)">def</span></span><span style=3D"color:rgb(= 212,212,212)"><span style=3D"color:rgb(0,0,0)"> </span></span><span style= =3D"color:rgb(220,220,170)"><span style=3D"color:rgb(0,0,0)">my_function</s= pan></span><span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(= 102,102,0)">(</span></span><span style=3D"color:rgb(212,212,212)"><span sty= le=3D"color:rgb(102,102,0)">*</span></span><span style=3D"color:rgb(156,220= ,254)"><span style=3D"color:rgb(0,0,0)">args</span></span><span style=3D"co= lor:rgb(212,212,212)"><span style=3D"color:rgb(102,102,0)">,</span></span><= span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102,102,0)">= **</span></span><span style=3D"color:rgb(156,220,254)"><span style=3D"color= :rgb(0,0,0)">kwargs</span></span><span style=3D"color:rgb(212,212,212)"><sp= an style=3D"color:rgb(102,102,0)">):</span></span></div><div><span style=3D= "color:rgb(212,212,212)"><span style=3D"color:rgb(0,0,0)"> =C2=A0 =C2=A0</s= pan></span><span style=3D"color:rgb(106,153,85)"><span style=3D"color:rgb(1= 36,0,0)">#if I GET /my_function/one/two/three</span></span></div><div><span= style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(136,0,0)"> =C2= =A0 =C2=A0</span></span><span style=3D"color:rgb(106,153,85)"><span style= =3D"color:rgb(136,0,0)">#kwargs would be: {'some': 'one', &= #39;variable': 'two', 'here': 'three'} </span><= /span></div></div></div></code></div><br></div><div>but I can't figure = out what I need to pass back to the "pipeline" for cherrypy to ke= ep working correctly.</div><div><br></div><div>Thanks,</div><div>Adam<br></= div></div> <p></p> -- <br> You received this message because you are subscribed to the Google Groups &= quot;cherrypy-users" group.<br> To unsubscribe from this group and stop receiving emails from it, send an e= mail to <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"= KIFosgfpBwAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:&= #39;;return true;" onclick=3D"this.href=3D'javascript:';return true= ;">cherryp...@<wbr>googlegroups.com</a>.<br> To post to this group, send email to <a href=3D"javascript:" target=3D"_bla= nk" gdf-obfuscated-mailto=3D"KIFosgfpBwAJ" rel=3D"nofollow" onmousedown=3D"= this.href=3D'javascript:';return true;" onclick=3D"this.href=3D'= ;javascript:';return true;">cherryp...@googlegroups.<wbr>com</a>.<br> Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use= rs" target=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'http= s://groups.google.com/group/cherrypy-users';return true;" onclick=3D"th= is.href=3D'https://groups.google.com/group/cherrypy-users';return t= rue;">https://groups.google.com/<wbr>group/cherrypy-users</a>.<br> To view this discussion on the web visit <a href=3D"https://groups.google.c= om/d/msgid/cherrypy-users/69b04f8b-99ae-4846-8489-77e0a37b2ce9%40googlegrou= ps.com?utm_medium=3Demail&utm_source=3Dfooter" target=3D"_blank" rel=3D= "nofollow" onmousedown=3D"this.href=3D'https://groups.google.com/d/msgi= d/cherrypy-users/69b04f8b-99ae-4846-8489-77e0a37b2ce9%40googlegroups.com?ut= m_medium\x3demail\x26utm_source\x3dfooter';return true;" onclick=3D"thi= s.href=3D'https://groups.google.com/d/msgid/cherrypy-users/69b04f8b-99a= e-4846-8489-77e0a37b2ce9%40googlegroups.com?utm_medium\x3demail\x26utm_sour= ce\x3dfooter';return true;">https://groups.google.com/d/<wbr>msgid/cher= rypy-users/69b04f8b-<wbr>99ae-4846-8489-77e0a37b2ce9%<wbr>40googlegroups.co= m</a>.<br> For more options, visit <a href=3D"https://groups.google.com/d/optout" targ= et=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'https://grou= ps.google.com/d/optout';return true;" onclick=3D"this.href=3D'https= ://groups.google.com/d/optout';return true;">https://groups.google.com/= d/<wbr>optout</a>.<br> </blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"= ><div dir=3D"ltr">Cheers,<div>Sviatoslav.</div></div></div> </blockquote></div> <p></p> -- <br /> You received this message because you are subscribed to the Google Groups &= quot;cherrypy-users" 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/b3112eb7-b578-48eb-906a-aadf9bb5b7a5%40googlegrou= ps.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d/= msgid/cherrypy-users/b3112eb7-b578-48eb-906a-aadf9bb5b7a5%40googlegroups.co= m</a>.<br /> ------=_Part_8081_1321478431.1564305540010-- ------=_Part_8080_433453732.1564305540010--