List Info

Thread: Tool implementation and Body optimisation




Tool implementation and Body optimisation
user name
2007-03-28 17:28:47
Hello,

I'm currently developping an application used to serve
content in 
different format through HTTP.

I decided to use a Tool that will run at the
'before_finalize' point, 
that will take a look at the Accept header and then will
serialize the 
body obtained from the cherrypy controller to the requested
format.

I guess you can see me coming into a bug.

In fact this simple piece of code won't work:

    class Root:

        _cp_config = {'tools.serializer.on': True}

        def a(self, thing):
            return [thing]
        a.exposed = True

        def b(self, thing):
            return thing
        b.exposed = True

    root = Root()

So that when serializing the output of '/a?thing=toto' to
json I should get
 '["toto"]' and the output of '/b?thing=toto' to
json should return 
 '"toto"'. But of course this does not work.

I tracked down the reason why this does not work. It relies
in the Body
implementation in _cprequest. This implementation for
performance 
reason wraps a string into a list of one element. Using the
collapse_body
method won't work either since then it's the return value of
'a' that is
wrongly set.

The solution I've found so far is to monkey patch
cherrypy.response.Body

So dear list, I was wondering does anybody here got a better
idea ?

PS: Do you have an idea when will "CherryPy
Essentials" will begin 
    shipping ?
    
-- 
(°>  Nicolas Évrard
/ )  Liège - Belgique
^^

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Tool implementation and Body optimisation
country flaguser name
United States
2007-03-28 17:38:52
Nicolas Évrard wrote:
> I'm currently developping an application used to serve
content in 
> different format through HTTP.
> 
> I decided to use a Tool that will run at the
'before_finalize' point, 
> that will take a look at the Accept header and then
will 
> serialize the 
> body obtained from the cherrypy controller to the
requested format.
> 
> I guess you can see me coming into a bug.
> 
> In fact this simple piece of code won't work:
> 
>     class Root:
> 
>         _cp_config = {'tools.serializer.on': True}
> 
>         def a(self, thing):
>             return [thing]
>         a.exposed = True
> 
>         def b(self, thing):
>             return thing
>         b.exposed = True
> 
>     root = Root()
> 
> So that when serializing the output of '/a?thing=toto'
to 
> json I should get
>  '["toto"]' and the output of '/b?thing=toto'
to json should return 
>  '"toto"'. But of course this does not work.
> 
> I tracked down the reason why this does not work. It
relies 
> in the Body
> implementation in _cprequest. This implementation for
performance 
> reason wraps a string into a list of one element. Using
the 
> collapse_body
> method won't work either since then it's the return
value of 
> 'a' that is
> wrongly set.
> 
> The solution I've found so far is to monkey patch 
> cherrypy.response.Body
> 
> So dear list, I was wondering does anybody here got a
better idea ?

Yup. If you're not going to return an iterable (usually a
string) from your page handler, then your best approach is
usually to wrap request.handler. See http://www.
cherrypy.org/wiki/PageHandlers for discussion on how to
do that.

It's up to you WHEN to wrap the request.handler. You can do
that via a custom Dispatcher or via a Tool (e.g.
before_main).


Robert Brewer
System Architect
Amor Ministries
fumanchuamor.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Tool implementation and Body optimisation
user name
2007-03-29 16:18:49
* Robert Brewer  [2007-03-29 00:38 +0200]: 
>> So that when serializing the output of
'/a?thing=toto' to 
>> json I should get
>>  '["toto"]' and the output of
'/b?thing=toto' to json should return 
>>  '"toto"'. But of course this does not
work.
>> 
>> I tracked down the reason why this does not work.
It relies 
>> in the Body
>> implementation in _cprequest. This implementation
for performance 
>> reason wraps a string into a list of one element.
Using the 
>> collapse_body
>> method won't work either since then it's the return
value of 
>> 'a' that is
>> wrongly set.
>> 
>> The solution I've found so far is to monkey patch 
>> cherrypy.response.Body
>> 
>> So dear list, I was wondering does anybody here got
a better idea ?
>
>Yup. If you're not going to return an iterable (usually
a string) from your page handler, then your best approach is
usually to wrap request.handler. See http://www.
cherrypy.org/wiki/PageHandlers for discussion on how to
do that..
>
>It's up to you WHEN to wrap the request.handler. You can
do that via a custom Dispatcher or via a Tool (e.g.
before_main).

Thank you for your quick answer. It works flawlessly.

-- 
(°>  Nicolas Évrard
/ )  Liège - Belgique
^^

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )