List Info

Thread: Parsing Request.params




Parsing Request.params
user name
2006-05-21 08:21:40
Hello,

I'm about to start writting a basic implementation of a
method to parse
the request params, to check if the keys contain braces, the
behaviour
would be similar to PHP, for an instance, when there's a
<input
name="data[10]" value="test">
there will be an array named "data" with
an element "10" with the value test, instead of
a variable named
"data[10]" (which would only work if using
**kwargs)

I'd love to know if there would be some kind of issue
around this or if
somebody has already tried/done it.


I'll be modifying _cphttptools.py around the line 240,
I'll do
something like this:

(warning: pseudo code)
for key in self.params.keys():
  if not key.count('['):
    continue
  openbrace = key.index('[')
  newkey = key[0:openbrace]
  if not self.params.has_key(newkey):
    self.params[newkey] = {}
  self.params[newkey][key[openbrace+1, -1]] =
self.params[key]
  del self.params[key]

This obviously only accepts 1 level of depth, I might want
more depth.

X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.11.53.63 with SMTP id b63mr97085cwa;
        Sun, 21 May 2006 01:21:40 -0700 (PDT)
X-Google-Token: e1uEVgwAAADn4mmWoEHR0l7Qgqjd-Bci
Received: from 201.114.40.175 by
j33g2000cwa.googlegroups.com with HTTP;
	Sun, 21 May 2006 08:21:40 +0000 (UTC)
From: "Sheco" <sergiodurangmail.com>
To: "cherrypy-users" <cherrypy-usersgooglegroups.com>
Subject: Parsing Request.params
Date: Sun, 21 May 2006 01:21:40 -0700
Message-ID: <1148199700.518937.151230j33g2000cwa.googlegroups.com>
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1;
en-US; rv:1.8.0.2) Gecko/20060308
Firefox/1.5.0.2,gzip(gfe),gzip(gfe)
Mime-Version: 1.0
Content-Type: text/plain

Hello,

I'm about to start writting a basic implementation of a
method to parse
the request params, to check if the keys contain braces, the
behaviour
would be similar to PHP, for an instance, when there's a
<input
name="data[10]" value="test">
there will be an array named "data" with
an element "10" with the value test, instead of
a variable named
"data[10]" (which would only work if using
**kwargs)

I'd love to know if there would be some kind of issue
around this or if
somebody has already tried/done it.


I'll be modifying _cphttptools.py around the line 240,
I'll do
something like this:

(warning: pseudo code)
for key in self.params.keys():
  if not key.count('['):
    continue
  openbrace = key.index('[')
  newkey = key[0:openbrace]
  if not self.params.has_key(newkey):
    self.params[newkey] = {}
  self.params[newkey][key[openbrace+1, -1]] =
self.params[key]
  del self.params[key]

This obviously only accepts 1 level of depth, I might want
more depth.


--~--~---------~--~----~------------~-------~--~----~
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 http://
groups.google.com/group/cherrypy-users
-~----------~----~----~----~------~----~------~--~---

Parsing Request.params
user name
2006-05-21 17:38:45
Ok I wrote this function and I just called it at the end of
processBody() in _cphttptools.py, it supports infinite
depth, I can now
have:
<form action="save"
method="post">
<input type="data[1][name]"
value="sergio">
<input type="data[1][email]"
value="myemail.com">
</form>

...

def save(self, data):
   return data

it will return the string representation of the dictionary
"data"
{'1': {'name': 'sergio', 'email': 'myemai.com'}}

This is the method, I've already done some basic tests:

    def update_params(self):
        for paramkey in self.params.keys():
            try:
                openbrace = paramkey.index('[')
            except Exception,e:
                return
            key = paramkey[0:openbrace]
            if not self.params.has_key(key):
                self.params[key] = {}
            current = self.params[key]
            while True:
                closebrace = paramkey.index(']',
openbrace)
                key = paramkey[openbrace+1:closebrace]
                try:
                    openbrace = paramkey.index('[',
closebrace)
                except Exception,e:
                    current[key] = self.params[paramkey]
                    del self.params[paramkey]
                    break

                if not current.has_key(key):
                    current[key] = {}
                current = current[key]

X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.11.53.63 with SMTP id b63mr104829cwa;
        Sun, 21 May 2006 10:38:46 -0700 (PDT)
X-Google-Token: iUsTSAwAAAD-zXV7gKUUKGOn8e_-NozF
Received: from 201.114.40.175 by
j33g2000cwa.googlegroups.com with HTTP;
	Sun, 21 May 2006 17:38:45 +0000 (UTC)
From: "Sheco" <sergiodurangmail.com>
To: "cherrypy-users" <cherrypy-usersgooglegroups.com>
Subject: Re: Parsing Request.params
Date: Sun, 21 May 2006 17:38:45 -0000
Message-ID: <1148233125.997170.306340j33g2000cwa.googlegroups.com>
In-Reply-To: <1148199700.518937.151230j33g2000cwa.googlegroups.com>
References: <1148199700.518937.151230j33g2000cwa.googlegroups.com>
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1;
en-US; rv:1.8.0.2) Gecko/20060308
Firefox/1.5.0.2,gzip(gfe),gzip(gfe)
Mime-Version: 1.0
Content-Type: text/plain

Ok I wrote this function and I just called it at the end of
processBody() in _cphttptools.py, it supports infinite
depth, I can now
have:
<form action="save"
method="post">
<input type="data[1][name]"
value="sergio">
<input type="data[1][email]"
value="myemail.com">
</form>

...

def save(self, data):
   return data

it will return the string representation of the dictionary
"data"
{'1': {'name': 'sergio', 'email': 'myemai.com'}}

This is the method, I've already done some basic tests:

    def update_params(self):
        for paramkey in self.params.keys():
            try:
                openbrace = paramkey.index('[')
            except Exception,e:
                return
            key = paramkey[0:openbrace]
            if not self.params.has_key(key):
                self.params[key] = {}
            current = self.params[key]
            while True:
                closebrace = paramkey.index(']',
openbrace)
                key = paramkey[openbrace+1:closebrace]
                try:
                    openbrace = paramkey.index('[',
closebrace)
                except Exception,e:
                    current[key] = self.params[paramkey]
                    del self.params[paramkey]
                    break

                if not current.has_key(key):
                    current[key] = {}
                current = current[key]


--~--~---------~--~----~------------~-------~--~----~
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 http://
groups.google.com/group/cherrypy-users
-~----------~----~----~----~------~----~------~--~---

[1-2]

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