2 # $Id: WebInputMixin.py,v 1.10 2006/01/06 21:56:54 tavis_rudd Exp $
3 """Provides helpers for Template.webInput(), a method for importing web
4 transaction variables in bulk. See the docstring of webInput for full details.
7 ================================================================================
8 Author: Mike Orr <iron@mso.oz.net>
9 License: This software is released for unlimited distribution under the
10 terms of the MIT license. See the LICENSE file.
11 Version: $Revision: 1.10 $
12 Start Date: 2002/03/17
13 Last Revision Date: $Date: 2006/01/06 21:56:54 $
15 __author__
= "Mike Orr <iron@mso.oz.net>"
16 __revision__
= "$Revision: 1.10 $"[11:-2]
18 from Cheetah
.Utils
.Misc
import useOrRaise
20 class NonNumericInputError(ValueError): pass
22 ##################################################
23 ## PRIVATE FUNCTIONS AND CLASSES
26 """A container object for info about type converters.
27 .name, string, name of this converter (for error messages).
28 .func, function, factory function.
29 .default, value to use or raise if the real value is missing.
30 .error, value to use or raise if .func() raises an exception.
32 def __init__(self
, name
, func
, default
, error
):
35 self
.default
= default
39 def _lookup(name
, func
, multi
, converters
):
40 """Look up a Webware field/cookie/value/session value. Return
41 '(realName, value)' where 'realName' is like 'name' but with any
42 conversion suffix strips off. Applies numeric conversion and
43 single vs multi values according to the comments in the source.
45 # Step 1 -- split off the conversion suffix from 'name'; e.g. "height:int".
46 # If there's no colon, the suffix is "". 'longName' is the name with the
47 # suffix, 'shortName' is without.
48 # XXX This implementation assumes "height:" means "height".
49 colon
= name
.find(':')
52 shortName
, ext
= name
[:colon
], name
[colon
+1:]
54 longName
= shortName
= name
57 # Step 2 -- look up the values by calling 'func'.
58 if longName
!= shortName
:
59 values
= func(longName
, None) or func(shortName
, None)
61 values
= func(shortName
, None)
62 # 'values' is a list of strings, a string or None.
64 # Step 3 -- Coerce 'values' to a list of zero, one or more strings.
67 elif isinstance(values
, str):
70 # Step 4 -- Find a _Converter object or raise TypeError.
72 converter
= converters
[ext
]
74 fmt
= "'%s' is not a valid converter name in '%s'"
76 raise TypeError(fmt
% tup
)
78 # Step 5 -- if there's a converter func, run it on each element.
79 # If the converter raises an exception, use or raise 'converter.error'.
80 if converter
.func
is not None:
85 elm
= converter
.func(elm
)
86 except (TypeError, ValueError):
87 tup
= converter
.name
, elm
88 errmsg
= "%s '%s' contains invalid characters" % tup
89 elm
= useOrRaise(converter
.error
, errmsg
)
91 # 'values' is now a list of strings, ints or floats.
93 # Step 6 -- If we're supposed to return a multi value, return the list
94 # as is. If we're supposed to return a single value and the list is
95 # empty, return or raise 'converter.default'. Otherwise, return the
96 # first element in the list and ignore any additional values.
98 return shortName
, values
100 return shortName
, useOrRaise(converter
.default
)
101 return shortName
, values
[0]
103 # vim: sw=4 ts=4 expandtab