11-11-05 07:46 AM
Inspired by our FieldStorage work with file uploads, I also made some
implementation changes to Field and StringField in util.py. In essense,
that will make things like using form['key'] multiple times much more
efficient. Also, FieldStorage will return the same object if called twice.
I'd like some feedback from the group on these changes, and also some
directions on what I'd need to do to get them integrated in the
mod_python beta. I have no experience whatsoever in contributing to open
source projects.
Major change is the Field and FieldStorage implementation:
class Field:
filename = None
headers = {}
def __init__(self, name):
self.name = name
def __repr__(self):
"""Return printable representation."""
return "Field(%s, %s)" % (`self.name`, `self.value`)
def __getattr__(self, name):
if name != 'value':
raise AttributeError, name
if self.file:
self.file.seek(0)
self.value = self.file.read()
self.file.seek(0)
else:
self.value = None
return self.value
def __del__(self):
self.file.close()
class StringField(str):
""" This class is basically a string with
added attributes for compatibility with std lib cgi.py. Basically, this
works the opposite of Field, as it stores its data in a string, but
creates
a file on demand. Field creates a value on demand and stores data
in a file.
"""
filename = None
headers = {}
ctype = "text/plain"
type_options = {}
disposition = None
disp_options = None
# I wanted __init__(name, value) but that does not work
(apparently, you
# cannot subclass str with a constructor that takes >1 argument)
def __init__(self, value):
'''Create StringField instance. You'll have to set name
yourself.'''
str.__init__(self, value)
self.value = value
def __getattr__(self, name):
if name != 'file':
raise AttributeError, name
self.file = cStringIO.StringIO(self.value)
return self.file
I've attached a working util.py (based on 3.1.4, so there are also the
changes to allow large uploads in the file).
[ Post a follow-up to this message ]
|