You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mod_python-commits@quetz.apache.org by gr...@apache.org on 2003/09/15 17:43:33 UTC
cvs commit: httpd-python/lib/python/mod_python util.py
grisha 2003/09/15 08:43:33
Modified: Doc modpython4.tex
lib/python/mod_python util.py
Log:
Per Mark McClain's suggestion, added getfirst() and getlist() to FieldStorage.
Revision Changes Path
1.59 +21 -4 httpd-python/Doc/modpython4.tex
Index: modpython4.tex
===================================================================
RCS file: /home/cvs/httpd-python/Doc/modpython4.tex,v
retrieving revision 1.58
retrieving revision 1.59
diff -u -r1.58 -r1.59
--- modpython4.tex 9 Sep 2003 21:48:00 -0000 1.58
+++ modpython4.tex 15 Sep 2003 15:43:33 -0000 1.59
@@ -1422,10 +1422,10 @@
\begin{notice}
Unlike the standard library \module{cgi} module
\class{FieldStorage} class, a \class{Field} object is returned
- \emph{only} when it is a file upload. In all other cases an instance
- the return is an instance of \class{StringField}, which is a subclass
- of \class{str}. This means that you do not need to use the
- \member{.value} attribute to access values of fields in most cases.
+ \emph{only} when it is a file upload. In all other cases the
+ return is an instance of \class{StringField}. This means that you
+ do not need to use the \member{.value} attribute to access values
+ of fields in most cases.
\end{notice}
In addition to standard mapping object methods, \class{FieldStorage} objects
@@ -1435,6 +1435,23 @@
This is a list of \class{Field} objects, one for each input. Multiple
inputs with the same name will have multiple elements in this list.
\end{memberdesc}
+
+ \class{FieldStorage} methods:
+
+ \begin{methoddesc}[FieldStorage]{getfirst}{name\optional{, default}}
+ Always returns only one value associated with form field
+ \var{name}. If no such form field or value exists then the method
+ returns the value specified by the optional parameter
+ \var{default}. This parameter defaults to \code{None} if not
+ specified.
+ \end{methoddesc}
+
+ \begin{methoddesc}[FieldStorage]{getlist}{name}
+ This method always returns a list of values associated with form
+ field \var{name}. The method returns an empty list if no such form
+ field or value exists for \var{name}. It returns a list consisting
+ of one item if only one such value exists.
+ \end{methoddesc}
\end{classdesc}
1.19 +32 -9 httpd-python/lib/python/mod_python/util.py
Index: util.py
===================================================================
RCS file: /home/cvs/httpd-python/lib/python/mod_python/util.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- util.py 28 Aug 2003 18:47:13 -0000 1.18
+++ util.py 15 Sep 2003 15:43:33 -0000 1.19
@@ -58,7 +58,8 @@
import _apache
import apache
-import StringIO
+import cStringIO
+import tempfile
from types import *
from exceptions import *
@@ -130,7 +131,7 @@
if req.args:
pairs = parse_qsl(req.args, keep_blank_values)
for pair in pairs:
- file = StringIO.StringIO(pair[1])
+ file = cStringIO.StringIO(pair[1])
self.list.append(Field(pair[0], file, "text/plain", {},
None, {}))
@@ -151,7 +152,7 @@
pairs = parse_qsl(req.read(clen), keep_blank_values)
for pair in pairs:
- file = StringIO.StringIO(pair[1])
+ file = cStringIO.StringIO(pair[1])
self.list.append(Field(pair[0], file, "text/plain",
{}, None, {}))
@@ -207,7 +208,7 @@
if disp_options.has_key("filename"):
file = self.make_file()
else:
- file = StringIO.StringIO()
+ file = cStringIO.StringIO()
# read it in
self.read_to_boundary(req, boundary, file)
@@ -228,7 +229,6 @@
def make_file(self):
- import tempfile
return tempfile.TemporaryFile("w+b")
def skip_to_boundary(self, req, boundary):
@@ -263,10 +263,10 @@
found = []
for item in self.list:
if item.name == key:
- if isinstance(item.file, StringIO.StringIO):
- found.append(StringField(item.value))
- else:
+ if isinstance(item.file, FileType):
found.append(item)
+ else:
+ found.append(StringField(item.value))
if not found:
raise KeyError, key
if len(found) == 1:
@@ -302,6 +302,29 @@
def __len__(self):
"""Dictionary style len(x) support."""
return len(self.keys())
+
+ def getfirst(self, key, default=None):
+ """ return the first value received """
+ for item in self.list:
+ if item.name == key:
+ if isinstance(item.file, FileType):
+ return item
+ else:
+ return StringField(item.value)
+ return default
+
+ def getlist(self, key):
+ """ return a list of received values """
+ if self.list is None:
+ raise TypeError, "not indexable"
+ found = []
+ for item in self.list:
+ if item.name == key:
+ if isinstance(item.file, FileType):
+ found.append(item)
+ else:
+ found.append(StringField(item.value))
+ return found
def parse_header(line):
"""Parse a Content-type like header.