Commit 5185570e authored by Toby Dickenson's avatar Toby Dickenson

refactored much code that conditionally called v.read(). Also, allow the input...

refactored much code that conditionally called v.read(). Also, allow the input to a converter to be a unicode string. This is not used by ZPublisher, but is helpful for other code that uses the converters such as OFS.PropertyManager
parent 1655e985
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE
# #
############################################################################## ##############################################################################
__version__='$Revision: 1.15 $'[11:-2] __version__='$Revision: 1.16 $'[11:-2]
import re import re
from types import ListType, TupleType, UnicodeType from types import ListType, TupleType, UnicodeType
...@@ -18,13 +18,12 @@ from types import ListType, TupleType, UnicodeType ...@@ -18,13 +18,12 @@ from types import ListType, TupleType, UnicodeType
def field2string(v): def field2string(v):
if hasattr(v,'read'): return v.read() if hasattr(v,'read'): return v.read()
elif isinstance(v,UnicodeType) : elif isinstance(v,UnicodeType) :
return v return v.encode('latin1')
else: else:
return str(v) return str(v)
def field2text(v, nl=re.compile('\r\n|\n\r').search): def field2text(v, nl=re.compile('\r\n|\n\r').search):
if hasattr(v,'read'): v=v.read() v = field2string(v)
else: v=str(v)
mo = nl(v) mo = nl(v)
if mo is None: return v if mo is None: return v
l = mo.start(0) l = mo.start(0)
...@@ -42,16 +41,14 @@ def field2text(v, nl=re.compile('\r\n|\n\r').search): ...@@ -42,16 +41,14 @@ def field2text(v, nl=re.compile('\r\n|\n\r').search):
return '\n'.join(r) return '\n'.join(r)
def field2required(v): def field2required(v):
if hasattr(v,'read'): v=v.read() v = field2string(v)
else: v=str(v)
if v.strip(): return v if v.strip(): return v
raise ValueError, 'No input for required field<p>' raise ValueError, 'No input for required field<p>'
def field2int(v): def field2int(v):
if type(v) in (ListType, TupleType): if type(v) in (ListType, TupleType):
return map(field2int, v) return map(field2int, v)
if hasattr(v,'read'): v=v.read() v = field2string(v)
else: v=str(v)
if v: if v:
try: return int(v) try: return int(v)
except ValueError: except ValueError:
...@@ -63,8 +60,7 @@ def field2int(v): ...@@ -63,8 +60,7 @@ def field2int(v):
def field2float(v): def field2float(v):
if type(v) in (ListType, TupleType): if type(v) in (ListType, TupleType):
return map(field2float, v) return map(field2float, v)
if hasattr(v,'read'): v=v.read() v = field2string(v)
else: v=str(v)
if v: if v:
try: return float(v) try: return float(v)
except ValueError: except ValueError:
...@@ -77,9 +73,7 @@ def field2float(v): ...@@ -77,9 +73,7 @@ def field2float(v):
def field2long(v): def field2long(v):
if type(v) in (ListType, TupleType): if type(v) in (ListType, TupleType):
return map(field2long, v) return map(field2long, v)
if hasattr(v,'read'): v=v.read() v = field2string(v)
else: v=str(v)
# handle trailing 'L' if present. # handle trailing 'L' if present.
if v[-1:] in ('L', 'l'): if v[-1:] in ('L', 'l'):
v = v[:-1] v = v[:-1]
...@@ -92,8 +86,7 @@ def field2long(v): ...@@ -92,8 +86,7 @@ def field2long(v):
raise ValueError, 'Empty entry when <strong>integer</strong> expected' raise ValueError, 'Empty entry when <strong>integer</strong> expected'
def field2tokens(v): def field2tokens(v):
if hasattr(v,'read'): v=v.read() v = field2string(v)
else: v=str(v)
return v.split() return v.split()
def field2lines(v): def field2lines(v):
...@@ -106,12 +99,11 @@ def field2lines(v): ...@@ -106,12 +99,11 @@ def field2lines(v):
def field2date(v): def field2date(v):
from DateTime import DateTime from DateTime import DateTime
if hasattr(v,'read'): v=v.read() v = field2string(v)
else: v=str(v)
return DateTime(v) return DateTime(v)
def field2boolean(v): def field2boolean(v):
return v return not not v
class _unicode_converter: class _unicode_converter:
def __call__(self,v): def __call__(self,v):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment