Re: Creating decorator that modifies _cp_dispatch

Sviatoslav Sydorenko <[email protected]> Sun, 23 Jun 2019 21:39:48 +0200
Newsgroups gmane.comp.python.cherrypy
Message-ID <CAFYONRCqZ=L+3Dmg1U1yFSZEPkytvqVxk+LYJTLJT+8h4wVXpA@mail.gmail.com>
--0000000000006a654c058c02dc95
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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

=D0=BD=D0=B4, 23 =D1=87=D0=B5=D1=80=D0=B2. 2019 =D0=BE 16:08 Adam Baxter <v=
[email protected]> =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
> out how I'd make a decorator like cherrypy.expose, except for modifying t=
he
> 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 cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
> To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
> 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
> <https://groups.google.com/d/msgid/cherrypy-users/69b04f8b-99ae-4846-8489=
-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 post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
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.

--0000000000006a654c058c02dc95
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">It&#39;s a classic case for decorators with args. You need=
 to write a function which would return decorator, which would return the s=
ubstitution 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 style=3D"">return</span>=C2=A0=
http_handler<span class=3D"gmail-ginger-module-highlighter-mistake-type-3" =
id=3D"gmail-gwmw-15613187006061020916138" style=3D"">(</span>*args, **kwarg=
s)=C2=A0 # &lt;-- 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><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr"=
>=D0=BD=D0=B4, 23 =D1=87=D0=B5=D1=80=D0=B2. 2019 =D0=BE 16:08 Adam Baxter &=
lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt; =
=D0=BF=D0=B8=D1=88=D0=B5:<br></div><blockquote class=3D"gmail_quote" 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&#39;m thinking abo=
ut this the wrong way, but I can&#39;t seem to work out how I&#39;d make a =
decorator like cherrypy.expose, except for modifying the way dispatch works=
.</div><div><br></div><div>What I&#39;d like to do is implement an explicit=
 decorator that /maps/path/segments to variables</div><div><div style=3D"ba=
ckground-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:=
solid;border-width:1px" class=3D"gmail-m_-8686802912904384295prettyprint"><=
code class=3D"gmail-m_-8686802912904384295prettyprint"><div class=3D"gmail-=
m_-8686802912904384295subprettyprint"><div style=3D"color:rgb(212,212,212);=
background-color:rgb(30,30,30);font-family:Consolas,&quot;Courier New&quot;=
,monospace;font-weight:normal;font-size:14px;line-height:19px;white-space:p=
re-wrap"><span style=3D"color:rgb(0,0,0)" class=3D"gmail-m_-868680291290438=
4295styled-by-prettify"><br></span><div><span style=3D"color:rgb(220,220,17=
0)"><span style=3D"color:rgb(0,102,102)" class=3D"gmail-m_-8686802912904384=
295styled-by-prettify">@from_path</span></span><span style=3D"color:rgb(212=
,212,212)"><span style=3D"color:rgb(102,102,0)" class=3D"gmail-m_-868680291=
2904384295styled-by-prettify">(</span></span><span style=3D"color:rgb(206,1=
45,120)"><span style=3D"color:rgb(0,136,0)" class=3D"gmail-m_-8686802912904=
384295styled-by-prettify">&quot;some&quot;</span></span><span style=3D"colo=
r:rgb(212,212,212)"><span style=3D"color:rgb(102,102,0)" class=3D"gmail-m_-=
8686802912904384295styled-by-prettify">,</span></span><span style=3D"color:=
rgb(206,145,120)"><span style=3D"color:rgb(0,136,0)" class=3D"gmail-m_-8686=
802912904384295styled-by-prettify">&quot;variable&quot;</span></span><span =
style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102,102,0)" class=
=3D"gmail-m_-8686802912904384295styled-by-prettify">,</span></span><span st=
yle=3D"color:rgb(206,145,120)"><span style=3D"color:rgb(0,136,0)" class=3D"=
gmail-m_-8686802912904384295styled-by-prettify">&quot;here&quot;</span></sp=
an><span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102,102,=
0)" class=3D"gmail-m_-8686802912904384295styled-by-prettify">)</span></span=
></div><div><span style=3D"color:rgb(86,156,214)"><span style=3D"color:rgb(=
0,0,136)" class=3D"gmail-m_-8686802912904384295styled-by-prettify">def</spa=
n></span><span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(0,=
0,0)" class=3D"gmail-m_-8686802912904384295styled-by-prettify"> </span></sp=
an><span style=3D"color:rgb(220,220,170)"><span style=3D"color:rgb(0,0,0)" =
class=3D"gmail-m_-8686802912904384295styled-by-prettify">my_function</span>=
</span><span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102,=
102,0)" class=3D"gmail-m_-8686802912904384295styled-by-prettify">(</span></=
span><span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102,10=
2,0)" class=3D"gmail-m_-8686802912904384295styled-by-prettify">*</span></sp=
an><span style=3D"color:rgb(156,220,254)"><span style=3D"color:rgb(0,0,0)" =
class=3D"gmail-m_-8686802912904384295styled-by-prettify">args</span></span>=
<span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102,102,0)"=
 class=3D"gmail-m_-8686802912904384295styled-by-prettify">,</span></span><s=
pan style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102,102,0)" c=
lass=3D"gmail-m_-8686802912904384295styled-by-prettify">**</span></span><sp=
an style=3D"color:rgb(156,220,254)"><span style=3D"color:rgb(0,0,0)" class=
=3D"gmail-m_-8686802912904384295styled-by-prettify">kwargs</span></span><sp=
an style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(102,102,0)" cl=
ass=3D"gmail-m_-8686802912904384295styled-by-prettify">):</span></span></di=
v><div><span style=3D"color:rgb(212,212,212)"><span style=3D"color:rgb(0,0,=
0)" class=3D"gmail-m_-8686802912904384295styled-by-prettify"> =C2=A0 =C2=A0=
</span></span><span style=3D"color:rgb(106,153,85)"><span style=3D"color:rg=
b(136,0,0)" class=3D"gmail-m_-8686802912904384295styled-by-prettify">#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)" class=3D"gmail-m_-868=
6802912904384295styled-by-prettify"> =C2=A0 =C2=A0</span></span><span style=
=3D"color:rgb(106,153,85)"><span style=3D"color:rgb(136,0,0)" class=3D"gmai=
l-m_-8686802912904384295styled-by-prettify">#kwargs would be: {&#39;some&#3=
9;: &#39;one&#39;, &#39;variable&#39;: &#39;two&#39;, &#39;here&#39;: &#39;=
three&#39;} </span></span></div></div></div></code></div><br></div><div>but=
 I can&#39;t figure out what I need to pass back to the &quot;pipeline&quot=
; for cherrypy to keep 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&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]" targ=
et=3D"_blank">cherrypy-users+unsubscribe-/[email protected]</a>.<br>
To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl=
egroups.com" target=3D"_blank">cherrypy-users-/[email protected]</a>.<br>
Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use=
rs" target=3D"_blank">https://groups.google.com/group/cherrypy-users</a>.<b=
r>
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&amp;utm_source=3Dfooter" target=3D"_blank">https:=
//groups.google.com/d/msgid/cherrypy-users/69b04f8b-99ae-4846-8489-77e0a37b=
2ce9%40googlegroups.com</a>.<br>
For more options, visit <a href=3D"https://groups.google.com/d/optout" targ=
et=3D"_blank">https://groups.google.com/d/optout</a>.<br>
</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=
 class=3D"gmail_signature"><div dir=3D"ltr">Cheers,<div>Sviatoslav.</div></=
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 post to this group, send email to <a href=3D"mailto:cherrypy-users@googl=
egroups.com">cherrypy-users-/[email protected]</a>.<br />
Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use=
rs">https://groups.google.com/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/CAFYONRCqZ%3DL%2B3Dmg1U1yFSZEPkytvqVxk%2BLYJTLJT%=
2B8h4wVXpA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">https:/=
/groups.google.com/d/msgid/cherrypy-users/CAFYONRCqZ%3DL%2B3Dmg1U1yFSZEPkyt=
vqVxk%2BLYJTLJT%2B8h4wVXpA%40mail.gmail.com</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />

--0000000000006a654c058c02dc95--