Initial Commit
[Projects.git] / pkgbuilds / pytivo / pkg / usr / share / pyTivo / Cheetah / Utils / WebInputMixin.py
blob930c13e430a8a78bb688ec49a787663b3968ed1c
1 #!/usr/bin/env python
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.
6 Meta-Data
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 $
14 """
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
25 class _Converter:
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.
31 """
32 def __init__(self, name, func, default, error):
33 self.name = name
34 self.func = func
35 self.default = default
36 self.error = error
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.
44 """
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(':')
50 if colon != -1:
51 longName = name
52 shortName, ext = name[:colon], name[colon+1:]
53 else:
54 longName = shortName = name
55 ext = ''
57 # Step 2 -- look up the values by calling 'func'.
58 if longName != shortName:
59 values = func(longName, None) or func(shortName, None)
60 else:
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.
65 if values is None:
66 values = []
67 elif isinstance(values, str):
68 values = [values]
70 # Step 4 -- Find a _Converter object or raise TypeError.
71 try:
72 converter = converters[ext]
73 except KeyError:
74 fmt = "'%s' is not a valid converter name in '%s'"
75 tup = (ext, longName)
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:
81 tmp = values[:]
82 values = []
83 for elm in tmp:
84 try:
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)
90 values.append(elm)
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.
97 if multi:
98 return shortName, values
99 if len(values) == 0:
100 return shortName, useOrRaise(converter.default)
101 return shortName, values[0]
103 # vim: sw=4 ts=4 expandtab