The Occasional Occurence

Collection Implementation for CherryPy 2.2

November 07, 2006 at 08:14 AM | categories: Python, computing, cherrypy, General

Inspired by wsgicollection, I cooked up a collection implementation for CherryPy 2.2.x. Here is the docstring for the base Collection class:

A class representing a collection of items.

Items can be created, updated, viewed, listed and deleted.  Dispatches
based on HTTP method and URL fragments.

Classes that need to implement collection-like behavior should inherit from
this class and supply Python method for handling desired HTTP methods.

Something to be aware of is that handlers for the various collection
operations are not "exposed" in the traditional CherryPy sense.  Instead,
they are mapped in the following manner:

HTTP methods meant to operate on the collection and individual collection
items are mapped to handlers in the col_dict and item_dict class attributes,

When a GET request is made for a resource identified by a noun like
/creator_page, a "get_creator_page" method is looked up in the collection
object.  The same thing for a PUT request to /45;metadata - a handler named
"put_metadata" would be looked up in the collection object, and the item
number would be passed in as the first argument to the method.

LIMITATION: only integer item IDs are supported.

The soon-to-be-released CherryPy 3 should make this type of dispatching even easier, but sometimes it's fun to teach an old dog new tricks ;-)