I mentioned a possible filter in my earlier message, it's at the bottom
of my email. It will map things with periods in their name to a
dictionary tree. And then you can do things like def method(self,
tree): ... instead of worrying about kwargs.
################################
import cherrypy
from cherrypy.filters.basefilter import BaseFilter
class BuildTreeFilter(BaseFilter):
"""
Will build a tree for request params with periods in the name,
for an example look at _buildTree().
"""
def beforeMain(self):
cherrypy.request.paramMap =
_buildTree(cherrypy.request.paramMap)
def _buildTree(self, oldDict):
"""
Given a dictionary with keys like 'foo.bar.joo', this will
return a new tree
with a key of foo = { 'bar': {'joo' : oldValue}}, for every
key.
Example:
>>> print _buildTree({'company': 'Unexistential Inc',
'names.firstName': ['Joe', 'Boe', 'Moe'],
'names.lastName': ['Smith', 'Myth',
'Keith'],
'phone': '555-8980'})
>>> {'company': 'Unexistential Inc',
'names': {'firstName': ['Joe', 'Boe', 'Moe'],
'lastName': ['Smith', 'Myth', 'Keith']},
'phone': '555-8980'}
"""
stack = []
y = {}
for key, value in oldDict.iteritems():
splitList = key.split(".", 1)
if len(splitList) > 1: #there was a period in the key
firstLevel = splitList[0]
secondLevel = splitList[1]
firstValue = y.get(firstLevel)
if firstValue:
if type(firstValue) != dict:
y[firstLevel] = {}
y[firstLevel].update({secondLevel : value})
else:
y[firstLevel] = {secondLevel : value}
stack.append( firstLevel ) #add to stack only once
else:
y[key] = value
for i in iter(stack):
#y[ i[0] ] = foo(i[1])
y[i] = foo( y[i] )
return y