A Base Class for Better CherryPy Tools

80 views
Skip to first unread message

Eric Larson

unread,
Sep 18, 2012, 12:15:36 PM9/18/12
to cherryp...@googlegroups.com
Hi All,

I'm a huge fan of CherryPy as it always seems to have the right design,
providing a flexible platform while keeping the mundane details out of
the way. Tools are a great example of this design. Unfortunately,
writing Tools always ends up being somewhat error prone and I end up
using copy and paste. Seeing as the actual API isn't terribly difficult
to get right, I wrote a base class similar to the SimplePlugin base
class for plugins that will hopefully make writing tools a little
easier.

I wrote up a blog about with the code included.

http://ionrock.wordpress.com/2012/09/16/better-cherrypy-tools/[2]

Hopefully it is helpful to someone!

Eric

Sylvain Hellegouarch

unread,
Sep 18, 2012, 3:29:44 PM9/18/12
to cherryp...@googlegroups.com
Hi Eric,

All good stuff really. I'm so used to writing tools that I often forget that their current API may not be as straightforward as one could expect (though hardly complex as you say). I like the idea of automatically registering hookpoints. Though to be fair my tool methods are named after their role not the hook point where they'll be attached.

For instance, let's take this simple redis tool:


It's more useful to me to understand what the methods do rather than where they are actually set to be executed on.
--
- Sylvain
http://www.defuze.org
http://twitter.com/lawouach

Sylvain Hellegouarch

unread,
Sep 18, 2012, 3:31:47 PM9/18/12
to cherryp...@googlegroups.com
Interestingly, a decorator of hookpoint would go a long way.

class MyTool(...):

@cherrypy.hookpoint('before_finalize')
def cleanup_some_resource(self):
    ....
 

Eric Larson

unread,
Sep 18, 2012, 3:53:20 PM9/18/12
to cherryp...@googlegroups.com

Sylvain Hellegouarch <s...@defuze.org> writes:

> On Tue, Sep 18, 2012 at 9:29 PM, Sylvain Hellegouarch <s...@defuze.org> wrote:
> All good stuff really. I'm so used to writing tools that I often forget
> that their current API may not be as straightforward as one could expect
> (though hardly complex as you say).
>

Just to be clear, I don't consider it complex in that it is difficult to
understand or keep straight in your head. Rather the complexity happens
in that there isn't a single prescribed method for specific tasks. A
wealth of options rarely makes a decision easier.

I agree that Tools really are pretty simple, especially compared to WSGI
middleware.

>
> Interestingly, a decorator of hookpoint would go a long way.
>
> class MyTool(...):
>
> @cherrypy.hookpoint('before_finalize')
> def cleanup_some_resource(self):
> ....
>

That is a great idea. I'll play around with it and see what comes of
it. Thanks for the feedback!

Eric
> --
> You received this message because you are subscribed to the Google Groups
> "cherrypy-users" group.
> To post to this group, send email to cherryp...@googlegroups.com.
> To unsubscribe from this group, send email to
> cherrypy-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/
> cherrypy-users?hl=en.

Eric Larson

unread,
Sep 18, 2012, 9:23:53 PM9/18/12
to cherryp...@googlegroups.com

Sylvain Hellegouarch <s...@defuze.org> writes:

> Interestingly, a decorator of hookpoint would go a long way.
>
> class MyTool(...):
>
> @cherrypy.hookpoint('before_finalize')
> def cleanup_some_resource(self):
> ....
>

I played around with this for a minute and something didn't feel
right. I figured it would be just as easy to do something like this
using the same base class:

class SomeTool(SimpleTool):

def cleanup_resources(self):
...
before_finalize = clean_resources

This is possible with a decorator, but this seems just as good IMO.

Thoughts?

Eric

Sylvain Hellegouarch

unread,
Sep 19, 2012, 1:58:02 AM9/19/12
to cherryp...@googlegroups.com
On Wed, Sep 19, 2012 at 3:23 AM, Eric Larson <er...@ionrock.org> wrote:

Sylvain Hellegouarch <s...@defuze.org> writes:

> Interestingly, a decorator of hookpoint would go a long way.
>
> class MyTool(...):
>
> @cherrypy.hookpoint('before_finalize')
> def cleanup_some_resource(self):
>     ....
>

I played around with this for a minute and something didn't feel
right. I figured it would be just as easy to do something like this
using the same base class:

  class SomeTool(SimpleTool):

      def cleanup_resources(self):
          ...
      before_finalize = clean_resources

This is possible with a decorator, but this seems just as good IMO.

Thoughts?


Just as well indeed. Decorators have only the added value of being more dynamic. You could for instance decide not to attach the hookpoint at runtime. But that would make things quite complex at some point. 

Aric Coady

unread,
Sep 19, 2012, 12:52:56 PM9/19/12
to cherryp...@googlegroups.com
On Tuesday, September 18, 2012 6:24:01 PM UTC-7, Eric Larson wrote:

Sylvain Hellegouarch <s...@defuze.org> writes:

> Interestingly, a decorator of hookpoint would go a long way.
>
> class MyTool(...):
>
> @cherrypy.hookpoint('before_finalize')
> def cleanup_some_resource(self):
>     ....
>  

I played around with this for a minute and something didn't feel
right. I figured it would be just as easy to do something like this
using the same base class:

  class SomeTool(SimpleTool):

      def cleanup_resources(self):
          ...
      before_finalize = clean_resources

This is possible with a decorator, but this seems just as good IMO.

Thoughts?

 I think the decorator would be more useful for simple function-based tools.  I've used this:

def tool(hook):
    def decorator(func):
        setattr(cherrypy.tools, func.__name__, cherrypy.Tool(hook, func))
        return func
    return decorator

Reply all
Reply to author
Forward
0 new messages